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