1 /* $Id: tcp.h 24900 2013-01-08 22:46:42Z planetmaker $ */
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
11 * @file tcp.h Basic functions to receive and send TCP packets.
14 #ifndef NETWORK_CORE_TCP_H
15 #define NETWORK_CORE_TCP_H
22 /** The states of sending the packets. */
23 enum SendPacketsState
{
24 SPS_CLOSED
, ///< The connection got closed.
25 SPS_NONE_SENT
, ///< The buffer is still full, so no (parts of) packets could be sent.
26 SPS_PARTLY_SENT
, ///< The packets are partly sent; there are more packets to be sent in the queue.
27 SPS_ALL_SENT
, ///< All packets in the queue are sent.
30 /** Base socket handler for all TCP sockets */
31 class NetworkTCPSocketHandler
: public NetworkSocketHandler
{
33 Packet
*packet_queue
; ///< Packets that are awaiting delivery
34 Packet
*packet_recv
; ///< Partially received packet
36 SOCKET sock
; ///< The socket currently connected to
37 bool writable
; ///< Can we write to this socket?
40 * Whether this socket is currently bound to a socket.
41 * @return true when the socket is bound, false otherwise
43 bool IsConnected() const { return this->sock
!= INVALID_SOCKET
; }
45 virtual NetworkRecvStatus
CloseConnection(bool error
= true);
46 virtual void SendPacket(Packet
*packet
);
47 SendPacketsState
SendPackets(bool closing_down
= false);
49 virtual Packet
*ReceivePacket();
51 bool CanSendReceive();
54 * Whether there is something pending in the send queue.
55 * @return true when something is pending in the send queue.
57 bool HasSendQueue() { return this->packet_queue
!= nullptr; }
59 NetworkTCPSocketHandler(SOCKET s
= INVALID_SOCKET
);
60 ~NetworkTCPSocketHandler();
64 * "Helper" class for creating TCP connections in a non-blocking manner
68 class ThreadObject
*thread
; ///< Thread used to create the TCP connection
69 bool connected
; ///< Whether we succeeded in making the connection
70 bool aborted
; ///< Whether we bailed out (i.e. connection making failed)
71 bool killed
; ///< Whether we got killed
72 SOCKET sock
; ///< The socket we're connecting with
76 static void ThreadEntry(void *param
);
79 /** Address we're connecting to */
80 NetworkAddress address
;
83 TCPConnecter(const NetworkAddress
&address
);
84 /** Silence the warnings */
85 virtual ~TCPConnecter() {}
88 * Callback when the connection succeeded.
89 * @param s the socket that we opened
91 virtual void OnConnect(SOCKET s
) {}
94 * Callback for when the connection attempt failed.
96 virtual void OnFailure() {}
98 static void CheckCallbacks();
99 static void KillAll();
102 #endif /* ENABLE_NETWORK */
104 #endif /* NETWORK_CORE_TCP_H */