- switch (req->type) {
- case EVT_READ:
- case EVT_PEEK:
- ret = recv(m_fd, req->buffer, req->size,
- (req->type == EVT_PEEK) ? MSG_PEEK : 0);
- if (ret < 0) {
- req->error = errno;
- req->done = TRUE;
- break;
- }
- len = ret;
- if ((len < req->size) && (m_flags & WAITALL)) {
- req->size -= len;
- req->nbytes += len;
- req->buffer += len;
- req->auto_wakeup->Start(m_timeout*1000, TRUE);
- req_list[req_flag].Insert(req);
- break;
- }
- req->done = TRUE;
- req->nbytes += len;
-#ifndef __WXGTK__
- DestroyCallbacks();
-#endif
- break;
- case EVT_WRITE:
- ret = send(m_fd, req->buffer, req->size, 0);
- if (ret < 0) {
- req->error = errno;
- req->done = TRUE;
- break;
- }
- len = ret;
- if ((len < req->size) && (m_flags & WAITALL)) {
- req->size -= len;
- req->nbytes += len;
- req->buffer += len;
- req->auto_wakeup->Start(m_timeout*1000, TRUE);
- req_list[req_flag].Insert(req);
- break;
- }
- req->done = TRUE;
- req->nbytes += len;
-#ifndef __WXGTK__
- DestroyCallbacks();
-#endif
- break;
- default:
- return FALSE;
- }
- return TRUE;
-}
-
-void wxSocketBase::WantSpeedBuffer(char *buffer, size_t nbytes,
- wxRequestEvent evt)
-{
- int ret;
-
- switch (evt) {
- case EVT_PEEK:
- case EVT_READ:
- ret = read(m_fd, buffer, nbytes);
- break;
- case EVT_WRITE:
- ret = write(m_fd, buffer, nbytes);
- break;
- }
- if (ret < 0) {
- m_lcount = 0;
- m_error = errno;
- } else
- m_lcount = ret;
-}
-
-void wxSocketBase::WantBuffer(char *buffer, size_t nbytes,
- wxRequestEvent evt)
-{
- bool buf_timed_out;
-
- if (m_fd == INVALID_SOCKET || !m_handler || !m_connected)
- return;
-
- if (m_flags & SPEED) {
- WantSpeedBuffer(buffer, nbytes, evt);
- return;
- }
-
- SockRequest *buf = new SockRequest;
- wxSockWakeUp s_wake(NULL, (int *)&buf_timed_out, (int)TRUE);
-
- m_wantbuf++;
- req_list[evt].Append(buf);
-
- SaveState();
- SetNotify(REQ_LOST | EventToNotify(evt));
- SetupCallbacks();
- buf->buffer = buffer;
- buf->size = nbytes;
- buf->done = FALSE;
- buf->type = evt;
- buf->nbytes = 0;
- buf->auto_wakeup = &s_wake;
- buf->error = 0;
- buf_timed_out = FALSE;
-
- s_wake.Start(m_timeout*1000, TRUE);
- if (m_flags & NOWAIT) {
- DoRequests(evt);
- } else {
- while (!buf->done && !buf_timed_out)
- PROCESS_EVENTS();
- }
- m_wantbuf--;
- m_lcount = buf->nbytes;
- if (buf_timed_out)
- m_error = ETIMEDOUT;
- else
- m_error = buf->error;
-
- delete buf;
- RestoreState();
-}
-
-// --------------------------------------------------------------
-// wxSocketServer ///////////////////////////////////////////////
-// --------------------------------------------------------------
-
-// --------------------------------------------------------------
-// --------- wxSocketServer CONSTRUCTOR -------------------------
-// --------------------------------------------------------------
-wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
- wxSockFlags flags) :
- wxSocketBase(flags, SOCK_SERVER)
-{
- m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0);
-
- 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;
- }
-}
-
-// --------------------------------------------------------------
-// --------- wxSocketServer Accept ------------------------------
-// --------------------------------------------------------------
-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;
-
- return TRUE;
-}
-
-wxSocketBase *wxSocketServer::Accept()
-{
- wxSocketBase* sock = new wxSocketBase();
-
- sock->SetFlags((wxSockFlags)m_flags);
-
- if (!AcceptWith(*sock))
- return NULL;
-
- if (m_handler)
- m_handler->Register(sock);
-
- return sock;
-}
-
-// --------------------------------------------------------------
-// --------- wxSocketServer callbacks ---------------------------
-// --------------------------------------------------------------
-void wxSocketServer::OnRequest(wxRequestEvent evt)
-{
- if (evt == EVT_ACCEPT) {
- OldOnNotify(EVT_ACCEPT);
- }
-}
-
-// --------------------------------------------------------------
-// wxSocketClient ///////////////////////////////////////////////
-// --------------------------------------------------------------