class wxSocketState : public wxObject {
public:
- bool notify_state;
+ bool notify_state;
GSocketEventFlags evt_notify_state;
wxSocketBase::wxSockFlags socket_flags;
wxSocketBase::wxSockCbk c_callback;
wxEvtHandler(),
m_socket(NULL), m_flags(_flags), m_type(_type),
m_neededreq(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG),
- m_lcount(0), m_timeout(3600),
+ m_lcount(0), m_timeout(600),
m_unread(NULL), m_unrd_size(0), m_unrd_cur(0),
m_cbk(NULL), m_cdata(NULL),
m_connected(FALSE), m_notify_state(FALSE), m_id(-1),
wxEvtHandler(),
m_socket(NULL), m_flags(SPEED | WAITALL), m_type(SOCK_UNINIT),
m_neededreq(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG),
- m_lcount(0), m_timeout(3600),
+ m_lcount(0), m_timeout(600),
m_unread(NULL), m_unrd_size(0), m_unrd_cur(0),
m_cbk(NULL), m_cdata(NULL),
m_connected(FALSE), m_notify_state(FALSE), m_id(-1),
bool wxSocketBase::Close()
{
- if (m_socket)
+ if (m_socket)
{
if (m_notify_state == TRUE)
Notify(FALSE);
// --------------------------------------------------------------
// wxSocketBase base IO function
// --------------------------------------------------------------
+class _wxSocketInternalTimer: public wxTimer {
+ public:
+ int *m_state;
+ unsigned long m_new_val;
+
+ void Notify()
+ {
+ *m_state = m_new_val; // Change the value
+ }
+};
int wxSocketBase::DeferRead(char *buffer, size_t nbytes)
{
GSocketEventFlags old_event_flags;
bool old_notify_state;
+ // Timer for timeout
+ _wxSocketInternalTimer timer;
wxASSERT(m_defering == NO_DEFER);
+ // Set the defering mode to READ.
m_defering = DEFER_READ;
+ // Save the old state.
old_event_flags = NeededReq();
old_notify_state = m_notify_state;
-
+
+ // Set the new async flag.
SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
Notify(TRUE);
+ // Set the current buffer.
m_defer_buffer = buffer;
m_defer_nbytes = nbytes;
+ m_defer_timer = &timer;
+
+ timer.m_state = (int *)&m_defer_buffer;
+ timer.m_new_val = 0;
+
+ timer.Start(m_timeout * 1000, FALSE);
+
+ // Wait for buffer completion.
while (m_defer_buffer != NULL)
wxYield();
+ timer.Stop();
+
+ // Restore the old state.
Notify(old_notify_state);
SetNotify(old_event_flags);
+ // Disable defering mode.
m_defering = NO_DEFER;
+ m_defer_timer = NULL;
+ // Return the number of bytes read from the socket.
return nbytes-m_defer_nbytes;
}
{
GSocketEventFlags old_event_flags;
bool old_notify_state;
+ // Timer for timeout
+ _wxSocketInternalTimer timer;
wxASSERT(m_defering == NO_DEFER);
m_defering = DEFER_WRITE;
+ // Save the old state
old_event_flags = NeededReq();
old_notify_state = m_notify_state;
SetNotify(GSOCK_OUTPUT_FLAG | GSOCK_LOST_FLAG);
Notify(TRUE);
+ // Set the current buffer
m_defer_buffer = (char *)buffer;
m_defer_nbytes = nbytes;
+
+ // Start timer
+ timer.m_state = (int *)&m_defer_buffer;
+ timer.m_new_val = 0;
+
+ m_defer_timer = &timer;
+ timer.Start(m_timeout * 1000, FALSE);
+
while (m_defer_buffer != NULL)
wxYield();
+ // Stop timer
+ m_defer_timer = NULL;
+ timer.Stop();
+
+ // Restore the old state
Notify(old_notify_state);
SetNotify(old_event_flags);
if (ret < 0)
m_defer_nbytes++;
+ // If we are waiting for all bytes to be acquired, keep the defering modei
+ // enabled.
if ((m_flags & WAITALL) == 0 || m_defer_nbytes == 0 || ret < 0) {
m_defer_buffer = NULL;
Notify(FALSE);
- } else
+ } else {
m_defer_buffer += ret;
+ m_defer_timer->Start(m_timeout * 1000, FALSE);
+ }
}
// ---------------------------------------------------------------------
SaveState();
SetFlags(NOWAIT | SPEED);
- while (recv_size == MAX_BUFSIZE)
+ while (recv_size == MAX_BUFSIZE)
{
recv_size = Read(my_data, MAX_BUFSIZE).LastCount();
}
// --------- wxSocketBase wait functions ------------------------
// --------------------------------------------------------------
-class _wxSocketInternalTimer: public wxTimer {
- public:
- int *m_state;
-
- void Notify()
- {
- *m_state = GSOCK_MAX_EVENT; // Just to say it's a timeout.
- }
-};
-
static void wx_socket_wait(GSocket *socket, GSocketEvent event, char *cdata)
{
int *state = (int *)cdata;
bool wxSocketBase::_Wait(long seconds, long milliseconds, int type)
{
bool old_notify_state = m_notify_state;
- int state = 0;
+ int state = -1;
_wxSocketInternalTimer timer;
if (!m_connected || !m_socket)
return FALSE;
+ // Set the variable to change
timer.m_state = &state;
+ timer.m_new_val = GSOCK_MAX_EVENT;
+ // Disable the previous handler
Notify(FALSE);
+ // Set the timeout
timer.Start(seconds * 1000 + milliseconds, TRUE);
- GSocket_SetFallback(m_socket, type, wx_socket_wait, (char *)&state);
+ GSocket_SetCallback(m_socket, type, wx_socket_wait, (char *)&state);
- while (state == 0)
+ while (state == -1)
wxYield();
- GSocket_UnsetFallback(m_socket, type);
+ GSocket_UnsetCallback(m_socket, type);
timer.Stop();
+ // Notify will restore automatically the old GSocket flags
Notify(old_notify_state);
return (state != GSOCK_MAX_EVENT);
GSocketEventFlags wxSocketBase::EventToNotify(GSocketEvent evt)
{
- switch (evt)
+ switch (evt)
{
case GSOCK_INPUT:
return GSOCK_INPUT_FLAG;
return GSOCK_OUTPUT_FLAG;
case GSOCK_CONNECTION:
return GSOCK_CONNECTION_FLAG;
- case GSOCK_LOST_FLAG:
+ case GSOCK_LOST:
return GSOCK_LOST_FLAG;
default:
return 0;
if (!m_socket)
return;
- GSocket_UnsetFallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
+ GSocket_UnsetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG);
if (!notify)
return;
- GSocket_SetFallback(m_socket, m_neededreq, wx_socket_fallback, (char *)this);
+ GSocket_SetCallback(m_socket, m_neededreq, wx_socket_fallback, (char *)this);
}
void wxSocketBase::OnRequest(GSocketEvent req_evt)
m_unread = tmp;
}
-
+
m_unrd_size += size;
memcpy(m_unread, buffer, size);
// Update the flags of m_socket.
SetFlags(m_flags);
GSocket_SetPeer(m_socket, addr_man.GetAddress());
- if (GSocket_Connect(m_socket, GSOCK_STREAMED) != GSOCK_NOERROR) {
+ if (GSocket_Connect(m_socket, GSOCK_STREAMED) != GSOCK_NOERROR)
return FALSE;
- }
// Enables bg events.
// ------------------
void wxSocketClient::OnRequest(GSocketEvent evt)
{
- if (evt == GSOCK_CONNECTION)
+ if (evt == GSOCK_CONNECTION)
{
- if (m_connected)
+ if (m_connected)
{
m_neededreq &= ~GSOCK_CONNECTION_FLAG;
return;
event->m_socket = m_socket;
}
+// --------------------------------------------------------------------------
+// wxSocketModule
+// --------------------------------------------------------------------------
+class WXDLLEXPORT wxSocketModule: public wxModule {
+ DECLARE_DYNAMIC_CLASS(wxSocketModule)
+ public:
+ bool OnInit() {
+ return GSocket_Init();
+ }
+ void OnExit() {
+ GSocket_Cleanup();
+ }
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule)
+
#endif
// wxUSE_SOCKETS