ezSockets.h

Go to the documentation of this file.
00001 //ezSockets.h - Easy-to-use multiplatform sockets class
00002 //  Designed by Joshua Allen
00003 //  UDP Supporty by Adam Lowman
00004 //  Editor for EzSockets 1, 2 and 3: Charles Lohr
00005 //  Supports: OSX, Linux, Windows (NT 3.5+ or 95+), PS2, XBOX
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 // Summary : WinsockX is bad, XTL is good.
00016 // Explained : WinsockX may rely on some declares 
00017 //             that are present in XTL. Also, using
00018 //             XTL includes some files maybe needed
00019 //             for other operations on Xbox.
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;   //Special -- for internal use unless UDP mode, then it's the address
00043     unsigned int Size;
00044     unsigned short  Port;   //The remote port;
00045 
00046     ezSocketsPacket * Next; //Only used if in a linked list.
00047 
00048     //Commands used to operate on NetPackets
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     //Crate the socket
00071     bool Create(int Protocol = IPPROTO_TCP, int Type = SOCK_STREAM);
00072 
00073     //Bind Socket to local port
00074     bool Bind(unsigned short port);
00075 
00076     //Listen with socket
00077     bool Listen(unsigned long depth = 5);
00078 
00079     //Accept incomming socket
00080     bool Accept(ezSockets &socket);
00081     ezSockets * Accept();
00082 
00083     //Connect
00084     bool Connect(const MString& host, unsigned short port);
00085 
00086     //Kill socket
00087     void Close();
00088 
00089     //see if socket has been created
00090     bool Check();
00091 
00092     bool CanRead();
00093     bool DataAvailable();
00094     bool IsError();
00095     bool CanWrite();
00096 
00097     //Raw data system 
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     //Read arbitrary amount of data
00106     int ReadLeftover( char * data, unsigned int maxbytes );
00107 
00108     //Packet system (for structures and classes)
00109     void SendPack( const ezSocketsPacket &pPack ); 
00110 
00111     //Read 
00112     int ReadPack( ezSocketsPacket &pPack );
00113     int PeekPack( ezSocketsPacket &pPack );
00114 
00115     bool bBlocking;
00116 
00117     enum SockState
00118     { 
00119         skDISCONNECTED = 0, 
00120         skUNDEF1, //Not implemented
00121         skLISTENING, 
00122         skUNDEF3, //Not implemented
00123         skUNDEF4, //Not implemented
00124         skUNDEF5, //Not implemented
00125         skUNDEF6, //Not implemented
00126         skCONNECTED, 
00127         skERROR 
00128     } state;
00129 
00130     int lastCode;   //Used for debugging purposes
00131 
00132     enum TransportMode
00133     {
00134         skPackets = 0,  //The normal packet system we are used to (Transmit/Receive Size)
00135         skGeneral,      //Flow the data into a general use buffer, or out of a buffer. 
00136         skUDP,          //Move data from UDP on a per-packet basis. The first 4 bytes will be the UDP Address. (Transmit is the same)
00137     } mode;
00138 
00139     MString GetAddress() { return address; }
00140     bool SetupForReuseAddr( bool bAllow );  //After Create
00141     bool SetupForMulticast( const MString & sAddressToJoin ); //After Create, after ReuseAddr, before receive.
00142     bool SetupForBroadcast( bool bBroadcast );  //After create, before connect.
00143 private:
00144 
00145     MString address;
00146     ezSocketsPacket * pDataInHead;
00147     ezSocketsPacket * pDataInTail;
00148     int iBytesInPending;
00149     int iReadInPending;
00150 
00151     //Shift all data from the socket, into pDataInHead.
00152     //bForceBlock will block if in packet mode, until a full packet has been received.
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     //Used for Select() command
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     //Only necessiary in windows, xbox
00169 #if defined(_WIN32) || defined(_XBOX)
00170     WSADATA wsda;
00171 #endif
00172     char Buffer[ezSocketsBuffersize];
00173 };
00174 
00175 #endif
00176 
00177 /*
00178  * (c) 2003-2006 Josh Allen, Charles Lohr, and Adam Lowman
00179  * All rights reserved.
00180  * 
00181  * Permission is hereby granted, free of charge, to any person obtaining a
00182  * copy of this software and associated documentation files (the
00183  * "Software"), to deal in the Software without restriction, including
00184  * without limitation the rights to use, copy, modify, merge, publish,
00185  * distribute, and/or sell copies of the Software, and to permit persons to
00186  * whom the Software is furnished to do so, provided that the above
00187  * copyright notice(s) and this permission notice appear in all copies of
00188  * the Software and that both the above copyright notice(s) and this
00189  * permission notice appear in supporting documentation.
00190  * 
00191  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00192  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00193  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
00194  * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
00195  * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
00196  * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
00197  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
00198  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00199  * PERFORMANCE OF THIS SOFTWARE.
00200  */

Hosted by SourceForge.net Logo