- Timestamp:
- 10/24/12 11:12:11 (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/CRXSocket.h
r5 r14 18 18 using namespace std; 19 19 20 typedef struct sockaddr stSockAddr; 21 typedef struct sockaddr_in stSockAddrIn; 22 typedef unsigned short ushort; 23 typedef unsigned int uint; 20 #define TCPSOCKET_NO_TIMEOUT -1 24 21 25 22 #define ERROR_TCPSOCKET -1000 … … 36 33 #define ERROR_TCPSOCKET_FAILED_TO_RECEIVE ERROR_TCPSOCKET - 10 37 34 #define ERROR_TCPSOCKET_NOT_READY ERROR_TCPSOCKET - 11 35 #define ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT ERROR_TCPSOCKET - 12 38 36 39 37 class CRXSocket 40 38 { 41 39 private: 42 static bool mInitialized;43 st SockAddrIn mAddress;44 int mSocket;40 static bool mInitialized; 41 struct sockaddr_in mAddress; 42 int mSocket; 45 43 46 44 private: … … 90 88 * for server 91 89 */ 92 int CreateServer ( ushortaPort,93 intaBacklog = 5,94 st SockAddrIn* aAddress = NULL);90 int CreateServer (const unsigned short aPort, 91 const int aBacklog = 5, 92 struct sockaddr_in * aAddress = NULL); 95 93 96 int Accept (st SockAddrIn * aRemoteAddress = NULL,97 int * aAddressLength = NULL);94 int Accept (struct sockaddr_in * aRemoteAddress = NULL, 95 int * aAddressLength = NULL); 98 96 99 97 /** 100 98 * for client 101 99 */ 102 int Connect (std::string aUrl, 103 ushort aPort); 100 int Connect (const std::string aUrl, 101 const unsigned short aPort, 102 const int aTimeout = TCPSOCKET_NO_TIMEOUT); 104 103 105 104 /** -
trunk/src/CRXSocket.cpp
r6 r14 18 18 { 19 19 /*----------------------------------------------------------------*/ 20 memset ((void *)&mAddress, 0x0, sizeof (st SockAddrIn));20 memset ((void *)&mAddress, 0x0, sizeof (struct sockaddr_in)); 21 21 22 22 if (!CRXSocket::IsInitialized ()) … … 160 160 161 161 int 162 CRXSocket::Connect (std::string aUrl, 163 ushort aPort) 162 CRXSocket::Connect (const std::string aUrl, 163 const unsigned short aPort, 164 const int aTimeout) 164 165 { 165 166 int aResult = -1; 166 167 struct hostent * aHostEnt; 168 169 #ifndef _WIN32 170 struct timeval aTimeVal; 171 aTimeVal.tv_sec = aTimeout; 172 aTimeVal.tv_usec= 0; 173 #else 174 int aTimeMilliSec = aTimeout * 1000; 175 #endif 167 176 168 177 /*----------------------------------------------------------------*/ … … 188 197 } 189 198 190 aResult = connect (mSocket, (stSockAddr*) &mAddress, sizeof (mAddress)); 199 if (aTimeout > TCPSOCKET_NO_TIMEOUT) 200 { 201 #ifdef _WIN32 202 aResult = setsockopt (mSocket, 203 SOL_SOCKET, 204 SO_RCVTIMEO, 205 (char *) &aTimeMilliSec, 206 sizeof (aTimeMilliSec)); 207 #else 208 aResult = setsockopt (mSocket, 209 SOL_SOCKET, 210 SO_RCVTIMEO, 211 &aTimeVal, 212 (socklen_t) sizeof (aTimeVal)); 213 #endif 214 if (aResult < 0) 215 { 216 return ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT; 217 } 218 } 219 220 aResult = connect (mSocket, (struct sockaddr*) &mAddress, sizeof (mAddress)); 191 221 if (aResult < 0) 192 222 { … … 199 229 200 230 int 201 CRXSocket::CreateServer ( ushortaPort,202 intaBacklog,203 st SockAddrIn* aAddress)231 CRXSocket::CreateServer (const unsigned short aPort, 232 const int aBacklog, 233 struct sockaddr_in * aAddress) 204 234 { 205 235 int aResult = -1; … … 216 246 mAddress.sin_port = htons (aPort); 217 247 218 aResult = bind (mSocket, (st SockAddr *)&mAddress, sizeof (stSockAddr));248 aResult = bind (mSocket, (struct sockaddr *)&mAddress, sizeof (struct sockaddr)); 219 249 if (aResult < 0) 220 250 { … … 230 260 if (aAddress != NULL) 231 261 { 232 memset ((void *)aAddress, 0x0, sizeof (st SockAddrIn));233 memcpy ((void *)aAddress, (void *)&mAddress, sizeof (st SockAddrIn));234 } 235 236 /*----------------------------------------------------------------*/ 237 return aResult; 238 } 239 240 int 241 CRXSocket::Accept (st SockAddrIn* aRemoteAddress,242 int * aAddressLength)243 { 244 int aResult = -1;245 246 st SockAddrIn aAddress;247 socklen_t aLength = sizeof (aAddress);262 memset ((void *)aAddress, 0x0, sizeof (struct sockaddr_in)); 263 memcpy ((void *)aAddress, (void *)&mAddress, sizeof (struct sockaddr_in)); 264 } 265 266 /*----------------------------------------------------------------*/ 267 return aResult; 268 } 269 270 int 271 CRXSocket::Accept (struct sockaddr_in * aRemoteAddress, 272 int * aAddressLength) 273 { 274 int aResult = -1; 275 276 struct sockaddr_in aAddress; 277 socklen_t aLength = sizeof (aAddress); 248 278 249 279 /*----------------------------------------------------------------*/ 250 280 if (!IsReady ()) return ERROR_TCPSOCKET_NOT_READY; 251 281 252 aResult = accept (mSocket, (st SockAddr *) &aAddress, &aLength);282 aResult = accept (mSocket, (struct sockaddr *) &aAddress, &aLength); 253 283 if (aResult < 0) 254 284 { … … 258 288 if (aRemoteAddress != NULL) 259 289 { 260 memset ((void *)aRemoteAddress, 0x0, sizeof (st SockAddrIn));261 memcpy ((void *)aRemoteAddress, (void *)&aAddress, sizeof (st SockAddrIn));290 memset ((void *)aRemoteAddress, 0x0, sizeof (struct sockaddr_in)); 291 memcpy ((void *)aRemoteAddress, (void *)&aAddress, sizeof (struct sockaddr_in)); 262 292 } 263 293 -
trunk/src/main.cpp
r6 r14 42 42 __stream << __message; \ 43 43 string __string = __stream.str (); \ 44 sprintf (__error, "[%s][%d] %d :%s\n", __func__, __LINE__, __code, __string.c_str ()); \44 sprintf (__error, "[%s][%d] (%d) %s\n", __func__, __LINE__, __code, __string.c_str ()); \ 45 45 cout << __error; \ 46 46 } while (0) … … 54 54 int main (int argc, char* argv[]) 55 55 { 56 int aResult = 0;57 const u shortaPort = 8080;56 int aResult = 0; 57 const unsigned short aPort = 8080; 58 58 59 59 CRXSocket aSocket; … … 119 119 CRXProxyThread (void * aThreadArg) 120 120 { 121 int aResult = 0;122 int aReceivedSize = 0;123 int aSentSize = 0;124 125 const u intaBufferSize = 1024 * 64;126 char aBuffer[aBufferSize] = {0x00, };127 128 CRXHttpRequest aHttpRequest;129 CRXHttpResponse aHttpResponse;130 string aUrl = "";131 string aHost = "";132 int aPort = 0;133 134 CRXSocket aWebServer;135 CRXSocket aWebBrowser = ((CRXProxyThreadArgs *)aThreadArg)->mSocket;136 137 bool aIsMoreRequest = false;121 int aResult = 0; 122 int aReceivedSize = 0; 123 int aSentSize = 0; 124 125 const unsigned int aBufferSize = 1024 * 64; 126 char aBuffer[aBufferSize] = {0x00, }; 127 128 CRXHttpRequest aHttpRequest; 129 CRXHttpResponse aHttpResponse; 130 string aUrl = ""; 131 string aHost = ""; 132 int aPort = 0; 133 134 CRXSocket aWebServer; 135 CRXSocket aWebBrowser = ((CRXProxyThreadArgs *)aThreadArg)->mSocket; 136 137 bool aIsMoreRequest = false; 138 138 /*----------------------------------------------------------------*/ 139 139 delete (CRXProxyThreadArgs *)aThreadArg; … … 145 145 if (aReceivedSize < 0) 146 146 { 147 CRX_PRINT_ERROR (aReceivedSize, "failed to receive from client"); 148 aResult = CRX_ERROR (); 147 CRX_PRINT_ERROR (aReceivedSize, "failed to receive from client (" << CRX_ERROR () << ")"); 149 148 return aResult; 150 149 } … … 157 156 158 157 /* connect */ 159 aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort () );158 aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort (), 1); 160 159 if (aResult < 0) 161 160 { 162 CRX_PRINT_ERROR (aResult, "failed to connect to server <" << aHost << ":" << aPort << ">"); 163 aResult = CRX_ERROR (); 161 CRX_PRINT_ERROR (aResult, "failed to connect to server <" << aHost << ":" << aPort << "> (" << CRX_ERROR () << ")"); 164 162 return aResult; 165 163 } … … 170 168 if (aSentSize != aReceivedSize) 171 169 { 172 CRX_PRINT_ERROR (aResult, "failed to send to server"); 173 aResult = CRX_ERROR (); 170 CRX_PRINT_ERROR (aResult, "failed to send to server (" << CRX_ERROR () << ")"); 174 171 return aResult; 175 172 } … … 183 180 if (aReceivedSize < 0) 184 181 { 185 CRX_PRINT_ERROR (aReceivedSize, "failed to receive"); 186 aResult = CRX_ERROR (); 182 CRX_PRINT_ERROR (aReceivedSize, "failed to receive (" << CRX_ERROR () << ")"); 187 183 break; 188 184 } … … 196 192 if (aSentSize != aReceivedSize) 197 193 { 198 CRX_PRINT_ERROR (aSentSize, "failed to send"); 199 aResult = CRX_ERROR (); 194 CRX_PRINT_ERROR (aSentSize, "failed to send (" << CRX_ERROR () << ")"); 200 195 break; 201 196 } -
trunk/src/makefile
r11 r14 62 62 dummy: 63 63 64 test: $(TARGET)64 test: all 65 65 $(TARGET) 66 66
Note:
See TracChangeset
for help on using the changeset viewer.