2 // This file is part of the aMule Project.
4 // Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "ECMuleSocket.h"
28 #include "../../../NetworkFunctions.h"
30 //-------------------- CECSocketHandler --------------------
32 #define EC_SOCKET_HANDLER (wxID_HIGHEST + 644)
34 class CECMuleSocketHandler
: public wxEvtHandler
{
36 CECMuleSocketHandler() {};
39 void SocketHandler(wxSocketEvent
& event
);
44 BEGIN_EVENT_TABLE(CECMuleSocketHandler
, wxEvtHandler
)
45 EVT_SOCKET(EC_SOCKET_HANDLER
, CECMuleSocketHandler::SocketHandler
)
48 void CECMuleSocketHandler::SocketHandler(wxSocketEvent
& event
)
50 CECSocket
*socket
= dynamic_cast<CECSocket
*>(event
.GetSocket());
51 wxCHECK_RET(socket
, wxT("Socket event with a NULL socket!"));
53 switch(event
.GetSocketEvent()) {
63 case wxSOCKET_CONNECTION
:
68 // Nothing should arrive here...
74 static CECMuleSocketHandler g_ECSocketHandler
;
77 // CECMuleSocket API - User interface functions
80 CECMuleSocket::CECMuleSocket(bool use_events
)
82 CECSocket(use_events
),
86 SetEventHandler(g_ECSocketHandler
, EC_SOCKET_HANDLER
);
87 SetNotify(wxSOCKET_CONNECTION_FLAG
| wxSOCKET_INPUT_FLAG
|
88 wxSOCKET_OUTPUT_FLAG
| wxSOCKET_LOST_FLAG
);
90 SetFlags(wxSOCKET_NOWAIT
);
92 SetFlags(wxSOCKET_WAITALL
| wxSOCKET_BLOCK
);
97 CECMuleSocket::~CECMuleSocket()
101 bool CECMuleSocket::ConnectSocket(wxIPV4address
& address
)
103 return CECSocket::ConnectSocket(StringIPtoUint32(address
.IPAddress()),address
.Service());
107 bool CECMuleSocket::InternalConnect(uint32_t ip
, uint16_t port
, bool wait
) {
109 addr
.Hostname(Uint32toStringIP(ip
));
111 return wxSocketClient::Connect(addr
, wait
);
114 int CECMuleSocket::InternalGetLastError() {
115 switch(LastError()) {
116 case wxSOCKET_NOERROR
:
117 return EC_ERROR_NOERROR
;
119 return EC_ERROR_INVOP
;
121 return EC_ERROR_IOERR
;
122 case wxSOCKET_INVADDR
:
123 return EC_ERROR_INVADDR
;
124 case wxSOCKET_INVSOCK
:
125 return EC_ERROR_INVSOCK
;
126 case wxSOCKET_NOHOST
:
127 return EC_ERROR_NOHOST
;
128 case wxSOCKET_INVPORT
:
129 return EC_ERROR_INVPORT
;
130 case wxSOCKET_WOULDBLOCK
:
131 return EC_ERROR_WOULDBLOCK
;
132 case wxSOCKET_TIMEDOUT
:
133 return EC_ERROR_TIMEDOUT
;
134 case wxSOCKET_MEMERR
:
135 return EC_ERROR_MEMERR
;
137 return EC_ERROR_UNKNOWN
;