Update readme.md
[openttd-joker.git] / src / network / core / tcp.h
blob0e5393ffdcf0b8a14d7c250d42000b0f4797fbc2
1 /* $Id: tcp.h 24900 2013-01-08 22:46:42Z planetmaker $ */
3 /*
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/>.
8 */
10 /**
11 * @file tcp.h Basic functions to receive and send TCP packets.
14 #ifndef NETWORK_CORE_TCP_H
15 #define NETWORK_CORE_TCP_H
17 #include "address.h"
18 #include "packet.h"
20 #ifdef ENABLE_NETWORK
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 {
32 private:
33 Packet *packet_queue; ///< Packets that are awaiting delivery
34 Packet *packet_recv; ///< Partially received packet
35 public:
36 SOCKET sock; ///< The socket currently connected to
37 bool writable; ///< Can we write to this socket?
39 /**
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();
53 /**
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();
63 /**
64 * "Helper" class for creating TCP connections in a non-blocking manner
66 class TCPConnecter {
67 private:
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
74 void Connect();
76 static void ThreadEntry(void *param);
78 protected:
79 /** Address we're connecting to */
80 NetworkAddress address;
82 public:
83 TCPConnecter(const NetworkAddress &address);
84 /** Silence the warnings */
85 virtual ~TCPConnecter() {}
87 /**
88 * Callback when the connection succeeded.
89 * @param s the socket that we opened
91 virtual void OnConnect(SOCKET s) {}
93 /**
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 */