2007-01-02 18:34:03 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-02-23 09:37:33 +01:00
|
|
|
/**
|
|
|
|
* @file tcp.h Basic functions to receive and send TCP packets.
|
|
|
|
*/
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
#ifndef NETWORK_CORE_TCP_H
|
|
|
|
#define NETWORK_CORE_TCP_H
|
|
|
|
|
2009-01-20 12:28:18 +01:00
|
|
|
#include "address.h"
|
2007-01-04 22:21:14 +01:00
|
|
|
#include "packet.h"
|
|
|
|
|
2009-10-04 22:00:56 +02:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2011-02-12 22:09:34 +01:00
|
|
|
/** The states of sending the packets. */
|
|
|
|
enum SendPacketsState {
|
|
|
|
SPS_CLOSED, ///< The connection got closed.
|
|
|
|
SPS_NONE_SENT, ///< The buffer is still full, so no (parts of) packets could be sent.
|
|
|
|
SPS_PARTLY_SENT, ///< The packets are partly sent; there are more packets to be sent in the queue.
|
|
|
|
SPS_ALL_SENT, ///< All packets in the queue are sent.
|
|
|
|
};
|
|
|
|
|
2007-01-12 21:19:49 +01:00
|
|
|
/** Base socket handler for all TCP sockets */
|
2009-01-14 13:50:13 +01:00
|
|
|
class NetworkTCPSocketHandler : public NetworkSocketHandler {
|
2007-02-02 00:50:15 +01:00
|
|
|
private:
|
|
|
|
Packet *packet_queue; ///< Packets that are awaiting delivery
|
|
|
|
Packet *packet_recv; ///< Partially received packet
|
2007-01-12 21:19:49 +01:00
|
|
|
public:
|
2009-04-07 22:27:13 +02:00
|
|
|
SOCKET sock; ///< The socket currently connected to
|
2007-01-28 21:47:25 +01:00
|
|
|
bool writable; ///< Can we write to this socket?
|
2007-01-12 21:19:49 +01:00
|
|
|
|
2009-04-07 22:27:13 +02:00
|
|
|
/**
|
|
|
|
* Whether this socket is currently bound to a socket.
|
|
|
|
* @return true when the socket is bound, false otherwise
|
|
|
|
*/
|
|
|
|
bool IsConnected() const { return this->sock != INVALID_SOCKET; }
|
|
|
|
|
2009-06-19 22:26:18 +02:00
|
|
|
virtual NetworkRecvStatus CloseConnection(bool error = true);
|
2010-12-05 15:48:39 +01:00
|
|
|
virtual void SendPacket(Packet *packet);
|
2011-02-12 22:09:34 +01:00
|
|
|
SendPacketsState SendPackets(bool closing_down = false);
|
2007-01-12 21:19:49 +01:00
|
|
|
|
2010-11-30 21:01:26 +01:00
|
|
|
virtual Packet *ReceivePacket();
|
2008-12-23 09:39:30 +01:00
|
|
|
|
2010-10-15 21:33:08 +02:00
|
|
|
bool CanSendReceive();
|
|
|
|
|
2012-10-28 22:26:57 +01:00
|
|
|
/**
|
|
|
|
* Whether there is something pending in the send queue.
|
|
|
|
* @return true when someting is pending in the send queue.
|
|
|
|
*/
|
|
|
|
bool HasSendQueue() { return this->packet_queue != NULL; }
|
|
|
|
|
2009-01-14 13:50:13 +01:00
|
|
|
NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET);
|
|
|
|
~NetworkTCPSocketHandler();
|
2007-02-02 00:50:15 +01:00
|
|
|
};
|
2007-01-02 18:34:03 +01:00
|
|
|
|
2009-01-20 12:28:18 +01:00
|
|
|
/**
|
|
|
|
* "Helper" class for creating TCP connections in a non-blocking manner
|
|
|
|
*/
|
|
|
|
class TCPConnecter {
|
|
|
|
private:
|
|
|
|
class ThreadObject *thread; ///< Thread used to create the TCP connection
|
|
|
|
bool connected; ///< Whether we succeeded in making the connection
|
|
|
|
bool aborted; ///< Whether we bailed out (i.e. connection making failed)
|
|
|
|
bool killed; ///< Whether we got killed
|
|
|
|
SOCKET sock; ///< The socket we're connecting with
|
|
|
|
|
|
|
|
void Connect();
|
|
|
|
|
|
|
|
static void ThreadEntry(void *param);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** Address we're connecting to */
|
|
|
|
NetworkAddress address;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TCPConnecter(const NetworkAddress &address);
|
|
|
|
/** Silence the warnings */
|
|
|
|
virtual ~TCPConnecter() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback when the connection succeeded.
|
|
|
|
* @param s the socket that we opened
|
|
|
|
*/
|
|
|
|
virtual void OnConnect(SOCKET s) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for when the connection attempt failed.
|
|
|
|
*/
|
|
|
|
virtual void OnFailure() {}
|
|
|
|
|
|
|
|
static void CheckCallbacks();
|
|
|
|
static void KillAll();
|
|
|
|
};
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
|
|
|
#endif /* NETWORK_CORE_TCP_H */
|