Fixed some bugs dealing with non-blocking sockets

This commit is contained in:
Charles Lohr
2004-07-04 05:03:44 +00:00
parent 57fcd732fd
commit 34075a1162
+8 -7
View File
@@ -152,6 +152,8 @@ bool EzSockets::connect( const std::string& host, unsigned short port )
addr.sin_addr = *( (LPIN_ADDR)*phe->h_addr_list ); addr.sin_addr = *( (LPIN_ADDR)*phe->h_addr_list );
int desc = ::connect( sock, (struct sockaddr *)&addr, sizeof(addr) ); int desc = ::connect( sock, (struct sockaddr *)&addr, sizeof(addr) );
if (desc >=0)
state = skCONNECTED;
return desc >= 0; return desc >= 0;
#else #else
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@@ -159,6 +161,8 @@ bool EzSockets::connect( const std::string& host, unsigned short port )
inet_pton( AF_INET, host.c_str(), &addr.sin_addr ); inet_pton( AF_INET, host.c_str(), &addr.sin_addr );
int desc = ::connect( sock, (struct sockaddr *)&addr, sizeof(addr) ); int desc = ::connect( sock, (struct sockaddr *)&addr, sizeof(addr) );
if (desc >=0)
state = skCONNECTED;
return desc >= 0; return desc >= 0;
#endif #endif
} }
@@ -260,16 +264,12 @@ int EzSockets::PeekData(char *data, unsigned int bytes)
pUpdateRead(); pUpdateRead();
} }
int bytesRead = bytes; int bytesRead = bytes;
if (inBuffer.length()<bytes) if (inBuffer.length()<bytes)
bytesRead = inBuffer.length(); bytesRead = inBuffer.length();
memcpy(data,(inBuffer.substr(0,bytesRead)).c_str(),bytesRead); memcpy(data,(inBuffer.substr(0,bytesRead)).c_str(),bytesRead);
return bytesRead; return bytesRead;
} }
@@ -323,7 +323,7 @@ int EzSockets::PeekPack(char * data, unsigned int max)
if (inBuffer.length()>3) if (inBuffer.length()>3)
{ {
unsigned int size=0; unsigned int size=0;
PeekData((char*)size,4); PeekData((char*)&size,4);
size = ntohl(size); size = ntohl(size);
if (inBuffer.length()<(size+4)) if (inBuffer.length()<(size+4))
return (-1); return (-1);
@@ -389,8 +389,9 @@ int EzSockets::pUpdateRead()
if (bytes>0) if (bytes>0)
inBuffer.append(tempData,bytes); inBuffer.append(tempData,bytes);
//You cannot read 0 bytes! //You cannot read - bytes!
if (bytes<1) //0 bytes may happen under non-blocking circuimstances
if (bytes<0)
state = skERROR; state = skERROR;
return bytes; return bytes;