|
Odamex
Setting the Standard in Multiplayer Doom
|
00001 // Emacs style mode select -*- C++ -*- 00002 //----------------------------------------------------------------------------- 00003 // 00004 // $Id: net_io.h 1788 2010-08-24 04:42:57Z russellrice $ 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 huntsvegas dot org) 00024 // 00025 //----------------------------------------------------------------------------- 00026 00027 00028 #ifndef NET_IO_H 00029 #define NET_IO_H 00030 00031 #include <wx/socket.h> 00032 #include <wx/mstream.h> 00033 #include <wx/datstrm.h> 00034 #include <wx/timer.h> 00035 #include <wx/tokenzr.h> 00036 00037 #ifdef __WXMSW__ 00038 #include <windows.h> 00039 #include <winsock.h> 00040 #else 00041 #include <sys/socket.h> 00042 #include <netinet/in.h> 00043 #include <arpa/inet.h> 00044 #include <sys/wait.h> 00045 #include <netdb.h> 00046 #endif 00047 00048 #define MAX_PAYLOAD 8192 00049 00050 // Used for platforms such as windows (where you must initialize WSA before 00051 // using the sockets api) 00052 bool InitializeSocketAPI(); 00053 void ShutdownSocketAPI(); 00054 00055 class BufferedSocket 00056 { 00057 private: 00058 // the internal buffers, 2 for a reason 00059 void *m_ReceiveBuffer; 00060 void *m_SendBuffer; 00061 wxMemoryInputStream *m_ReceiveBufferHandler; 00062 wxMemoryOutputStream *m_SendBufferHandler; 00063 00064 bool m_BadRead; 00065 bool m_BadWrite; 00066 00067 // Endianess switch for buffers 00068 static const wxByte BigEndian; 00069 00070 // the socket 00071 int m_Socket; 00072 00073 // local address 00074 struct sockaddr_in m_LocalAddress; 00075 00076 // outgoing address (server) 00077 struct sockaddr_in m_RemoteAddress; 00078 00079 wxUint32 m_SendPing, m_ReceivePing; 00080 00081 void SetSendPing(const wxUint32 &i) { m_SendPing = i; } 00082 void SetRecvPing(const wxUint32 &i) { m_ReceivePing = i; } 00083 00084 void CheckError(); 00085 00086 bool CreateSocket(); 00087 void DestroySocket(); 00088 public: 00089 BufferedSocket(); // Create a blank instance with stuff initialized 00090 00091 virtual ~BufferedSocket(); // "Choose! Choose the form of the destructor!" 00092 00093 // Set the outgoing address 00094 void SetRemoteAddress(const wxString &Address, const wxInt16 &Port); 00095 // Set the outgoing address in "address:port" format 00096 bool SetRemoteAddress(const wxString &Address); 00097 // Gets the outgoing address 00098 void GetRemoteAddress(wxString &Address, wxUint16 &Port) const; 00099 // Gets the outgoing address in "address:port" format 00100 wxString GetRemoteAddress() const; 00101 00102 // Send/receive data 00103 wxInt32 SendData(const wxInt32 &Timeout); 00104 wxInt32 GetData(const wxInt32 &Timeout); 00105 00106 // a method for a round-trip time in milliseconds 00107 wxUint32 GetPing() { return (m_ReceivePing - m_SendPing); } 00108 00109 // Read values 00110 bool ReadString(wxString &); 00111 bool ReadBool(bool &); 00112 // Signed reads 00113 bool Read32(wxInt32 &); 00114 bool Read16(wxInt16 &); 00115 bool Read8(wxInt8 &); 00116 // Unsigned reads 00117 bool Read32(wxUint32 &); 00118 bool Read16(wxUint16 &); 00119 bool Read8(wxUint8 &); 00120 00121 bool BadRead() { return m_BadRead; } 00122 00123 // Write values 00124 bool WriteString(const wxString &); 00125 bool WriteBool(const bool &); 00126 // Signed writes 00127 bool Write32(const wxInt32 &); 00128 bool Write16(const wxInt16 &); 00129 bool Write8(const wxInt8 &); 00130 // Unsigned writes 00131 bool Write32(const wxUint32 &); 00132 bool Write16(const wxUint16 &); 00133 bool Write8(const wxUint8 &); 00134 00135 bool BadWrite() { return m_BadWrite; } 00136 00137 // Reset buffer positions to 0 00138 void ResetRecvBuffer() { m_ReceiveBufferHandler->SeekI(0, wxFromStart); } ; 00139 void ResetSendBuffer() { m_SendBufferHandler->SeekO(0, wxFromStart); }; 00140 void ResetBuffers() { ResetRecvBuffer(); ResetSendBuffer(); }; 00141 00142 // Can read or write X bytes to a buffer 00143 bool CanRead(const size_t &); 00144 bool CanWrite(const size_t &); 00145 00146 // Clear buffers 00147 void ClearRecvBuffer(); 00148 void ClearSendBuffer(); 00149 void ClearBuffers() { ClearRecvBuffer(); ClearSendBuffer(); }; 00150 }; 00151 00152 #endif