Improve speed of category tab title updates
[amule.git] / src / MuleUDPSocket.h
blobc36d6ef1c4731aba934cc126b5e3739cd9c2aae5
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2005-2008 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.
19 //
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 class CEncryptedDatagramSocket;
34 class CProxyData;
35 class CPacket;
37 /***
38 * This class provides a UBT governed UDP-socket.
40 * The CMuleUDPSocket are created with the NOWAIT option and
41 * handle both INPUT and OUTPUT events.
43 * The following additional features are provided compared to CDatagramSocketProxy:
44 * - Goverened by the UBT.
45 * - Automatic sending/receiving of packets.
46 * - Fallover recovery for when a socket becomes invalid (error 4).
48 * @see ThrottledControlSocket
49 * @see CEncryptedDatagramSocket
51 class CMuleUDPSocket : public ThrottledControlSocket
54 public:
55 /**
56 * Opens a UDP socket at the specified address.
58 * @param name Name used when logging events.
59 * @param id The ID used for events.
60 * @param address The address where the socket will listen.
61 * @param ProxyData ProxyData assosiated with the socket.
63 CMuleUDPSocket(const wxString& name, int id, const amuleIPV4Address& address, const CProxyData* ProxyData = NULL);
65 /**
66 * Destructor, safely closes the socket if opened.
68 virtual ~CMuleUDPSocket();
71 /**
72 * Opens the socket.
74 * The socket is bound to the address specified in
75 * the constructor.
77 void Open();
79 /**
80 * Closes the socket.
82 * The socket can be reopened by calling Open. Closing a
83 * already closed socket is an illegal operation.
85 void Close();
88 /** This function is called by aMule when the socket may send. */
89 virtual void OnSend(int errorCode);
90 /** This function is called by aMule when there are data to be received. */
91 virtual void OnReceive(int errorCode);
92 /** This function is called by aMule when there is an error while receiving. */
93 virtual void OnReceiveError(int errorCode, uint32 ip, uint16 port);
94 /** This function is called when the socket is lost (see comments in func.) */
95 virtual void OnDisconnected(int errorCode);
97 /**
98 * Queues a packet for sending.
100 * @param packet The packet to send.
101 * @param IP The target IP address.
102 * @param port The target port.
103 * @param bEncrypt If the packet must be encrypted
104 * @param port The target port.
105 * @param pachTargetClientHashORKadID The client hash or Kad ID
106 * @param bKad
107 * @param nReceiverVerifyKey
109 * Note that CMuleUDPSocket takes ownership of the packet.
111 void SendPacket(CPacket* packet, uint32 IP, uint16 port, bool bEncrypt, const uint8* pachTargetClientHashORKadID, bool bKad, uint32 nReceiverVerifyKey);
115 * Returns true if the socket is Ok, false otherwise.
117 * @see wxSocketBase::Ok
119 bool Ok();
121 protected:
123 * This function is called when a packet has been received.
125 * @param addr The address from where data was received.
126 * @param buffer The data that has been received.
127 * @param length The length of the data buffer.
129 virtual void OnPacketReceived(uint32 ip, uint16 port, byte* buffer, size_t length) = 0;
132 /** See ThrottledControlSocket::SendControlData */
133 SocketSentBytes SendControlData(uint32 maxNumberOfBytesToSend, uint32 minFragSize);
135 private:
137 * Sends a packet to the specified address.
139 * @param buffer The data to be sent.
140 * @param length the length of the data buffer.
141 * @param ip The target ip address.
142 * @param port The target port.
144 bool SendTo(uint8_t *buffer, uint32_t length, uint32_t ip, uint16_t port);
148 * Creates a new socket.
150 * Calling this function when a socket already exists
151 * is an illegal operation.
153 void CreateSocket();
156 * Destroys the current socket, if any.
158 void DestroySocket();
161 //! Specifies if the last write attempt would cause the socket to block.
162 bool m_busy;
163 //! The name of the socket, used for debugging messages.
164 wxString m_name;
165 //! The socket-ID, used for event-handling.
166 int m_id;
167 //! The address at which the socket is currently bound.
168 amuleIPV4Address m_addr;
169 //! Proxy settings used by the socket ...
170 const CProxyData* m_proxy;
171 //! Mutex needed due to the use of the UBT.
172 wxMutex m_mutex;
173 //! The currently opened socket, if any.
174 CEncryptedDatagramSocket* m_socket;
176 //! Storage struct used for queueing packets.
177 struct UDPPack
179 //! The packet, which at this point is owned by CMuleUDPSocket.
180 CPacket* packet;
181 //! The timestamp of when the packet was queued.
182 uint32 time;
183 //! Target IP address.
184 uint32 IP;
185 //! Target port.
186 uint16 port;
187 //! If the packet is encrypted.
188 bool bEncrypt;
189 //! Is it a kad packet?
190 bool bKad;
191 // The verification key for RC4 encryption.
192 uint32 nReceiverVerifyKey;
193 // Client hash or kad ID.
194 uint8 pachTargetClientHashORKadID[16];
197 //! The queue of packets waiting to be sent.
198 std::list<UDPPack> m_queue;
201 #endif // CLIENTUDPSOCKET_H
202 // File_checked_for_headers