00001
00002
00003
00004
00005
00006
00007 #ifndef EZSOCKETS_H
00008 #define EZSOCKETS_H
00009
00010 #include <fcntl.h>
00011 #include <ctype.h>
00012 #include "MercuryString.h"
00013
00014 #if defined(_XBOX)
00015
00016
00017
00018
00019
00020 #include <xtl.h>
00021 #elif defined(WIN32)
00022 #include <winsock2.h>
00023 #else
00024 #include <netinet/in.h>
00025 #endif
00026
00027 const unsigned int ezSocketsBuffersize = 16384;
00028
00029 class ezSocketsPacket
00030 {
00031 public:
00032 ezSocketsPacket();
00033 ~ezSocketsPacket();
00034
00035 void CopyPacket( ezSocketsPacket & pPacketToCopy );
00036 void ClearPacket();
00037 void SetupPacket( unsigned int iSize );
00038 void DestroyTree();
00039
00040 char * Data;
00041 unsigned int Position;
00042 unsigned int PositionTAG;
00043 unsigned int Size;
00044 unsigned short Port;
00045
00046 ezSocketsPacket * Next;
00047
00048
00049 unsigned char Read1();
00050 unsigned short Read2();
00051 unsigned long Read4();
00052 MString ReadNT();
00053 int ReadData( int Bytes, char * ToReplace );
00054
00055 void Write1( unsigned char Info );
00056 void Write2( unsigned short Info );
00057 void Write4( unsigned long Info );
00058 void WriteNT( const MString& Info );
00059 void WriteData( const char * Info, unsigned int Length );
00060 private:
00061 void Grow( unsigned long iSizeTo );
00062 };
00063
00064 class ezSockets
00065 {
00066 public:
00067 ezSockets();
00068 ~ezSockets();
00069
00070
00071 bool Create(int Protocol = IPPROTO_TCP, int Type = SOCK_STREAM);
00072
00073
00074 bool Bind(unsigned short port);
00075
00076
00077 bool Listen(unsigned long depth = 5);
00078
00079
00080 bool Accept(ezSockets &socket);
00081 ezSockets * Accept();
00082
00083
00084 bool Connect(const MString& host, unsigned short port);
00085
00086
00087 void Close();
00088
00089
00090 bool Check();
00091
00092 bool CanRead();
00093 bool DataAvailable();
00094 bool IsError();
00095 bool CanWrite();
00096
00097
00098 void SendData(const MString& outData);
00099 void SendData(const char *data, unsigned int bytes);
00100 int ReadData(char *data, unsigned int bytes);
00101 int PeekData(char *data, unsigned int bytes);
00102 bool ReadLine( MString & str );
00103 void WriteLine( const MString & str );
00104
00105
00106 int ReadLeftover( char * data, unsigned int maxbytes );
00107
00108
00109 void SendPack( const ezSocketsPacket &pPack );
00110
00111
00112 int ReadPack( ezSocketsPacket &pPack );
00113 int PeekPack( ezSocketsPacket &pPack );
00114
00115 bool bBlocking;
00116
00117 enum SockState
00118 {
00119 skDISCONNECTED = 0,
00120 skUNDEF1,
00121 skLISTENING,
00122 skUNDEF3,
00123 skUNDEF4,
00124 skUNDEF5,
00125 skUNDEF6,
00126 skCONNECTED,
00127 skERROR
00128 } state;
00129
00130 int lastCode;
00131
00132 enum TransportMode
00133 {
00134 skPackets = 0,
00135 skGeneral,
00136 skUDP,
00137 } mode;
00138
00139 MString GetAddress() { return address; }
00140 bool SetupForReuseAddr( bool bAllow );
00141 bool SetupForMulticast( const MString & sAddressToJoin );
00142 bool SetupForBroadcast( bool bBroadcast );
00143 private:
00144
00145 MString address;
00146 ezSocketsPacket * pDataInHead;
00147 ezSocketsPacket * pDataInTail;
00148 int iBytesInPending;
00149 int iReadInPending;
00150
00151
00152
00153 void ReceiveGeneralData( bool bForceBlock = false );
00154
00155 struct sockaddr_in fromAddr;
00156
00157 int maxcon;
00158 int sock;
00159 struct sockaddr_in addr;
00160
00161
00162 fd_set *scks;
00163 timeval *times;
00164
00165 int pReadData(char* data, int maxsize );
00166 int pWriteData(const char* data, int dataSize);
00167
00168
00169 #if defined(_WIN32) || defined(_XBOX)
00170 WSADATA wsda;
00171 #endif
00172 char Buffer[ezSocketsBuffersize];
00173 };
00174
00175 #endif
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200