X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e22036dc390137b9926a5f87ea08399332da0b6d..520e470fdd0daef09c77938db642e4583933c90d:/src/common/socket.cpp diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 5ab0e09d96..b92ed0e948 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -9,10 +9,10 @@ // 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". @@ -31,6 +31,9 @@ #include #include +// Not enough OS behaviour defined for wxStubs +#ifndef __WXSTUBS__ + #include #include #include @@ -54,6 +57,10 @@ #include #include +#ifdef sun +#include +#endif + #endif // __UNIX__ #include @@ -92,6 +99,9 @@ #ifdef __WINDOWS__ #define close closesocket #define ioctl ioctlsocket +#ifdef errno +#undef errno +#endif #define errno WSAGetLastError() #ifdef EWOULDBLOCK #undef EWOULDBLOCK @@ -107,7 +117,7 @@ #endif #ifdef __WXMOTIF__ -#define wxAPP_CONTEXT wxTheApp->appContext +#define wxAPP_CONTEXT ((XtAppContext)wxTheApp->GetAppContext()) #endif #ifdef __WINDOWS__ @@ -180,7 +190,7 @@ public: wxSockWakeUp *auto_wakeup; wxSocketBase::wxRequestNotify type; }; - + ///////////////////////////////////////////////////////////////////////////// // Some internal define @@ -266,7 +276,7 @@ bool wxSocketBase::Close() while (node) { SockRequest *req = (SockRequest *)node->Data(); req->done = TRUE; - + n = node->Next(); delete node; node = n; @@ -325,7 +335,7 @@ wxSocketBase& wxSocketBase::ReadMsg(char* buffer, size_t nbytes) { SockMsg msg; size_t len, len2, sig; - + Read((char *)&msg, sizeof(msg)); if (m_lcount != sizeof(msg)) return *this; @@ -368,27 +378,27 @@ wxSocketBase& wxSocketBase::ReadMsg(char* buffer, size_t nbytes) 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; @@ -415,7 +425,7 @@ bool wxSocketBase::IsData() const 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); } // --------------------------------------------------------------------- @@ -429,7 +439,7 @@ void wxSocketBase::Discard() SaveState(); SetFlags((wxSockFlags)(NOWAIT | SPEED)); - + while (recv_size == MAX_BUFSIZE) { recv_size = Read(my_data, MAX_BUFSIZE).LastCount(); } @@ -451,12 +461,8 @@ bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const 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; @@ -470,11 +476,7 @@ bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const 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); @@ -507,7 +509,7 @@ void wxSocketBase::RestoreState() return; wxSockState *state = (wxSockState *)node->Data(); - + SetFlags(state->flags); m_neededreq = state->cbk_set; m_cbk = state->cbk; @@ -539,7 +541,7 @@ bool wxSocketBase::_Wait(long seconds, long microseconds, int type) if (seconds != -1) wakeup.Start((int)(seconds*1000 + (microseconds / 1000)), TRUE); - + m_waitflags = 0x80 | type; while (m_waitflags & 0x80) PROCESS_EVENTS(); @@ -1022,10 +1024,11 @@ void wxSocketBase::WantSpeedBuffer(char *buffer, size_t nbytes, 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) { @@ -1050,7 +1053,7 @@ 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); @@ -1099,17 +1102,17 @@ wxSocketServer::wxSocketServer(wxSockAddress& addr_man, 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; @@ -1122,24 +1125,24 @@ wxSocketServer::wxSocketServer(wxSockAddress& addr_man, 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; @@ -1201,21 +1204,21 @@ bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) ) 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?)... @@ -1223,7 +1226,7 @@ bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) ) flag = 1; setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int)); #endif - + struct sockaddr *remote; size_t len; @@ -1236,21 +1239,21 @@ bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) ) 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) @@ -1261,7 +1264,7 @@ void wxSocketClient::OnRequest(wxRequestEvent evt) return; } m_waitflags = 0x40; - m_connected = TRUE; + m_connected = TRUE; OldOnNotify(EVT_CONNECT); DestroyCallbacks(); return; @@ -1412,7 +1415,7 @@ void wxSocketHandler::UnRegister(wxSocketBase* sock) for (node = socks->First(); node; node = node->Next()) { wxSocketBase* s = (wxSocketBase*)node->Data(); - + if (s == sock) { delete node; sock->DestroyCallbacks(); @@ -1537,3 +1540,6 @@ HWND wxSocketHandler::GetHWND() const } #endif + +#endif + // __WXSTUBS__