1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
6 //
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
9 // respective authors.
10 //
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.
15 //
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.
20 //
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
24 //
25 
26 #include "ECMuleSocket.h"
27 
28 #include "../../../amuleIPV4Address.h"
29 #include "../../../NetworkFunctions.h"
30 
31 #ifdef ASIO_SOCKETS
32 #include <boost/system/error_code.hpp>
33 #else
34 
35 //-------------------- CECSocketHandler --------------------
36 
37 #define	EC_SOCKET_HANDLER	(wxID_HIGHEST + 644)
38 
39 class CECMuleSocketHandler: public wxEvtHandler {
40  public:
CECMuleSocketHandler()41         CECMuleSocketHandler() {};
42 
43  private:
44         void SocketHandler(wxSocketEvent& event);
45 
46         DECLARE_EVENT_TABLE()
47 };
48 
BEGIN_EVENT_TABLE(CECMuleSocketHandler,wxEvtHandler)49 BEGIN_EVENT_TABLE(CECMuleSocketHandler, wxEvtHandler)
50         EVT_SOCKET(EC_SOCKET_HANDLER, CECMuleSocketHandler::SocketHandler)
51 END_EVENT_TABLE()
52 
53 void CECMuleSocketHandler::SocketHandler(wxSocketEvent& event)
54 {
55         CECSocket *socket = dynamic_cast<CECSocket *>(event.GetSocket());
56         wxCHECK_RET(socket, wxT("Socket event with a NULL socket!"));
57 
58         switch(event.GetSocketEvent()) {
59         case wxSOCKET_LOST:
60             socket->OnLost();
61             break;
62         case wxSOCKET_INPUT:
63             socket->OnInput();
64             break;
65         case wxSOCKET_OUTPUT:
66             socket->OnOutput();
67             break;
68         case wxSOCKET_CONNECTION:
69             socket->OnConnect();
70             break;
71 
72         default:
73             // Nothing should arrive here...
74             wxFAIL;
75             break;
76         }
77 }
78 
79 static CECMuleSocketHandler	g_ECSocketHandler;
80 
81 #endif /* ASIO_SOCKETS */
82 
83 //
84 // CECMuleSocket API - User interface functions
85 //
86 
CECMuleSocket(bool use_events)87 CECMuleSocket::CECMuleSocket(bool use_events)
88 :
89 CECSocket(use_events)
90 {
91 #ifdef ASIO_SOCKETS
92 	Notify(use_events);
93 #else
94 	if ( use_events ) {
95 		SetEventHandler(g_ECSocketHandler, EC_SOCKET_HANDLER);
96 		SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG |
97 			  wxSOCKET_OUTPUT_FLAG | wxSOCKET_LOST_FLAG);
98 		Notify(true);
99 		SetFlags(wxSOCKET_NOWAIT);
100 	} else {
101 		SetFlags(wxSOCKET_WAITALL | wxSOCKET_BLOCK);
102 		Notify(false);
103 	}
104 #endif
105 }
106 
~CECMuleSocket()107 CECMuleSocket::~CECMuleSocket()
108 {
109 }
110 
ConnectSocket(amuleIPV4Address & address)111 bool CECMuleSocket::ConnectSocket(amuleIPV4Address& address)
112 {
113 	return CECSocket::ConnectSocket(StringIPtoUint32(address.IPAddress()),address.Service());
114 }
115 
116 
InternalConnect(uint32_t ip,uint16_t port,bool wait)117 bool CECMuleSocket::InternalConnect(uint32_t ip, uint16_t port, bool wait) {
118 	amuleIPV4Address addr;
119 	addr.Hostname(Uint32toStringIP(ip));
120 	addr.Service(port);
121 	return CLibSocket::Connect(addr, wait);
122 }
123 
InternalGetLastError()124 int CECMuleSocket::InternalGetLastError()
125 {
126 	switch (LastError()) {
127 #ifdef ASIO_SOCKETS
128 		case boost::system::errc::success:
129 			return EC_ERROR_NOERROR;
130 		case boost::system::errc::address_family_not_supported:
131 		case boost::system::errc::address_in_use:
132 		case boost::system::errc::address_not_available:
133 		case boost::system::errc::bad_address:
134 		case boost::system::errc::invalid_argument:
135 			return EC_ERROR_INVADDR;
136 		case boost::system::errc::already_connected:
137 		case boost::system::errc::connection_already_in_progress:
138 		case boost::system::errc::not_connected:
139 			return EC_ERROR_INVOP;
140 		case boost::system::errc::connection_aborted:
141 		case boost::system::errc::connection_reset:
142 		case boost::system::errc::io_error:
143 		case boost::system::errc::network_down:
144 		case boost::system::errc::network_reset:
145 		case boost::system::errc::network_unreachable:
146 			return EC_ERROR_IOERR;
147 		case boost::system::errc::connection_refused:
148 		case boost::system::errc::host_unreachable:
149 			return EC_ERROR_NOHOST;
150 		case boost::system::errc::not_a_socket:
151 			return EC_ERROR_INVSOCK;
152 		case boost::system::errc::not_enough_memory:
153 			return EC_ERROR_MEMERR;
154 		case boost::system::errc::operation_would_block:
155 			return EC_ERROR_WOULDBLOCK;
156 		case boost::system::errc::timed_out:
157 			return EC_ERROR_TIMEDOUT;
158 #else
159 		case wxSOCKET_NOERROR:
160 			return EC_ERROR_NOERROR;
161 		case wxSOCKET_INVOP:
162 			return EC_ERROR_INVOP;
163 		case wxSOCKET_IOERR:
164 			return EC_ERROR_IOERR;
165 		case wxSOCKET_INVADDR:
166 			return EC_ERROR_INVADDR;
167 		case wxSOCKET_INVSOCK:
168 			return EC_ERROR_INVSOCK;
169 		case wxSOCKET_NOHOST:
170 			return EC_ERROR_NOHOST;
171 		case wxSOCKET_INVPORT:
172 			return EC_ERROR_INVPORT;
173 		case wxSOCKET_WOULDBLOCK:
174 			return EC_ERROR_WOULDBLOCK;
175 		case wxSOCKET_TIMEDOUT:
176 			return EC_ERROR_TIMEDOUT;
177 		case wxSOCKET_MEMERR:
178 			return EC_ERROR_MEMERR;
179 #endif
180 		default:
181 			return EC_ERROR_UNKNOWN;
182 	}
183 }
184