OpenTTD
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 
24 };
25 
28 private:
31 public:
32  SOCKET sock;
33  bool writable;
34 
39  bool IsConnected() const { return this->sock != INVALID_SOCKET; }
40 
41  NetworkRecvStatus CloseConnection(bool error = true) override;
42  virtual void SendPacket(Packet *packet);
43  SendPacketsState SendPackets(bool closing_down = false);
44 
45  virtual Packet *ReceivePacket();
46 
47  bool CanSendReceive();
48 
53  bool HasSendQueue() { return this->packet_queue != nullptr; }
54 
55  NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET);
57 };
58 
62 class TCPConnecter {
63 private:
64  bool connected;
65  bool aborted;
66  bool killed;
67  SOCKET sock;
68 
69  void Connect();
70 
71  static void ThreadEntry(TCPConnecter *param);
72 
73 protected:
76 
77 public:
78  TCPConnecter(const NetworkAddress &address);
80  virtual ~TCPConnecter() {}
81 
86  virtual void OnConnect(SOCKET s) {}
87 
91  virtual void OnFailure() {}
92 
93  static void CheckCallbacks();
94  static void KillAll();
95 };
96 
97 #endif /* NETWORK_CORE_TCP_H */
NetworkAddress address
Address we&#39;re connecting to.
Definition: tcp.h:75
SOCKET sock
The socket currently connected to.
Definition: tcp.h:32
Internal entity of a packet.
Definition: packet.h:40
The connection got closed.
Definition: tcp.h:20
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:62
bool aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:65
SOCKET sock
The socket we&#39;re connecting with.
Definition: tcp.h:67
Base socket handler for all TCP sockets.
Definition: tcp.h:27
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
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:27
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:86
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:145
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:95
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:91
bool writable
Can we write to this socket?
Definition: tcp.h:33
Packet * packet_recv
Partially received packet.
Definition: tcp.h:30
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
Wrapper for network addresses.
All packets in the queue are sent.
Definition: tcp.h:23
virtual ~TCPConnecter()
Silence the warnings.
Definition: tcp.h:80
The buffer is still full, so no (parts of) packets could be sent.
Definition: tcp.h:21
bool killed
Whether we got killed.
Definition: tcp.h:66
bool connected
Whether we succeeded in making the connection.
Definition: tcp.h:64
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:112
The packets are partly sent; there are more packets to be sent in the queue.
Definition: tcp.h:22
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp.h:39
Basic functions to create, fill and read packets.
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
bool HasSendQueue()
Whether there is something pending in the send queue.
Definition: tcp.h:53
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:225
SendPacketsState
The states of sending the packets.
Definition: tcp.h:19
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition: tcp.cpp:23
Packet * packet_queue
Packets that are awaiting delivery.
Definition: tcp.h:29
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:41