Skip to content

Commit

Permalink
Remove SLEEP define. in linux os, sleep microsecond and second is dif…
Browse files Browse the repository at this point in the history
…ferent function. and have no good idea to sleep 500ms.

Add RecvTimeout function. if need timeout/block receive, need call this function. for block call, set timeout value to 0.
  • Loading branch information
wcb33 committed Apr 27, 2020
1 parent 24c48cf commit bbbcd4d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 34 deletions.
3 changes: 0 additions & 3 deletions univ/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
#ifdef _WINDOWS
#pragma warning( disable:4251 )
#pragma warning(disable:4996)
#define SLEEP(t) Sleep(t)

#else
#define SLEEP(t) sleep((double)t/1000.0)
#define INVALID_SOCKET -1
#define closesocket close
#ifndef LPCWSTR
Expand Down
52 changes: 49 additions & 3 deletions univ/univ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ int Sloong::Universal::CUniversal::SendEx(SOCKET sock, const char * buf, int nSi
{
nSentSize = send(sock, buf + nSize - nNosendSize, nNosendSize, 0);

#define SOCKET_ERROR (-1)
if (nSentSize == SOCKET_ERROR)
if (nSentSize < 0)
{
#ifdef _WINDOWS
return -1;
Expand Down Expand Up @@ -394,7 +393,54 @@ int Sloong::Universal::CUniversal::SendEx(SOCKET sock, const char * buf, int nSi
return nAllSent;
}

int Sloong::Universal::CUniversal::RecvEx(int sock, char * buf, int nSize, int nTimeout, bool bAgain)
int Sloong::Universal::CUniversal::RecvEx(SOCKET sock, char * buf, int nSize, bool bAgain)
{
if (nSize <= 0)
return 0;

int nIsRecv = 0;
int nNoRecv = nSize;
int nRecv = 0;
char* pBuf = buf;
while (nIsRecv < nSize)
{
nRecv = recv(sock, pBuf + nSize - nNoRecv, nNoRecv, 0);
if (nRecv < 0)
{
#ifdef _WINDOWS
return -1;
#else
// On non-blocking mode, socket will make EAGAIN and EINTR two erros,
// but these two erros should no be returned directly.
if (errno == EAGAIN || errno == EINTR)
{
// If bAgain as true, and was receiving data, retry again.
if (bAgain == true && nIsRecv > 0)
continue;
else if (bAgain == false && nIsRecv > 0)
return nIsRecv;
else
return 0-errno;
}
// In other erros case, return directly.
else
{
return 0-errno;
}
#endif // _WINDOWS
}
// socket is closed
else if ( nRecv == 0 )
{
return -200;
}
nNoRecv -= nRecv;
nIsRecv += nRecv;
}
return nIsRecv;
}

int Sloong::Universal::CUniversal::RecvTimeout(SOCKET sock, char * buf, int nSize, int nTimeout, bool bAgain)
{
if (nSize <= 0)
return 0;
Expand Down
26 changes: 23 additions & 3 deletions univ/univ.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ namespace Sloong
static int SendEx(SOCKET sock, const char* buf, int nSize, int nStart=0, bool bAgain=false);
/************************************************************************/
/* ReceEx function.
Params:
sock -> the socket handle
buf -> the data buffer
nSize -> the receive size
bAgagin -> continue when the EINTR,EAGAIN error if value is true.
else return direct. in default is false. *Only LinuxOS
Return:
> 0 & = nSize : Send succeed, return value is the recv data length.
> 0 & < nSize : Receive partial success. return received data length. it less than nSize and bigger than 0.
= 0 : socket is closed. (recv function return 0)
-1 - -199 : recv function return an error. the value is Negative of the errno.
Note:
If 'bAgain' as true, recved some data, and in next time happened EINTR\EAGAIN error, function will into a loop until all data received or other error happened.
*/
/************************************************************************/
static int RecvEx(SOCKET sock, char* buf, int nSize, bool bAgain = false );

/************************************************************************/
/* ReceTimout function.
Params:
sock -> the socket handle
buf -> the data buffer
Expand All @@ -144,16 +164,16 @@ namespace Sloong
Return:
> 0 & = nSize : Send succeed, return value is the recv data length.
> 0 & < nSize : Receive partial success. return received data length. it less than nSize and bigger than 0.
= 0 : Timeout.(select function return 0)
= 0 : socket is closed. (recv function return 0)
-1 - -199 : recv function return an error. the value is Negative of the errno.
-200 : socket is closed. (recv function return 0)
-200 : Timeout.(select function return 0)
-201 - -400: select function return an error. the value is (-200-errno).
Note:
If 'bAgain' as true, recved some data, and in next time happened EINTR\EAGAIN error, function will into a loop until all data received or other error happened.
*/
/************************************************************************/
static int RecvEx(int sock, char* buf, int nSize, int nTimeout, bool bAgain = false );
static int RecvTimeout(SOCKET sock, char* buf, int nSize, int nTimeout, bool bAgain = false );

template<typename T>
static string ntos(T n)
Expand Down
48 changes: 24 additions & 24 deletions univ/version.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#ifndef VERSION_H
#define VERSION_H


// Version
#define VERSION_NUMBER 2,5,2,193
#define VERSION_FILEVERSION L"2.5.2.193"
#define VERSION_BUILDTIME L"2020/04/26"

#ifdef _DEBUG
#define VERSION_PRODUCTVERSION L"Ver.2.5 for Debug"
#define VERSION_FILEDESCRIPTION L"Sloong Universal Debug Libaray"
#define VERSION_PRODUCTNAME L"Sloong Universal Debug Libaray"
#else
#define VERSION_PRODUCTVERSION L"Ver.2.5")
#define VERSION_FILEDESCRIPTION L"Sloong Universal Libaray"
#define VERSION_PRODUCTNAME L"Sloong Universal Libaray"
#endif // _DEBUG
#define VERSION_INTERNALNAME L"Universal.dll")
#define VERSION_COMPANYNAME L"Sloong, Inc.")
#define VERSION_LEGALCOPYRIGHT L"Copyright (C) 2014-2020 Sloong, Inc."


#endif // !VERSION_H
#ifndef VERSION_H
#define VERSION_H


// Version
#define VERSION_NUMBER 2,5,2,194
#define VERSION_FILEVERSION L"2.5.2.194"
#define VERSION_BUILDTIME L"2020/04/26"

#ifdef _DEBUG
#define VERSION_PRODUCTVERSION L"Ver.2.5 for Debug"
#define VERSION_FILEDESCRIPTION L"Sloong Universal Debug Libaray"
#define VERSION_PRODUCTNAME L"Sloong Universal Debug Libaray"
#else
#define VERSION_PRODUCTVERSION L"Ver.2.5")
#define VERSION_FILEDESCRIPTION L"Sloong Universal Libaray"
#define VERSION_PRODUCTNAME L"Sloong Universal Libaray"
#endif // _DEBUG
#define VERSION_INTERNALNAME L"Universal.dll")
#define VERSION_COMPANYNAME L"Sloong, Inc.")
#define VERSION_LEGALCOPYRIGHT L"Copyright (C) 2014-2020 Sloong, Inc."


#endif // !VERSION_H
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.2.193
2.5.2.194

0 comments on commit bbbcd4d

Please sign in to comment.