Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

ezsockets.h

Go to the documentation of this file.
00001 /*******************************************************************\ 00002 | ezsockets.h: EzSockets Class Header | 00003 | Designed by Josh Allen, Charles Lohr and Adam Lowman. | 00004 | Socket programming methods based on Charles Lohr's EZW progam. | 00005 | Modified by Charles Lohr for use with Windows-Based OSes. | 00006 | UDP/NON-TCP Support by Adam Lowman. | 00007 \*******************************************************************/ 00008 00009 #ifndef EZSOCKETS_H 00010 #define EZSOCKETS_H 00011 00012 #if defined(WITHOUT_NETWORKING) 00013 #error do not include ezsockets.h when WITHOUT_NETWORKING 00014 #endif 00015 00016 #include <sstream> 00017 #include <string> 00018 #include <vector> 00019 #include <fcntl.h> 00020 #include <ctype.h> 00021 #include "global.h" // StepMania only includes 00022 00023 #if defined(_XBOX) 00024 // Summary : WinsockX is bad, XTL is good. 00025 // Explained : WinsockX may rely on some declares 00026 // that are present in XTL. Also, using 00027 // XTL includes some files maybe needed 00028 // for other operations on Xbox. 00029 #include <xtl.h> 00030 #elif defined(_WINDOWS) 00031 #include <winsock2.h> 00032 #else 00033 #include <netinet/in.h> 00034 #endif 00035 00036 using namespace std; 00037 00038 class EzSockets 00039 { 00040 public: 00041 00042 EzSockets(); 00043 ~EzSockets(); 00044 00045 //Crate the socket 00046 bool create(); 00047 bool create(int Protocol); 00048 bool create(int Protocol, int Type); 00049 00050 //Bind Socket to local port 00051 bool bind(unsigned short port); 00052 00053 //Listen with socket 00054 bool listen(); 00055 00056 //Accept incomming socket 00057 bool accept(EzSockets &socket); 00058 00059 //Connect 00060 bool connect(const string& host, unsigned short port); 00061 00062 //Kill socket 00063 void close(); 00064 00065 //see if socket has been created 00066 bool check(); 00067 00068 long uAddr(); 00069 00070 bool CanRead(); 00071 bool IsError(); 00072 bool CanWrite(); 00073 00074 void update(); 00075 00076 //Raw data system 00077 void SendData(const string& outData); 00078 void SendData(const char *data, unsigned int bytes); 00079 int ReadData(char *data, unsigned int bytes); 00080 int PeekData(char *data, unsigned int bytes); 00081 00082 //Packet system (for structures and classes) 00083 void SendPack(const char *data, unsigned int bytes); 00084 int ReadPack(char *data, unsigned int max); 00085 int PeekPack(char *data, unsigned int max); 00086 00087 //String (Flash) system / Null-terminated strings 00088 void SendStr(const string& data, char delim = '\0'); 00089 int ReadStr(string& data, char delim = '\0'); 00090 int PeekStr(string& data, char delim = '\0'); 00091 00092 00093 //Operators 00094 char operator[] (int i); //Access buffer 00095 friend istream& operator>>(istream& is, EzSockets& obj); 00096 friend ostream& operator<<(ostream& os, const EzSockets& obj); 00097 00098 bool blocking; 00099 enum SockState 00100 { 00101 skDISCONNECTED = 0, 00102 skUNDEF1, //Not implemented 00103 skLISTENING, 00104 skUNDEF3, //Not implemented 00105 skUNDEF4, //Not implemented 00106 skUNDEF5, //Not implemented 00107 skUNDEF6, //Not implemented 00108 skCONNECTED, 00109 skERROR 00110 }; 00111 struct sockaddr_in fromAddr; 00112 unsigned long fromAddr_len; 00113 00114 //The following possibly should be private. 00115 string inBuffer; 00116 string outBuffer; 00117 00118 int pUpdateWrite(); 00119 int pUpdateRead(); 00120 00121 int pReadData(char * data); 00122 int pWriteData(const char * data, int dataSize); 00123 00124 SockState state; 00125 00126 int lastCode; //Used for debugging purposes 00127 00128 CString address; 00129 00130 private: 00131 00132 //Only necessiary in windows, xbox 00133 #if defined(_WINDOWS) || defined(_XBOX) 00134 WSADATA wsda; 00135 #endif 00136 00137 int MAXCON; 00138 int sock; 00139 struct sockaddr_in addr; 00140 00141 00142 //Used for Select() command 00143 fd_set *scks; 00144 timeval *times; 00145 00146 //Buffers 00147 }; 00148 00149 istream& operator>>(istream& is, EzSockets& obj); 00150 ostream& operator<<(ostream& os, EzSockets& obj); 00151 00152 00153 #endif 00154 00155 /* 00156 * (c) 2003-2004 Josh Allen, Charles Lohr, and Adam Lowman 00157 * All rights reserved. 00158 * 00159 * Permission is hereby granted, free of charge, to any person obtaining a 00160 * copy of this software and associated documentation files (the 00161 * "Software"), to deal in the Software without restriction, including 00162 * without limitation the rights to use, copy, modify, merge, publish, 00163 * distribute, and/or sell copies of the Software, and to permit persons to 00164 * whom the Software is furnished to do so, provided that the above 00165 * copyright notice(s) and this permission notice appear in all copies of 00166 * the Software and that both the above copyright notice(s) and this 00167 * permission notice appear in supporting documentation. 00168 * 00169 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00170 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00171 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00172 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00173 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00174 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00175 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00176 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00177 * PERFORMANCE OF THIS SOFTWARE. 00178 */

Generated on Thu Jan 27 20:57:20 2005 for StepMania by doxygen 1.3.7