// RCS_ID: $Id$
// License: see wxWindows license
////////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
+#ifdef __GNUG__
#pragma implementation "socket.h"
// #pragma interface
// #pragma implementation "socket.cpp"
#include <wx/timer.h>
#include <wx/utils.h>
+// Not enough OS behaviour defined for wxStubs
+#ifndef __WXSTUBS__
+
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <unistd.h>
+#ifdef sun
+#include <sys/filio.h>
+#endif
+
#endif // __UNIX__
#include <signal.h>
/////////////////////////////////////////////////////////////////////////////
// wxSocket headers
/////////////////////////////////////////////////////////////////////////////
+#include "wx/module.h"
#define WXSOCK_INTERNAL
#include "wx/sckaddr.h"
#include "wx/socket.h"
#endif
#ifdef __WXMOTIF__
-#define wxAPP_CONTEXT wxTheApp->appContext
+#define wxAPP_CONTEXT ((XtAppContext)wxTheApp->GetAppContext())
#endif
#ifdef __WINDOWS__
// Some patch ///// END
/////////////////////////////////////////////////////////////////////////////
+// --------------------------------------------------------------
+// Module
+// --------------------------------------------------------------
+class wxSocketModule: public wxModule {
+ DECLARE_DYNAMIC_CLASS(wxSocketModule)
+public:
+ wxSocketModule() {}
+ bool OnInit();
+ void OnExit();
+};
+
// --------------------------------------------------------------
// ClassInfos
// --------------------------------------------------------------
IMPLEMENT_CLASS(wxSocketClient, wxSocketBase)
IMPLEMENT_CLASS(wxSocketHandler, wxObject)
IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent)
+IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule)
#endif
class wxSockWakeUp : public wxTimer {
wxSockWakeUp *auto_wakeup;
wxSocketBase::wxRequestNotify type;
};
-
+
/////////////////////////////////////////////////////////////////////////////
// Some internal define
while (node) {
SockRequest *req = (SockRequest *)node->Data();
req->done = TRUE;
-
+
n = node->Next();
delete node;
node = n;
{
SockMsg msg;
size_t len, len2, sig;
-
+
Read((char *)&msg, sizeof(msg));
if (m_lcount != sizeof(msg))
return *this;
wxSocketBase& wxSocketBase::WriteMsg(const char *buffer, size_t nbytes)
{
SockMsg msg;
-
+
msg.sig[0] = (char) 0xad;
msg.sig[1] = (char) 0xde;
msg.sig[2] = (char) 0xed;
if (Write((char *)&msg, sizeof(msg)).LastCount() < sizeof(msg))
return *this;
if (Write(buffer, nbytes).LastCount() < nbytes)
- return *this;
+ return *this;
msg.sig[0] = (char) 0xed;
msg.sig[1] = (char) 0xfe;
SaveState();
SetFlags((wxSockFlags)(NOWAIT | SPEED));
-
+
while (recv_size == MAX_BUFSIZE) {
recv_size = Read(my_data, MAX_BUFSIZE).LastCount();
}
if (m_fd < 0)
return FALSE;
-#ifdef __WINDOWS__
if (getpeername(m_fd, (struct sockaddr *)&my_addr, (int *)&len_addr) < 0)
-#else
- if (getpeername(m_fd, (struct sockaddr *)&my_addr, (unsigned int *)&len_addr) < 0)
-#endif
- return FALSE;
+ return FALSE;
addr_man.Disassemble(&my_addr, len_addr);
return TRUE;
if (m_fd < 0)
return FALSE;
-#ifdef __WINDOWS__
if (getsockname(m_fd, (struct sockaddr *)&my_addr, (int *)&len_addr) < 0)
-#else
- if (getsockname(m_fd, (struct sockaddr *)&my_addr, (unsigned int *)&len_addr) < 0)
-#endif
return FALSE;
addr_man.Disassemble(&my_addr, len_addr);
return;
wxSockState *state = (wxSockState *)node->Data();
-
+
SetFlags(state->flags);
m_neededreq = state->cbk_set;
m_cbk = state->cbk;
if (seconds != -1)
wakeup.Start((int)(seconds*1000 + (microseconds / 1000)), TRUE);
-
+
m_waitflags = 0x80 | type;
while (m_waitflags & 0x80)
PROCESS_EVENTS();
if (ret < 0) {
m_lcount = 0;
m_error = errno;
- } else
+ } else {
m_lcount = ret;
+ m_error = 0;
+ }
}
void wxSocketBase::WantBuffer(char *buffer, size_t nbytes,
SockRequest *buf = new SockRequest;
wxSockWakeUp s_wake(NULL, (int *)&buf_timed_out, (int)TRUE);
-
+
m_wantbuf++;
req_list[evt].Append(buf);
if (m_fd == INVALID_SOCKET)
return;
-
+
int flag = 1;
setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int));
-
+
struct sockaddr *myaddr;
size_t len;
-
+
addr_man.Build(myaddr, len);
if (bind(m_fd, myaddr, addr_man.SockAddrLen()) < 0)
return;
-
+
if (listen(m_fd, 5) < 0) {
m_fd = INVALID_SOCKET;
return;
bool wxSocketServer::AcceptWith(wxSocketBase& sock)
{
int fd2;
-
+
if ((fd2 = accept(m_fd, 0, 0)) < 0)
return FALSE;
struct linger linger;
linger.l_onoff = 0;
linger.l_linger = 1;
-
+
setsockopt(fd2, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger));
-
+
int flag = 0;
setsockopt(fd2, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int));
-
+
if (!(sock.m_flags & SPEED)) {
unsigned long flag2 = 1;
ioctl(fd2, FIONBIO, &flag2);
}
-
+
sock.m_type = SOCK_INTERNAL;
sock.m_fd = fd2;
sock.m_connected = TRUE;
Close();
m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0);
-
+
if (m_fd < 0)
return FALSE;
-
+
m_connected = FALSE;
linger.l_onoff = 1;
- linger.l_linger = 5;
+ linger.l_linger = 5;
setsockopt(m_fd, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger));
-
+
// Stay in touch with the state of things...
-
+
unsigned long flag = 1;
setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int));
-
+
// Disable the nagle algorithm, which delays sends till the
// buffer is full (or a certain time period has passed?)...
flag = 1;
setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
#endif
-
+
struct sockaddr *remote;
size_t len;
flag = 1;
ioctl(m_fd, FIONBIO, &flag);
}
-
+
Notify(TRUE);
m_connected = TRUE;
return TRUE;
}
-bool wxSocketClient::WaitOnConnect(long seconds)
+bool wxSocketClient::WaitOnConnect(long seconds, long microseconds)
{
- int ret = _Wait(seconds, 0, REQ_CONNECT | REQ_LOST);
-
+ int ret = _Wait(seconds, microseconds, REQ_CONNECT | REQ_LOST);
+
if (ret)
m_connected = TRUE;
-
- return m_connected;
+
+ return m_connected;
}
void wxSocketClient::OnRequest(wxRequestEvent evt)
return;
}
m_waitflags = 0x40;
- m_connected = TRUE;
+ m_connected = TRUE;
OldOnNotify(EVT_CONNECT);
DestroyCallbacks();
return;
LRESULT APIENTRY _EXPORT wxSocketHandlerWndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
+ if(message==WM_DESTROY)
+ {
+ ::SetWindowLong(hWnd, GWL_WNDPROC, (LONG) DefWindowProc);
+ return DefWindowProc(hWnd, message, wParam, lParam);
+ }
wxSocketHandler *h_sock = (wxSocketHandler *)GetWindowLong(hWnd, GWL_USERDATA);
wxNode *node = h_sock->smsg_list->Find(message);
wxSocketBase *sock;
for (node = socks->First(); node; node = node->Next()) {
wxSocketBase* s = (wxSocketBase*)node->Data();
-
+
if (s == sock) {
delete node;
sock->DestroyCallbacks();
}
#endif
+
+bool wxSocketModule::OnInit() {
+ wxSocketHandler::master = new wxSocketHandler();
+ return TRUE;
+}
+
+void wxSocketModule::OnExit() {
+ delete wxSocketHandler::master;
+ wxSocketHandler::master = NULL;
+}
+
+#endif
+ // __WXSTUBS__