// RCS_ID: $Id$
// License: see wxWindows license
////////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
+#ifdef __GNUG__
#pragma implementation "socket.h"
-#pragma interface
-#pragma implementation "socket.cpp"
+// #pragma interface
+// #pragma implementation "socket.cpp"
#endif
// For compilers that support precompilation, includes "wx.h".
#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>
#ifdef __WINDOWS__
#define close closesocket
#define ioctl ioctlsocket
+#ifdef errno
+#undef errno
+#endif
#define errno WSAGetLastError()
#ifdef EWOULDBLOCK
#undef EWOULDBLOCK
#endif
#ifdef __WXMOTIF__
-#define wxAPP_CONTEXT wxTheApp->appContext
+#define wxAPP_CONTEXT ((XtAppContext)wxTheApp->GetAppContext())
#endif
#ifdef __WINDOWS__
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] = 0xad;
- msg.sig[1] = 0xde;
- msg.sig[2] = 0xed;
- msg.sig[3] = 0xfe;
- msg.len[0] = nbytes & 0xff;
- msg.len[1] = (nbytes >> 8) & 0xff;
- msg.len[2] = (nbytes >> 16) & 0xff;
- msg.len[3] = (nbytes >> 24) & 0xff;
+ msg.sig[0] = (char) 0xad;
+ msg.sig[1] = (char) 0xde;
+ msg.sig[2] = (char) 0xed;
+ msg.sig[3] = (char) 0xfe;
+
+ msg.len[0] = (char) nbytes & 0xff;
+ msg.len[1] = (char) (nbytes >> 8) & 0xff;
+ msg.len[2] = (char) (nbytes >> 16) & 0xff;
+ msg.len[3] = (char) (nbytes >> 24) & 0xff;
if (Write((char *)&msg, sizeof(msg)).LastCount() < sizeof(msg))
return *this;
if (Write(buffer, nbytes).LastCount() < nbytes)
- return *this;
+ return *this;
- msg.sig[0] = 0xed;
- msg.sig[1] = 0xfe;
- msg.sig[2] = 0xad;
- msg.sig[3] = 0xde;
- msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = 0;
+ msg.sig[0] = (char) 0xed;
+ msg.sig[1] = (char) 0xfe;
+ msg.sig[2] = (char) 0xad;
+ msg.sig[3] = (char) 0xde;
+ msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0;
Write((char *)&msg, sizeof(msg));
return *this;
FD_ZERO(&sock_set);
FD_SET(m_fd, &sock_set);
select(FD_SETSIZE, &sock_set, NULL, NULL, &tv);
- return FD_ISSET(m_fd, &sock_set);
+ return (FD_ISSET(m_fd, &sock_set) != 0);
}
// ---------------------------------------------------------------------
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();
void wxSocketBase::WantSpeedBuffer(char *buffer, size_t nbytes,
wxRequestEvent evt)
{
- int ret;
+ int ret = 0;
switch (evt) {
case EVT_PEEK:
case EVT_READ:
- ret = read(m_fd, buffer, nbytes);
+ ret = recv(m_fd, buffer, nbytes,
+ (evt == EVT_PEEK) ? MSG_PEEK : 0);
break;
case EVT_WRITE:
- ret = write(m_fd, buffer, nbytes);
+ ret = send(m_fd, buffer, nbytes, 0);
break;
}
if (ret < 0) {
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;
// --------------------------------------------------------------
// --------- wxSocketClient Connect functions -------------------
// --------------------------------------------------------------
-bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait)
+bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) )
{
struct linger linger;
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;
for (node = socks->First(); node; node = node->Next()) {
wxSocketBase* s = (wxSocketBase*)node->Data();
-
+
if (s == sock) {
delete node;
sock->DestroyCallbacks();
}
#endif
+
+#endif
+ // __WXSTUBS__