Whitespace fixes
[amule.git] / src / libs / ec / cpp / ECMuleSocket.cpp
blob3f44e4366134e58c92ed60200b14a4d3a086baab
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.
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 {
35 public:
36 CECMuleSocketHandler() {};
38 private:
39 void SocketHandler(wxSocketEvent& event);
41 DECLARE_EVENT_TABLE()
44 BEGIN_EVENT_TABLE(CECMuleSocketHandler, wxEvtHandler)
45 EVT_SOCKET(EC_SOCKET_HANDLER, CECMuleSocketHandler::SocketHandler)
46 END_EVENT_TABLE()
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()) {
54 case wxSOCKET_LOST:
55 socket->OnLost();
56 break;
57 case wxSOCKET_INPUT:
58 socket->OnInput();
59 break;
60 case wxSOCKET_OUTPUT:
61 socket->OnOutput();
62 break;
63 case wxSOCKET_CONNECTION:
64 socket->OnConnect();
65 break;
67 default:
68 // Nothing should arrive here...
69 wxFAIL;
70 break;
74 static CECMuleSocketHandler g_ECSocketHandler;
77 // CECMuleSocket API - User interface functions
80 CECMuleSocket::CECMuleSocket(bool use_events)
82 CECSocket(use_events),
83 wxSocketClient()
85 if ( use_events ) {
86 SetEventHandler(g_ECSocketHandler, EC_SOCKET_HANDLER);
87 SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG |
88 wxSOCKET_OUTPUT_FLAG | wxSOCKET_LOST_FLAG);
89 Notify(true);
90 SetFlags(wxSOCKET_NOWAIT);
91 } else {
92 SetFlags(wxSOCKET_WAITALL | wxSOCKET_BLOCK);
93 Notify(false);
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) {
108 wxIPV4address addr;
109 addr.Hostname(Uint32toStringIP(ip));
110 addr.Service(port);
111 return wxSocketClient::Connect(addr, wait);
114 int CECMuleSocket::InternalGetLastError() {
115 switch(LastError()) {
116 case wxSOCKET_NOERROR:
117 return EC_ERROR_NOERROR;
118 case wxSOCKET_INVOP:
119 return EC_ERROR_INVOP;
120 case wxSOCKET_IOERR:
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;
136 default:
137 return EC_ERROR_UNKNOWN;