Remove do-nothing command and add warning about it
[amule.git] / src / MuleUDPSocket.h
blob1740c7a55a1fd2515ef1840b93c4c0badd8852d7
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2005-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #ifndef MULEUDPSOCKET_H
26 #define MULEUDPSOCKET_H
29 #include "Types.h" // Needed for uint16 and uint32
30 #include "ThrottledSocket.h" // Needed for ThrottledControlSocket
31 #include "amuleIPV4Address.h" // Needed for amuleIPV4Address
33 #include <wx/thread.h> // Needed for wxMutex
35 class CEncryptedDatagramSocket;
36 class CProxyData;
37 class CPacket;
39 /***
40 * This class provides a UBT governed UDP-socket.
42 * The CMuleUDPSocket are created with the NOWAIT option and
43 * handle both INPUT and OUTPUT events.
45 * The following additional features are provided compared to CDatagramSocketProxy:
46 * - Goverened by the UBT.
47 * - Automatic sending/receiving of packets.
48 * - Fallover recovery for when a socket becomes invalid (error 4).
50 * @see ThrottledControlSocket
51 * @see CEncryptedDatagramSocket
53 class CMuleUDPSocket : public ThrottledControlSocket
56 public:
57 /**
58 * Opens a UDP socket at the specified address.
60 * @param name Name used when logging events.
61 * @param id The ID used for events.
62 * @param address The address where the socket will listen.
63 * @param ProxyData ProxyData assosiated with the socket.
65 CMuleUDPSocket(const wxString& name, int id, const amuleIPV4Address& address, const CProxyData* ProxyData = NULL);
67 /**
68 * Destructor, safely closes the socket if opened.
70 virtual ~CMuleUDPSocket();
73 /**
74 * Opens the socket.
76 * The socket is bound to the address specified in
77 * the constructor.
79 void Open();
81 /**
82 * Closes the socket.
84 * The socket can be reopened by calling Open. Closing a
85 * already closed socket is an illegal operation.
87 void Close();
90 /** This function is called by aMule when the socket may send. */
91 virtual void OnSend(int errorCode);
92 /** This function is called by aMule when there are data to be received. */
93 virtual void OnReceive(int errorCode);
94 /** This function is called by aMule when there is an error while receiving. */
95 virtual void OnReceiveError(int errorCode, uint32 ip, uint16 port);
96 /** This function is called when the socket is lost (see comments in func.) */
97 virtual void OnDisconnected(int errorCode);
99 /**
100 * Queues a packet for sending.
102 * @param packet The packet to send.
103 * @param IP The target IP address.
104 * @param port The target port.
105 * @param bEncrypt If the packet must be encrypted
106 * @param port The target port.
107 * @param pachTargetClientHashORKadID The client hash or Kad ID
108 * @param bKad
109 * @param nReceiverVerifyKey
111 * Note that CMuleUDPSocket takes ownership of the packet.
113 void SendPacket(CPacket* packet, uint32 IP, uint16 port, bool bEncrypt, const uint8* pachTargetClientHashORKadID, bool bKad, uint32 nReceiverVerifyKey);
117 * Returns true if the socket is Ok, false otherwise.
119 * @see wxSocketBase::Ok
121 bool Ok();
123 /** Read buffer size */
124 static const unsigned UDP_BUFFER_SIZE = 16384;
126 protected:
128 * This function is called when a packet has been received.
130 * @param addr The address from where data was received.
131 * @param buffer The data that has been received.
132 * @param length The length of the data buffer.
134 virtual void OnPacketReceived(uint32 ip, uint16 port, uint8_t* buffer, size_t length) = 0;
137 /** See ThrottledControlSocket::SendControlData */
138 SocketSentBytes SendControlData(uint32 maxNumberOfBytesToSend, uint32 minFragSize);
140 private:
142 * Sends a packet to the specified address.
144 * @param buffer The data to be sent.
145 * @param length the length of the data buffer.
146 * @param ip The target ip address.
147 * @param port The target port.
149 bool SendTo(uint8_t *buffer, uint32_t length, uint32_t ip, uint16_t port);
153 * Creates a new socket.
155 * Calling this function when a socket already exists
156 * is an illegal operation.
158 void CreateSocket();
161 * Destroys the current socket, if any.
163 void DestroySocket();
166 //! Specifies if the last write attempt would cause the socket to block.
167 bool m_busy;
168 //! The name of the socket, used for debugging messages.
169 wxString m_name;
170 //! The socket-ID, used for event-handling.
171 int m_id;
172 //! The address at which the socket is currently bound.
173 amuleIPV4Address m_addr;
174 //! Proxy settings used by the socket ...
175 const CProxyData* m_proxy;
176 //! Mutex needed due to the use of the UBT.
177 wxMutex m_mutex;
178 //! The currently opened socket, if any.
179 CEncryptedDatagramSocket* m_socket;
181 //! Storage struct used for queueing packets.
182 struct UDPPack
184 //! The packet, which at this point is owned by CMuleUDPSocket.
185 CPacket* packet;
186 //! The timestamp of when the packet was queued.
187 uint32 time;
188 //! Target IP address.
189 uint32 IP;
190 //! Target port.
191 uint16 port;
192 //! If the packet is encrypted.
193 bool bEncrypt;
194 //! Is it a kad packet?
195 bool bKad;
196 // The verification key for RC4 encryption.
197 uint32 nReceiverVerifyKey;
198 // Client hash or kad ID.
199 uint8 pachTargetClientHashORKadID[16];
202 //! The queue of packets waiting to be sent.
203 std::list<UDPPack> m_queue;
206 #endif // CLIENTUDPSOCKET_H
207 // File_checked_for_headers