OpenTTD Source  1.11.0-beta2
tcp.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
12 #ifndef NETWORK_CORE_TCP_H
13 #define NETWORK_CORE_TCP_H
14 
15 #include "address.h"
16 #include "packet.h"
17 
18 #include <atomic>
19 
26 };
27 
30 private:
33 public:
34  SOCKET sock;
35  bool writable;
36 
41  bool IsConnected() const { return this->sock != INVALID_SOCKET; }
42 
43  NetworkRecvStatus CloseConnection(bool error = true) override;
44  virtual void SendPacket(Packet *packet);
45  SendPacketsState SendPackets(bool closing_down = false);
46 
47  virtual Packet *ReceivePacket();
48 
49  bool CanSendReceive();
50 
55  bool HasSendQueue() { return this->packet_queue != nullptr; }
56 
57  NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET);
59 };
60 
64 class TCPConnecter {
65 private:
66  std::atomic<bool> connected;
67  std::atomic<bool> aborted;
68  bool killed;
69  SOCKET sock;
70 
71  void Connect();
72 
73  static void ThreadEntry(TCPConnecter *param);
74 
75 protected:
78 
79 public:
82  virtual ~TCPConnecter() {}
83 
88  virtual void OnConnect(SOCKET s) {}
89 
93  virtual void OnFailure() {}
94 
95  static void CheckCallbacks();
96  static void KillAll();
97 };
98 
99 #endif /* NETWORK_CORE_TCP_H */
TCPConnecter::CheckCallbacks
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Definition: tcp_connect.cpp:65
TCPConnecter::TCPConnecter
TCPConnecter(const NetworkAddress &address)
Create a new connecter for the given address.
Definition: tcp_connect.cpp:26
TCPConnecter::OnConnect
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:88
TCPConnecter::aborted
std::atomic< bool > aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:67
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:64
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:41
SPS_CLOSED
@ SPS_CLOSED
The connection got closed.
Definition: tcp.h:22
NetworkTCPSocketHandler::IsConnected
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp.h:41
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:34
SPS_ALL_SENT
@ SPS_ALL_SENT
All packets in the queue are sent.
Definition: tcp.h:25
NetworkTCPSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:145
address.h
NetworkTCPSocketHandler::packet_recv
Packet * packet_recv
Partially received packet.
Definition: tcp.h:32
SendPacketsState
SendPacketsState
The states of sending the packets.
Definition: tcp.h:21
NetworkTCPSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Definition: tcp.cpp:38
TCPConnecter::killed
bool killed
Whether we got killed.
Definition: tcp.h:68
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:61
NetworkTCPSocketHandler::HasSendQueue
bool HasSendQueue()
Whether there is something pending in the send queue.
Definition: tcp.h:55
Packet
Internal entity of a packet.
Definition: packet.h:40
TCPConnecter::KillAll
static void KillAll()
Kill all connection attempts.
Definition: tcp_connect.cpp:94
TCPConnecter::address
NetworkAddress address
Address we're connecting to.
Definition: tcp.h:77
NetworkTCPSocketHandler::NetworkTCPSocketHandler
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition: tcp.cpp:23
NetworkTCPSocketHandler::writable
bool writable
Can we write to this socket?
Definition: tcp.h:35
SPS_NONE_SENT
@ SPS_NONE_SENT
The buffer is still full, so no (parts of) packets could be sent.
Definition: tcp.h:23
TCPConnecter::sock
SOCKET sock
The socket we're connecting with.
Definition: tcp.h:69
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:95
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:29
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:225
TCPConnecter::connected
std::atomic< bool > connected
Whether we succeeded in making the connection.
Definition: tcp.h:66
packet.h
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
TCPConnecter::ThreadEntry
static void ThreadEntry(TCPConnecter *param)
Entry point for the new threads.
Definition: tcp_connect.cpp:54
TCPConnecter::~TCPConnecter
virtual ~TCPConnecter()
Silence the warnings.
Definition: tcp.h:82
TCPConnecter::OnFailure
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:93
TCPConnecter::Connect
void Connect()
The actual connection function.
Definition: tcp_connect.cpp:40
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:129
SPS_PARTLY_SENT
@ SPS_PARTLY_SENT
The packets are partly sent; there are more packets to be sent in the queue.
Definition: tcp.h:24
NetworkTCPSocketHandler
Base socket handler for all TCP sockets.
Definition: tcp.h:29
NetworkTCPSocketHandler::packet_queue
Packet * packet_queue
Packets that are awaiting delivery.
Definition: tcp.h:31