00001 /* $Id: tcp.h 24642 2012-10-28 21:26:57Z rubidium $ */ 00002 00003 /* 00004 * This file is part of OpenTTD. 00005 * 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. 00006 * 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. 00007 * 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/>. 00008 */ 00009 00014 #ifndef NETWORK_CORE_TCP_H 00015 #define NETWORK_CORE_TCP_H 00016 00017 #include "address.h" 00018 #include "packet.h" 00019 00020 #ifdef ENABLE_NETWORK 00021 00023 enum SendPacketsState { 00024 SPS_CLOSED, 00025 SPS_NONE_SENT, 00026 SPS_PARTLY_SENT, 00027 SPS_ALL_SENT, 00028 }; 00029 00031 class NetworkTCPSocketHandler : public NetworkSocketHandler { 00032 private: 00033 Packet *packet_queue; 00034 Packet *packet_recv; 00035 public: 00036 SOCKET sock; 00037 bool writable; 00038 00043 bool IsConnected() const { return this->sock != INVALID_SOCKET; } 00044 00045 virtual NetworkRecvStatus CloseConnection(bool error = true); 00046 virtual void SendPacket(Packet *packet); 00047 SendPacketsState SendPackets(bool closing_down = false); 00048 00049 virtual Packet *ReceivePacket(); 00050 00051 bool CanSendReceive(); 00052 00057 bool HasSendQueue() { return this->packet_queue != NULL; } 00058 00059 NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET); 00060 ~NetworkTCPSocketHandler(); 00061 }; 00062 00066 class TCPConnecter { 00067 private: 00068 class ThreadObject *thread; 00069 bool connected; 00070 bool aborted; 00071 bool killed; 00072 SOCKET sock; 00073 00074 void Connect(); 00075 00076 static void ThreadEntry(void *param); 00077 00078 protected: 00080 NetworkAddress address; 00081 00082 public: 00083 TCPConnecter(const NetworkAddress &address); 00085 virtual ~TCPConnecter() {} 00086 00091 virtual void OnConnect(SOCKET s) {} 00092 00096 virtual void OnFailure() {} 00097 00098 static void CheckCallbacks(); 00099 static void KillAll(); 00100 }; 00101 00102 #endif /* ENABLE_NETWORK */ 00103 00104 #endif /* NETWORK_CORE_TCP_H */