#include "wx/module.h"
#include "wx/log.h"
#include "wx/intl.h"
-#include "wx/gdicmn.h" // for wxPendingDelete
+#include "wx/event.h"
+
+#if wxUSE_GUI
+ #include "wx/gdicmn.h" // for wxPendingDelete
+#endif // wxUSE_GUI
#include "wx/sckaddr.h"
#include "wx/socket.h"
#define MAX_DISCARD_SIZE (10 * 1024)
// what to do within waits
-#define PROCESS_EVENTS() wxYield()
+#if wxUSE_GUI
+ #define PROCESS_EVENTS() wxYield()
+#else
+ #define PROCESS_EVENTS()
+#endif
// use wxPostEvent or not
#define USE_DELAYED_EVENTS 1
+
// --------------------------------------------------------------------------
// ClassInfos
// --------------------------------------------------------------------------
public:
bool m_notify_state;
wxSocketEventFlags m_neededreq;
- wxSockFlags m_flags;
+ wxSocketFlags m_flags;
wxSocketBase::wxSockCbk m_cbk;
char *m_cdata;
// Ctor and dtor
// --------------------------------------------------------------------------
-wxSocketBase::wxSocketBase(wxSockFlags _flags, wxSockType _type) :
- wxEvtHandler(),
- m_socket(NULL), m_evt_handler(NULL), m_id(-1),
- m_flags(_flags), m_type(_type),
- m_neededreq(0), m_notify_state(FALSE),
- m_connected(FALSE), m_establishing(FALSE),
- m_reading(FALSE), m_writing(FALSE),
- m_error(FALSE), m_lcount(0), m_timeout(600), m_states(),
- m_unread(NULL), m_unrd_size(0), m_unrd_cur(0),
- m_cbk(NULL), m_cdata(NULL)
+void wxSocketBase::Init()
{
+ m_socket = NULL;
+ m_type = wxSOCKET_UNINIT;
+
+ // state
+ m_flags = 0;
+ m_connected =
+ m_establishing =
+ m_reading =
+ m_writing =
+ m_error = FALSE;
+ m_lcount = 0;
+ m_timeout = 600;
+ m_beingDeleted = FALSE;
+
+ // pushback buffer
+ m_unread = NULL;
+ m_unrd_size = 0;
+ m_unrd_cur = 0;
+
+ // events
+ m_id = -1;
+ m_handler = NULL;
+ m_clientData = NULL;
+ m_notify_state = FALSE;
+ m_neededreq = 0;
+ m_cbk = NULL;
+ m_cdata = NULL;
}
-wxSocketBase::wxSocketBase() :
- wxEvtHandler(),
- m_socket(NULL), m_evt_handler(NULL), m_id(-1),
- m_flags(NONE), m_type(SOCK_UNINIT),
- m_neededreq(0), m_notify_state(FALSE),
- m_connected(FALSE), m_establishing(FALSE),
- m_reading(FALSE), m_writing(FALSE),
- m_error(FALSE), m_lcount(0), m_timeout(600), m_states(),
- m_unread(NULL), m_unrd_size(0), m_unrd_cur(0),
- m_cbk(NULL), m_cdata(NULL)
+wxSocketBase::wxSocketBase()
{
+ Init();
+}
+
+wxSocketBase::wxSocketBase(wxSocketFlags flags, wxSocketType type)
+{
+ Init();
+
+ m_flags = flags;
+ m_type = type;
}
wxSocketBase::~wxSocketBase()
{
- if (m_unread)
- free(m_unread);
+ // Just in case the app called Destroy() *and* then deleted
+ // the socket immediately: don't leave dangling pointers.
+#if wxUSE_GUI
+ wxPendingDelete.DeleteObject(this);
+#endif
// Shutdown and close the socket
- Close();
+ if (!m_beingDeleted)
+ Close();
// Destroy the GSocket object
if (m_socket)
GSocket_destroy(m_socket);
+
+ // Free the pushback buffer
+ if (m_unread)
+ free(m_unread);
}
-/*
bool wxSocketBase::Destroy()
{
// Delayed destruction: the socket will be deleted during the next
// idle loop iteration. This ensures that all pending events have
// been processed.
-
m_beingDeleted = TRUE;
+
+ // Shutdown and close the socket
Close();
- if ( !wxPendingDelete.Member(this) )
+#if wxUSE_GUI
+ if ( wxPendingDelete.Member(this) )
wxPendingDelete.Append(this);
+#else
+ delete this;
+#endif
return TRUE;
}
-*/
+
// --------------------------------------------------------------------------
// Basic IO operations
return TRUE;
}
-wxSocketBase& wxSocketBase::Read(char* buffer, wxUint32 nbytes)
+wxSocketBase& wxSocketBase::Read(void* buffer, wxUint32 nbytes)
{
// Mask read events
m_reading = TRUE;
return *this;
}
-wxUint32 wxSocketBase::_Read(char* buffer, wxUint32 nbytes)
+wxUint32 wxSocketBase::_Read(void* buffer, wxUint32 nbytes)
{
int total;
int ret = 1;
// Try the pushback buffer first
total = GetPushback(buffer, nbytes, FALSE);
nbytes -= total;
- buffer += total;
+ buffer = (char *)buffer + total;
// If the socket is invalid or we got all the data, return now
if (!m_socket || !nbytes)
if (m_flags & wxSOCKET_NOWAIT)
{
GSocket_SetNonBlocking(m_socket, TRUE);
- ret = GSocket_Read(m_socket, buffer, nbytes);
+ ret = GSocket_Read(m_socket, (char *)buffer, nbytes);
GSocket_SetNonBlocking(m_socket, FALSE);
if (ret > 0)
if (!(m_flags & wxSOCKET_BLOCK) && !WaitForRead())
break;
- ret = GSocket_Read(m_socket, buffer, nbytes);
+ ret = GSocket_Read(m_socket, (char *)buffer, nbytes);
if (ret > 0)
{
total += ret;
- buffer += ret;
nbytes -= ret;
+ buffer = (char *)buffer + ret;
}
}
}
{
if ((m_flags & wxSOCKET_BLOCK) || WaitForRead())
{
- ret = GSocket_Read(m_socket, buffer, nbytes);
+ ret = GSocket_Read(m_socket, (char *)buffer, nbytes);
if (ret > 0)
total += ret;
return total;
}
-wxSocketBase& wxSocketBase::ReadMsg(char* buffer, wxUint32 nbytes)
+wxSocketBase& wxSocketBase::ReadMsg(void* buffer, wxUint32 nbytes)
{
wxUint32 len, len2, sig, total;
bool error;
old_flags = m_flags;
SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL);
- if (_Read((char *)&msg, sizeof(msg)) != sizeof(msg))
+ if (_Read(&msg, sizeof(msg)) != sizeof(msg))
goto exit;
- sig = (wxUint32)msg.sig[0];
+ sig = (wxUint32)msg.sig[0];
sig |= (wxUint32)(msg.sig[1] << 8);
sig |= (wxUint32)(msg.sig[2] << 16);
sig |= (wxUint32)(msg.sig[3] << 24);
if (len2 != 0)
goto exit;
}
- if (_Read((char *)&msg, sizeof(msg)) != sizeof(msg))
+ if (_Read(&msg, sizeof(msg)) != sizeof(msg))
goto exit;
sig = (wxUint32)msg.sig[0];
return *this;
}
-wxSocketBase& wxSocketBase::Peek(char* buffer, wxUint32 nbytes)
+wxSocketBase& wxSocketBase::Peek(void* buffer, wxUint32 nbytes)
{
// Mask read events
m_reading = TRUE;
return *this;
}
-wxSocketBase& wxSocketBase::Write(const char *buffer, wxUint32 nbytes)
+wxSocketBase& wxSocketBase::Write(const void *buffer, wxUint32 nbytes)
{
// Mask write events
m_writing = TRUE;
return *this;
}
-wxUint32 wxSocketBase::_Write(const char *buffer, wxUint32 nbytes)
+wxUint32 wxSocketBase::_Write(const void *buffer, wxUint32 nbytes)
{
wxUint32 total = 0;
int ret = 1;
if (m_flags & wxSOCKET_NOWAIT)
{
GSocket_SetNonBlocking(m_socket, TRUE);
- ret = GSocket_Write(m_socket, buffer, nbytes);
+ ret = GSocket_Write(m_socket, (const char *)buffer, nbytes);
GSocket_SetNonBlocking(m_socket, FALSE);
if (ret > 0)
if (!(m_flags & wxSOCKET_BLOCK) && !WaitForWrite())
break;
- ret = GSocket_Write(m_socket, buffer, nbytes);
+ ret = GSocket_Write(m_socket, (const char *)buffer, nbytes);
if (ret > 0)
{
total += ret;
- buffer += ret;
nbytes -= ret;
+ buffer = (const char *)buffer + ret;
}
}
}
{
if ((m_flags & wxSOCKET_BLOCK) || WaitForWrite())
{
- ret = GSocket_Write(m_socket, buffer, nbytes);
+ ret = GSocket_Write(m_socket, (const char *)buffer, nbytes);
if (ret > 0)
total = ret;
return total;
}
-wxSocketBase& wxSocketBase::WriteMsg(const char *buffer, wxUint32 nbytes)
+wxSocketBase& wxSocketBase::WriteMsg(const void *buffer, wxUint32 nbytes)
{
wxUint32 total;
bool error;
int old_flags;
- struct {
+ struct
+ {
unsigned char sig[4];
unsigned char len[4];
} msg;
msg.len[2] = (unsigned char) ((nbytes >> 16) & 0xff);
msg.len[3] = (unsigned char) ((nbytes >> 24) & 0xff);
- if (_Write((char *)&msg, sizeof(msg)) < sizeof(msg))
+ if (_Write(&msg, sizeof(msg)) < sizeof(msg))
goto exit;
total = _Write(buffer, nbytes);
msg.sig[3] = (unsigned char) 0xde;
msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0;
- if ((_Write((char *)&msg, sizeof(msg))) < sizeof(msg))
+ if ((_Write(&msg, sizeof(msg))) < sizeof(msg))
goto exit;
// everything was OK
return *this;
}
-wxSocketBase& wxSocketBase::Unread(const char *buffer, wxUint32 nbytes)
+wxSocketBase& wxSocketBase::Unread(const void *buffer, wxUint32 nbytes)
{
if (nbytes != 0)
Pushback(buffer, nbytes);
// timeout elapses. The polling loop calls PROCESS_EVENTS(), so
// this won't block the GUI.
-class _wxSocketInternalTimer: public wxTimer
-{
-public:
- int *m_state;
- unsigned long m_new_val;
-
- void Notify()
- {
- *m_state = (int)m_new_val; // Change the value
- }
-};
-
bool wxSocketBase::_Wait(long seconds, long milliseconds,
wxSocketEventFlags flags)
{
GSocketEventFlags result;
- _wxSocketInternalTimer timer;
long timeout;
- int state = -1;
// Set this to TRUE to interrupt ongoing waits
m_interrupt = FALSE;
if (!m_socket)
return FALSE;
- // Check for valid timeout value
+ // Check for valid timeout value.
if (seconds != -1)
timeout = seconds * 1000 + milliseconds;
else
timeout = m_timeout * 1000;
- // Activate timer
- if (timeout)
- {
- timer.m_state = &state;
- timer.m_new_val = 0;
- timer.Start((int)timeout, TRUE);
- }
-
// Active polling (without using events)
//
// NOTE: this duplicates some of the code in OnRequest (lost
// Do this at least once (important if timeout == 0, when
// we are just polling). Also, if just polling, do not yield.
- while (state == -1)
+ wxStopWatch chrono;
+ bool done = FALSE;
+
+ while (!done)
{
result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG);
// Incoming connection (server) or connection established (client)
if (result & GSOCK_CONNECTION_FLAG)
{
- timer.Stop();
m_connected = TRUE;
m_establishing = FALSE;
return TRUE;
// Data available or output buffer ready
if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG))
{
- timer.Stop();
return TRUE;
}
// Connection lost
if (result & GSOCK_LOST_FLAG)
{
- timer.Stop();
m_connected = FALSE;
m_establishing = FALSE;
return (flags & GSOCK_LOST_FLAG);
}
// Wait more?
- if ((timeout == 0) || (m_interrupt))
- break;
+ if ((!timeout) || (chrono.Time() > timeout) || (m_interrupt))
+ done = TRUE;
else
PROCESS_EVENTS();
}
- timer.Stop();
return FALSE;
}
GSocket_SetTimeout(m_socket, m_timeout * 1000);
}
-void wxSocketBase::SetFlags(wxSockFlags _flags)
+void wxSocketBase::SetFlags(wxSocketFlags flags)
{
- m_flags = _flags;
+ m_flags = flags;
}
+
// --------------------------------------------------------------------------
// Callbacks (now obsolete - use events instead)
// --------------------------------------------------------------------------
}
// --------------------------------------------------------------------------
-// Event system
+// Event handling
// --------------------------------------------------------------------------
-// All events (INPUT, OUTPUT, CONNECTION, LOST) are now always
-// internally watched; but users will only be notified of those
-// events they are interested in.
+// Callback function from GSocket. All events are internally
+// monitored, but users only get these they are interested in.
static void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket),
- GSocketEvent event,
+ GSocketEvent notify,
char *cdata)
{
wxSocketBase *sckobj = (wxSocketBase *)cdata;
- sckobj->OnRequest((wxSocketNotify)event);
-}
-
-wxSocketEventFlags wxSocketBase::EventToNotify(wxSocketNotify evt)
-{
- switch (evt)
- {
- case GSOCK_INPUT: return GSOCK_INPUT_FLAG;
- case GSOCK_OUTPUT: return GSOCK_OUTPUT_FLAG;
- case GSOCK_CONNECTION: return GSOCK_CONNECTION_FLAG;
- case GSOCK_LOST: return GSOCK_LOST_FLAG;
- }
- return 0;
-}
-
-void wxSocketBase::SetNotify(wxSocketEventFlags flags)
-{
- m_neededreq = flags;
+ sckobj->OnRequest((wxSocketNotify) notify);
}
-void wxSocketBase::Notify(bool notify)
-{
- m_notify_state = notify;
-}
void wxSocketBase::OnRequest(wxSocketNotify req_evt)
{
- wxSocketEvent event(m_id);
- wxSocketEventFlags flag = EventToNotify(req_evt);
-
// This duplicates some code in _Wait, but this doesn't
// hurt. It has to be here because we don't know whether
// the Wait functions will be used, and it has to be in
break;
}
+ // Schedule the event
+
+ wxSocketEventFlags flag = -1;
+ switch (req_evt)
+ {
+ case GSOCK_INPUT: flag = GSOCK_INPUT_FLAG; break;
+ case GSOCK_OUTPUT: flag = GSOCK_OUTPUT_FLAG; break;
+ case GSOCK_CONNECTION: flag = GSOCK_CONNECTION_FLAG; break;
+ case GSOCK_LOST: flag = GSOCK_LOST_FLAG; break;
+ }
+
if (((m_neededreq & flag) == flag) && m_notify_state)
{
- event.m_socket = this;
- event.m_skevt = req_evt;
+ wxSocketEvent event(m_id);
- if (m_evt_handler)
+ event.m_event = req_evt;
+ event.m_clientData = m_clientData;
+ event.SetEventObject(this);
+
+ if (m_handler)
#if USE_DELAYED_EVENTS
- wxPostEvent(m_evt_handler, event);
+ m_handler->AddPendingEvent(event);
#else
- ProcessEvent(event);
+ m_handler->ProcessEvent(event);
#endif
- OldOnNotify(req_evt);
if (m_cbk)
m_cbk(*this, req_evt, m_cdata);
}
}
-void wxSocketBase::OldOnNotify(wxSocketNotify WXUNUSED(evt))
+void wxSocketBase::Notify(bool notify)
{
+ m_notify_state = notify;
}
-void wxSocketBase::SetEventHandler(wxEvtHandler& h_evt, int id)
+void wxSocketBase::SetNotify(wxSocketEventFlags flags)
{
- m_evt_handler = &h_evt;
- m_id = id;
+ m_neededreq = flags;
+}
- SetNextHandler(&h_evt);
+void wxSocketBase::SetEventHandler(wxEvtHandler& handler, int id)
+{
+ m_handler = &handler;
+ m_id = id;
}
+
// --------------------------------------------------------------------------
// Pushback buffer
// --------------------------------------------------------------------------
-void wxSocketBase::Pushback(const char *buffer, wxUint32 size)
+void wxSocketBase::Pushback(const void *buffer, wxUint32 size)
{
if (!size) return;
if (m_unread == NULL)
- m_unread = (char *)malloc(size);
+ m_unread = malloc(size);
else
{
- char *tmp;
+ void *tmp;
- tmp = (char *)malloc(m_unrd_size + size);
- memcpy(tmp+size, m_unread, m_unrd_size);
+ tmp = malloc(m_unrd_size + size);
+ memcpy((char *)tmp + size, m_unread, m_unrd_size);
free(m_unread);
m_unread = tmp;
memcpy(m_unread, buffer, size);
}
-wxUint32 wxSocketBase::GetPushback(char *buffer, wxUint32 size, bool peek)
+wxUint32 wxSocketBase::GetPushback(void *buffer, wxUint32 size, bool peek)
{
if (!m_unrd_size)
return 0;
if (size > (m_unrd_size-m_unrd_cur))
size = m_unrd_size-m_unrd_cur;
- memcpy(buffer, (m_unread+m_unrd_cur), size);
+ memcpy(buffer, (char *)m_unread + m_unrd_cur, size);
if (!peek)
{
return size;
}
+
// ==========================================================================
-// wxSocketServer
+// wxSocketServer
// ==========================================================================
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
- wxSockFlags flags)
- : wxSocketBase(flags, SOCK_SERVER)
+ wxSocketFlags flags)
+ : wxSocketBase(flags, wxSOCKET_SERVER)
{
// Create the socket
m_socket = GSocket_new();
if (!child_socket)
return FALSE;
- sock.m_type = SOCK_INTERNAL;
+ sock.m_type = wxSOCKET_BASE;
sock.m_socket = child_socket;
sock.m_connected = TRUE;
{
wxSocketBase* sock = new wxSocketBase();
- sock->SetFlags((wxSockFlags)m_flags);
+ sock->SetFlags(m_flags);
if (!AcceptWith(*sock, wait))
return NULL;
// Ctor and dtor
// --------------------------------------------------------------------------
-wxSocketClient::wxSocketClient(wxSockFlags _flags)
- : wxSocketBase(_flags, SOCK_CLIENT)
+wxSocketClient::wxSocketClient(wxSocketFlags flags)
+ : wxSocketBase(flags, wxSOCKET_CLIENT)
{
}
-// XXX: What is this for ?
wxSocketClient::~wxSocketClient()
{
}
if (!m_establishing || !m_socket) // No connection in progress
return FALSE;
- return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG);
+ return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG |
+ GSOCK_LOST_FLAG);
}
// ==========================================================================
/* NOTE: experimental stuff - might change */
-wxDatagramSocket::wxDatagramSocket( wxSockAddress& addr, wxSockFlags flags )
- : wxSocketBase( flags, SOCK_DATAGRAM )
+wxDatagramSocket::wxDatagramSocket( wxSockAddress& addr,
+ wxSocketFlags flags )
+ : wxSocketBase( flags, wxSOCKET_DATAGRAM )
{
// Create the socket
m_socket = GSocket_new();
}
wxDatagramSocket& wxDatagramSocket::RecvFrom( wxSockAddress& addr,
- char* buf,
+ void* buf,
wxUint32 nBytes )
{
Read(buf, nBytes);
}
wxDatagramSocket& wxDatagramSocket::SendTo( wxSockAddress& addr,
- const char* buf,
+ const void* buf,
wxUint32 nBytes )
{
GSocket_SetPeer(m_socket, addr.GetAddress());
// wxSocketEvent
// ==========================================================================
-// XXX: Should be moved to event.cpp ?
-
-wxSocketEvent::wxSocketEvent(int id)
- : wxEvent(id)
+wxSocketEvent::wxSocketEvent(int id) : wxEvent(id)
{
- wxEventType type = (wxEventType)wxEVT_SOCKET;
-
- SetEventType(type);
+ SetEventType( (wxEventType)wxEVT_SOCKET );
}
-void wxSocketEvent::CopyObject(wxObject& obj_d) const
+void wxSocketEvent::CopyObject(wxObject& object_dest) const
{
- wxSocketEvent *event = (wxSocketEvent *)&obj_d;
+ wxSocketEvent *event = (wxSocketEvent *)&object_dest;
- wxEvent::CopyObject(obj_d);
+ wxEvent::CopyObject(object_dest);
- event->m_skevt = m_skevt;
- event->m_socket = m_socket;
+ event->m_event = m_event;
+ event->m_clientData = m_clientData;
}
+
// ==========================================================================
// wxSocketModule
// ==========================================================================