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 "../../../amuleIPV4Address.h"
29 #include "../../../NetworkFunctions.h"
32 #include <boost/system/error_code.hpp>
35 //-------------------- CECSocketHandler --------------------
37 #define EC_SOCKET_HANDLER (wxID_HIGHEST + 644)
39 class CECMuleSocketHandler
: public wxEvtHandler
{
41 CECMuleSocketHandler() {};
44 void SocketHandler(wxSocketEvent
& event
);
49 BEGIN_EVENT_TABLE(CECMuleSocketHandler
, wxEvtHandler
)
50 EVT_SOCKET(EC_SOCKET_HANDLER
, CECMuleSocketHandler::SocketHandler
)
53 void CECMuleSocketHandler::SocketHandler(wxSocketEvent
& event
)
55 CECSocket
*socket
= dynamic_cast<CECSocket
*>(event
.GetSocket());
56 wxCHECK_RET(socket
, wxT("Socket event with a NULL socket!"));
58 switch(event
.GetSocketEvent()) {
68 case wxSOCKET_CONNECTION
:
73 // Nothing should arrive here...
79 static CECMuleSocketHandler g_ECSocketHandler
;
82 // CECMuleSocket API - User interface functions
85 CECMuleSocket::CECMuleSocket(bool use_events
)
90 SetEventHandler(g_ECSocketHandler
, EC_SOCKET_HANDLER
);
91 SetNotify(wxSOCKET_CONNECTION_FLAG
| wxSOCKET_INPUT_FLAG
|
92 wxSOCKET_OUTPUT_FLAG
| wxSOCKET_LOST_FLAG
);
94 SetFlags(wxSOCKET_NOWAIT
);
96 SetFlags(wxSOCKET_WAITALL
| wxSOCKET_BLOCK
);
101 CECMuleSocket::~CECMuleSocket()
105 bool CECMuleSocket::ConnectSocket(amuleIPV4Address
& address
)
107 return CECSocket::ConnectSocket(StringIPtoUint32(address
.IPAddress()),address
.Service());
111 bool CECMuleSocket::InternalConnect(uint32_t ip
, uint16_t port
, bool wait
) {
112 amuleIPV4Address addr
;
113 addr
.Hostname(Uint32toStringIP(ip
));
115 return CLibSocket::Connect(addr
, wait
);
118 int CECMuleSocket::InternalGetLastError()
120 switch (LastError()) {
122 case boost::system::errc::success
:
123 return EC_ERROR_NOERROR
;
124 case boost::system::errc::address_family_not_supported
:
125 case boost::system::errc::address_in_use
:
126 case boost::system::errc::address_not_available
:
127 case boost::system::errc::bad_address
:
128 case boost::system::errc::invalid_argument
:
129 return EC_ERROR_INVADDR
;
130 case boost::system::errc::already_connected
:
131 case boost::system::errc::connection_already_in_progress
:
132 case boost::system::errc::not_connected
:
133 return EC_ERROR_INVOP
;
134 case boost::system::errc::connection_aborted
:
135 case boost::system::errc::connection_reset
:
136 case boost::system::errc::io_error
:
137 case boost::system::errc::network_down
:
138 case boost::system::errc::network_reset
:
139 case boost::system::errc::network_unreachable
:
140 return EC_ERROR_IOERR
;
141 case boost::system::errc::connection_refused
:
142 case boost::system::errc::host_unreachable
:
143 return EC_ERROR_NOHOST
;
144 case boost::system::errc::not_a_socket
:
145 return EC_ERROR_INVSOCK
;
146 case boost::system::errc::not_enough_memory
:
147 return EC_ERROR_MEMERR
;
148 case boost::system::errc::operation_would_block
:
149 return EC_ERROR_WOULDBLOCK
;
150 case boost::system::errc::timed_out
:
151 return EC_ERROR_TIMEDOUT
;
153 case wxSOCKET_NOERROR
:
154 return EC_ERROR_NOERROR
;
156 return EC_ERROR_INVOP
;
158 return EC_ERROR_IOERR
;
159 case wxSOCKET_INVADDR
:
160 return EC_ERROR_INVADDR
;
161 case wxSOCKET_INVSOCK
:
162 return EC_ERROR_INVSOCK
;
163 case wxSOCKET_NOHOST
:
164 return EC_ERROR_NOHOST
;
165 case wxSOCKET_INVPORT
:
166 return EC_ERROR_INVPORT
;
167 case wxSOCKET_WOULDBLOCK
:
168 return EC_ERROR_WOULDBLOCK
;
169 case wxSOCKET_TIMEDOUT
:
170 return EC_ERROR_TIMEDOUT
;
171 case wxSOCKET_MEMERR
:
172 return EC_ERROR_MEMERR
;
175 return EC_ERROR_UNKNOWN
;