OpenTTD
tcp.h
Go to the documentation of this file.
1 /* $Id$ */
2 
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  */
9 
14 #ifndef NETWORK_CORE_TCP_H
15 #define NETWORK_CORE_TCP_H
16 
17 #include "address.h"
18 #include "packet.h"
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  bool connected;
67  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:
80  TCPConnecter(const NetworkAddress &address);
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 */
NetworkAddress address
Address we&#39;re connecting to.
Definition: tcp.h:77
SOCKET sock
The socket currently connected to.
Definition: tcp.h:34
Internal entity of a packet.
Definition: packet.h:42
The connection got closed.
Definition: tcp.h:22
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:64
bool aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:67
SOCKET sock
The socket we&#39;re connecting with.
Definition: tcp.h:69
Base socket handler for all TCP sockets.
Definition: tcp.h:29
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:40
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:29
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:88
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:147
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:97
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:93
bool writable
Can we write to this socket?
Definition: tcp.h:35
Packet * packet_recv
Partially received packet.
Definition: tcp.h:32
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:24
Wrapper for network addresses.
All packets in the queue are sent.
Definition: tcp.h:25
virtual ~TCPConnecter()
Silence the warnings.
Definition: tcp.h:82
The buffer is still full, so no (parts of) packets could be sent.
Definition: tcp.h:23
bool killed
Whether we got killed.
Definition: tcp.h:68
bool connected
Whether we succeeded in making the connection.
Definition: tcp.h:66
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:114
The packets are partly sent; there are more packets to be sent in the queue.
Definition: tcp.h:24
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp.h:41
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:63
bool HasSendQueue()
Whether there is something pending in the send queue.
Definition: tcp.h:55
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:227
SendPacketsState
The states of sending the packets.
Definition: tcp.h:21
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition: tcp.cpp:25
Packet * packet_queue
Packets that are awaiting delivery.
Definition: tcp.h:31
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:43