| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/unix/sockunix.cpp |
| 3 | // Purpose: wxSocketImpl implementation for Unix systems |
| 4 | // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia, David Elliott, |
| 5 | // Vadim Zeitlin |
| 6 | // Created: April 1997 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1997 Guilhem Lavaux |
| 9 | // (c) 2008 Vadim Zeitlin |
| 10 | // Licence: wxWindows licence |
| 11 | ///////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | |
| 14 | #include "wx/wxprec.h" |
| 15 | |
| 16 | #if wxUSE_SOCKETS |
| 17 | |
| 18 | #include "wx/private/fd.h" |
| 19 | #include "wx/private/socket.h" |
| 20 | #include "wx/unix/private/sockunix.h" |
| 21 | |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #if defined(__WATCOMC__) |
| 25 | #include <nerrno.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #ifdef HAVE_SYS_SELECT_H |
| 31 | # include <sys/select.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifdef __EMX__ |
| 35 | #include <sys/select.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifndef WX_SOCKLEN_T |
| 39 | |
| 40 | #ifdef VMS |
| 41 | # define WX_SOCKLEN_T unsigned int |
| 42 | #else |
| 43 | # ifdef __GLIBC__ |
| 44 | # if __GLIBC__ == 2 |
| 45 | # define WX_SOCKLEN_T socklen_t |
| 46 | # endif |
| 47 | # elif defined(__WXMAC__) |
| 48 | # define WX_SOCKLEN_T socklen_t |
| 49 | # else |
| 50 | # define WX_SOCKLEN_T int |
| 51 | # endif |
| 52 | #endif |
| 53 | |
| 54 | #endif /* SOCKLEN_T */ |
| 55 | |
| 56 | #ifndef SOCKOPTLEN_T |
| 57 | #define SOCKOPTLEN_T WX_SOCKLEN_T |
| 58 | #endif |
| 59 | |
| 60 | // UnixWare reportedly needs this for FIONBIO definition |
| 61 | #ifdef __UNIXWARE__ |
| 62 | #include <sys/filio.h> |
| 63 | #endif |
| 64 | |
| 65 | // ============================================================================ |
| 66 | // wxSocketImpl implementation |
| 67 | // ============================================================================ |
| 68 | |
| 69 | wxSocketError wxSocketImplUnix::GetLastError() const |
| 70 | { |
| 71 | switch ( errno ) |
| 72 | { |
| 73 | case 0: |
| 74 | return wxSOCKET_NOERROR; |
| 75 | |
| 76 | case ENOTSOCK: |
| 77 | return wxSOCKET_INVSOCK; |
| 78 | |
| 79 | // unfortunately EAGAIN only has the "would block" meaning for read(), |
| 80 | // not for connect() for which it means something rather different but |
| 81 | // we can't distinguish between these two situations currently... |
| 82 | // |
| 83 | // also notice that EWOULDBLOCK can be different from EAGAIN on some |
| 84 | // systems (HP-UX being the only known example) while it's defined as |
| 85 | // EAGAIN on most others (e.g. Linux) |
| 86 | case EAGAIN: |
| 87 | #ifdef EWOULDBLOCK |
| 88 | #if EWOULDBLOCK != EAGAIN |
| 89 | case EWOULDBLOCK: |
| 90 | #endif |
| 91 | #endif // EWOULDBLOCK |
| 92 | case EINPROGRESS: |
| 93 | return wxSOCKET_WOULDBLOCK; |
| 94 | |
| 95 | default: |
| 96 | return wxSOCKET_IOERR; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void wxSocketImplUnix::DoEnableEvents(int flags, bool enable) |
| 101 | { |
| 102 | wxSocketManager * const manager = wxSocketManager::Get(); |
| 103 | if (!manager) |
| 104 | return; |
| 105 | |
| 106 | if ( enable ) |
| 107 | { |
| 108 | if ( flags & wxSOCKET_INPUT_FLAG ) |
| 109 | manager->Install_Callback(this, wxSOCKET_INPUT); |
| 110 | if ( flags & wxSOCKET_OUTPUT_FLAG ) |
| 111 | manager->Install_Callback(this, wxSOCKET_OUTPUT); |
| 112 | } |
| 113 | else // off |
| 114 | { |
| 115 | if ( flags & wxSOCKET_INPUT_FLAG ) |
| 116 | manager->Uninstall_Callback(this, wxSOCKET_INPUT); |
| 117 | if ( flags & wxSOCKET_OUTPUT_FLAG ) |
| 118 | manager->Uninstall_Callback(this, wxSOCKET_OUTPUT); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | int wxSocketImplUnix::CheckForInput() |
| 123 | { |
| 124 | char c; |
| 125 | int rc; |
| 126 | do |
| 127 | { |
| 128 | rc = recv(m_fd, &c, 1, MSG_PEEK); |
| 129 | } while ( rc == -1 && errno == EINTR ); |
| 130 | |
| 131 | return rc; |
| 132 | } |
| 133 | |
| 134 | void wxSocketImplUnix::OnStateChange(wxSocketNotify event) |
| 135 | { |
| 136 | NotifyOnStateChange(event); |
| 137 | |
| 138 | if ( event == wxSOCKET_LOST ) |
| 139 | Shutdown(); |
| 140 | } |
| 141 | |
| 142 | void wxSocketImplUnix::OnReadWaiting() |
| 143 | { |
| 144 | wxASSERT_MSG( m_fd != INVALID_SOCKET, "invalid socket ready for reading?" ); |
| 145 | |
| 146 | // we need to disable the read notifications until we read all the data |
| 147 | // already available for the socket, otherwise we're going to keep getting |
| 148 | // them continuously which is worse than inefficient: as IO notifications |
| 149 | // have higher priority than idle events in e.g. GTK+, our pending events |
| 150 | // whose handlers typically call Read() which would consume the data and so |
| 151 | // stop the notifications flood would never be dispatched at all if the |
| 152 | // notifications were not disabled |
| 153 | DisableEvents(wxSOCKET_INPUT_FLAG); |
| 154 | |
| 155 | |
| 156 | // find out what are we going to notify about exactly |
| 157 | wxSocketNotify notify; |
| 158 | |
| 159 | // TCP listening sockets become ready for reading when there is a pending |
| 160 | // connection |
| 161 | if ( m_server && m_stream ) |
| 162 | { |
| 163 | notify = wxSOCKET_CONNECTION; |
| 164 | } |
| 165 | else // check if there is really any input available |
| 166 | { |
| 167 | switch ( CheckForInput() ) |
| 168 | { |
| 169 | case 1: |
| 170 | notify = wxSOCKET_INPUT; |
| 171 | break; |
| 172 | |
| 173 | case 0: |
| 174 | // reading 0 bytes for a TCP socket means that the connection |
| 175 | // was closed by peer but for UDP it just means that we got an |
| 176 | // empty datagram |
| 177 | notify = m_stream ? wxSOCKET_LOST : wxSOCKET_INPUT; |
| 178 | break; |
| 179 | |
| 180 | default: |
| 181 | wxFAIL_MSG( "unexpected CheckForInput() return value" ); |
| 182 | // fall through |
| 183 | |
| 184 | case -1: |
| 185 | if ( GetLastError() == wxSOCKET_WOULDBLOCK ) |
| 186 | { |
| 187 | // just a spurious wake up |
| 188 | EnableEvents(wxSOCKET_INPUT_FLAG); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | notify = wxSOCKET_LOST; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | OnStateChange(notify); |
| 197 | } |
| 198 | |
| 199 | void wxSocketImplUnix::OnWriteWaiting() |
| 200 | { |
| 201 | wxASSERT_MSG( m_fd != INVALID_SOCKET, "invalid socket ready for writing?" ); |
| 202 | |
| 203 | // see comment in the beginning of OnReadWaiting() above |
| 204 | DisableEvents(wxSOCKET_OUTPUT_FLAG); |
| 205 | |
| 206 | |
| 207 | // check whether this is a notification for the completion of a |
| 208 | // non-blocking connect() |
| 209 | if ( m_establishing && !m_server ) |
| 210 | { |
| 211 | m_establishing = false; |
| 212 | |
| 213 | // check whether we connected successfully |
| 214 | int error; |
| 215 | SOCKOPTLEN_T len = sizeof(error); |
| 216 | |
| 217 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
| 218 | |
| 219 | if ( error ) |
| 220 | { |
| 221 | OnStateChange(wxSOCKET_LOST); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | OnStateChange(wxSOCKET_CONNECTION); |
| 226 | } |
| 227 | |
| 228 | OnStateChange(wxSOCKET_OUTPUT); |
| 229 | } |
| 230 | |
| 231 | void wxSocketImplUnix::OnExceptionWaiting() |
| 232 | { |
| 233 | // when using epoll() this is called when an error occurred on the socket |
| 234 | // so close it if it hadn't been done yet -- what else can we do? |
| 235 | // |
| 236 | // notice that we shouldn't be called at all when using select() as we |
| 237 | // don't use wxFDIO_EXCEPTION when registering the socket for monitoring |
| 238 | // and this is good because select() would call this for any OOB data which |
| 239 | // is not necessarily an error |
| 240 | if ( m_fd != INVALID_SOCKET ) |
| 241 | OnStateChange(wxSOCKET_LOST); |
| 242 | } |
| 243 | |
| 244 | #endif /* wxUSE_SOCKETS */ |