]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/sockunix.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/sockunix.cpp
3 // Purpose: wxSocketImpl implementation for Unix systems
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia, David Elliott,
8 // Copyright: (c) 1997 Guilhem Lavaux
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/wxprec.h"
18 #include "wx/private/fd.h"
19 #include "wx/private/socket.h"
20 #include "wx/unix/private/sockunix.h"
24 #if defined(__WATCOMC__)
28 #include <sys/types.h>
30 #ifdef HAVE_SYS_SELECT_H
31 # include <sys/select.h>
35 #include <sys/select.h>
41 # define WX_SOCKLEN_T unsigned int
45 # define WX_SOCKLEN_T socklen_t
47 # elif defined(__WXMAC__)
48 # define WX_SOCKLEN_T socklen_t
50 # define WX_SOCKLEN_T int
54 #endif /* SOCKLEN_T */
57 #define SOCKOPTLEN_T WX_SOCKLEN_T
60 // UnixWare reportedly needs this for FIONBIO definition
62 #include <sys/filio.h>
65 // ============================================================================
66 // wxSocketImpl implementation
67 // ============================================================================
70 wxSocketImpl
*wxSocketImpl::Create(wxSocketBase
& wxsocket
)
72 return new wxSocketImplUnix(wxsocket
);
76 wxSocketError
wxSocketImplUnix::GetLastError() const
81 return wxSOCKET_NOERROR
;
84 return wxSOCKET_INVSOCK
;
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...
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)
95 #if EWOULDBLOCK != EAGAIN
100 return wxSOCKET_WOULDBLOCK
;
103 return wxSOCKET_IOERR
;
107 void wxSocketImplUnix::DoEnableEvents(bool flag
)
109 wxSocketManager
* const manager
= wxSocketManager::Get();
112 manager
->Install_Callback(this, wxSOCKET_INPUT
);
113 manager
->Install_Callback(this, wxSOCKET_OUTPUT
);
117 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
118 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
123 void wxSocketImplUnix::OnStateChange(wxSocketNotify event
)
125 NotifyOnStateChange(event
);
127 if ( event
== wxSOCKET_LOST
)
131 void wxSocketImplUnix::OnReadWaiting()
135 if (m_fd
== INVALID_SOCKET
)
140 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
);
144 OnStateChange(wxSOCKET_INPUT
);
148 if (m_server
&& m_stream
)
150 OnStateChange(wxSOCKET_CONNECTION
);
156 /* graceful shutdown */
157 OnStateChange(wxSOCKET_LOST
);
161 /* Empty datagram received */
162 OnStateChange(wxSOCKET_INPUT
);
167 /* Do not throw a lost event in cases where the socket isn't really lost */
168 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
170 OnStateChange(wxSOCKET_INPUT
);
174 OnStateChange(wxSOCKET_LOST
);
180 void wxSocketImplUnix::OnWriteWaiting()
182 if (m_establishing
&& !m_server
)
185 SOCKOPTLEN_T len
= sizeof(error
);
187 m_establishing
= false;
189 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
193 OnStateChange(wxSOCKET_LOST
);
197 OnStateChange(wxSOCKET_CONNECTION
);
198 /* We have to fire this event by hand because CONNECTION (for clients)
199 * and OUTPUT are internally the same and we just disabled CONNECTION
200 * events with the above macro.
202 OnStateChange(wxSOCKET_OUTPUT
);
207 OnStateChange(wxSOCKET_OUTPUT
);
211 void wxSocketImplUnix::OnExceptionWaiting()
213 wxFAIL_MSG( "not supposed to be called" );
216 #endif /* wxUSE_SOCKETS */