2 // This file is part of the aMule Project.
4 // Copyright (c) 2005-2011 aMule Team ( admin@amule.org / http://www.amule.org )
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
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
;
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
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
);
68 * Destructor, safely closes the socket if opened.
70 virtual ~CMuleUDPSocket();
76 * The socket is bound to the address specified in
84 * The socket can be reopened by calling Open. Closing a
85 * already closed socket is an illegal operation.
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
);
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
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
123 /** Read buffer size */
124 static const unsigned UDP_BUFFER_SIZE
= 16384;
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
, byte
* buffer
, size_t length
) = 0;
137 /** See ThrottledControlSocket::SendControlData */
138 SocketSentBytes
SendControlData(uint32 maxNumberOfBytesToSend
, uint32 minFragSize
);
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.
161 * Destroys the current socket, if any.
163 void DestroySocket();
166 //! Specifies if the last write attempt would cause the socket to block.
168 //! The name of the socket, used for debugging messages.
170 //! The socket-ID, used for event-handling.
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.
178 //! The currently opened socket, if any.
179 CEncryptedDatagramSocket
* m_socket
;
181 //! Storage struct used for queueing packets.
184 //! The packet, which at this point is owned by CMuleUDPSocket.
186 //! The timestamp of when the packet was queued.
188 //! Target IP address.
192 //! If the packet is encrypted.
194 //! Is it a kad packet?
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