|
Odamex
Setting the Standard in Multiplayer Doom
|
00001 // Emacs style mode select -*- C++ -*- 00002 //----------------------------------------------------------------------------- 00003 // 00004 // $Id: net_io.h 2164 2011-05-07 20:10:55Z hypereye $ 00005 // 00006 // Copyright (C) 2006-2010 by The Odamex Team. 00007 // 00008 // This program is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU General Public License 00010 // as published by the Free Software Foundation; either version 2 00011 // of the License, or (at your option) any later version. 00012 // 00013 // This program is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 // 00018 // DESCRIPTION: 00019 // Low-level socket and buffer class 00020 // 00021 // AUTHORS: 00022 // Russell Rice (russell at odamex dot net) 00023 // Michael Wood (mwoodj at knology dot net) 00024 // 00025 //----------------------------------------------------------------------------- 00026 00027 #ifndef NET_IO_H 00028 #define NET_IO_H 00029 00030 #ifdef _XBOX 00031 #include <xtl.h> 00032 #elif _WIN32 00033 #include <windows.h> 00034 #include <winsock.h> 00035 #else 00036 #include <sys/socket.h> 00037 #include <netinet/in.h> 00038 #include <arpa/inet.h> 00039 #include <sys/wait.h> 00040 #include <netdb.h> 00041 #endif 00042 00043 #include "typedefs.h" 00044 00051 namespace agOdalaunch { 00052 00053 #ifndef _WIN32 00054 typedef int SOCKET; 00055 #endif 00056 00057 const size_t MAX_PAYLOAD = 8192; 00058 00059 typedef unsigned char byte; 00060 00061 class BufferedSocket 00062 { 00063 public: 00064 BufferedSocket(); // Constructor 00065 00066 virtual ~BufferedSocket(); // "Choose! Choose the form of the destructor!" 00067 00068 // Used for platforms such as windows (where you must initialize WSA before 00069 // using the sockets api) 00070 static bool InitializeSocketAPI(); 00071 static void ShutdownSocketAPI(); 00072 00073 // Set the outgoing address 00074 void SetRemoteAddress(const std::string &Address, const int16_t &Port); 00075 // Set the outgoing address in "address:port" format 00076 bool SetRemoteAddress(const std::string &Address); 00077 // Gets the outgoing address 00078 void GetRemoteAddress(std::string &Address, uint16_t &Port) const; 00079 // Gets the outgoing address in "address:port" format 00080 std::string GetRemoteAddress() const; 00081 00082 // Send/receive data 00083 int32_t SendData(const int32_t &Timeout); 00084 int32_t GetData(const int32_t &Timeout); 00085 00086 // a method for a round-trip time in milliseconds 00087 uint32_t GetPing() { return (m_ReceivePing - m_SendPing); } 00088 00089 // Read values 00090 bool ReadString(std::string &); 00091 bool ReadBool(bool &); 00092 // Signed reads 00093 bool Read32(int32_t &); 00094 bool Read16(int16_t &); 00095 bool Read8(int8_t &); 00096 // Unsigned reads 00097 bool Read32(uint32_t &); 00098 bool Read16(uint16_t &); 00099 bool Read8(uint8_t &); 00100 00101 bool BadRead() { return m_BadRead; } 00102 00103 // Write values 00104 bool WriteString(const std::string &); 00105 bool WriteBool(const bool &); 00106 // Signed writes 00107 bool Write32(const int32_t &); 00108 bool Write16(const int16_t &); 00109 bool Write8(const int8_t &); 00110 // Unsigned writes 00111 bool Write32(const uint32_t &); 00112 bool Write16(const uint16_t &); 00113 bool Write8(const uint8_t &); 00114 00115 bool BadWrite() { return m_BadWrite; } 00116 00117 // Can read or write X bytes to a buffer 00118 bool CanRead(const size_t &); 00119 bool CanWrite(const size_t &); 00120 00121 // Reset Buffer 00122 void ResetBuffer() { m_BufferPos = 0; } 00123 00124 // Clear buffer 00125 void ClearBuffer(); 00126 00127 private: 00128 bool CreateSocket(); 00129 void DestroySocket(); 00130 00131 void ReportError(int line, const char *function, const char *fmt, ...); 00132 00133 void SetSendPing(const uint32_t &i) { m_SendPing = i; } 00134 void SetRecvPing(const uint32_t &i) { m_ReceivePing = i; } 00135 00136 // the socket buffer 00137 byte *m_SocketBuffer; 00138 size_t m_BufferSize; 00139 size_t m_BufferPos; 00140 00141 bool m_BadRead; 00142 bool m_BadWrite; 00143 00144 // the socket 00145 SOCKET m_Socket; 00146 00147 // local address 00148 struct sockaddr_in m_LocalAddress; 00149 00150 // outgoing address (server) 00151 struct sockaddr_in m_RemoteAddress; 00152 00153 uint32_t m_SendPing, m_ReceivePing; 00154 }; 00155 00156 } // namespace 00157 00158 #endif