Skip to content

Commit

Permalink
Fix ofx network (#6675)
Browse files Browse the repository at this point in the history
#changelog #network #addon
  • Loading branch information
oxillo committed Jan 31, 2021
1 parent c828d52 commit 0f4a403
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 78 deletions.
21 changes: 16 additions & 5 deletions addons/ofxNetwork/src/ofxNetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
#include "ofUtils.h"



int ofxNetworkCheckErrno(const char* file, int line) {
//-----------------------------------------------------------------
int ofxNetworkGetLastError(){
#ifdef TARGET_WIN32
int err = WSAGetLastError();
return WSAGetLastError();
#else
int err = errno;
return errno;
#endif
}


//-----------------------------------------------------------------
void ofxNetworkLogError(int err, const char* file, int line){
switch(err){
case 0:
break;
Expand Down Expand Up @@ -111,6 +116,12 @@ int ofxNetworkCheckErrno(const char* file, int line) {
ofLogVerbose("ofxNetwork") << file << ": " << line << " unknown error: " << err << " see errno.h for description of the error";
break;
}
}


//-----------------------------------------------------------------
int ofxNetworkCheckErrno(const char* file, int line) {
int err = ofxNetworkGetLastError();
ofxNetworkLogError( err, file, line );
return err;
}
}
40 changes: 39 additions & 1 deletion addons/ofxNetwork/src/ofxNetworkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,44 @@
#define OFXNETWORK_ERROR(name) E ## name
#endif

/**
* @brief Log the latest network error and where it happened (file and line)
*
*/
#define ofxNetworkCheckError() ofxNetworkCheckErrno(__FILE__, __LINE__)

int ofxNetworkCheckErrno(const char* file, int line);
/**
* @brief Log the latest network error and where it happened (file and line)
*
*/
#define ofxNetworkLogLastError() ofxNetworkLogError(ofxNetworkGetLastError(), __FILE__, __LINE__)

/**
* @brief returns error code from the last network function
*
* @return int the last network error
*/
int ofxNetworkGetLastError();


/**
* @brief Logs the network error err using ofLogError.
*
* @param err the network error
* @param file the file where the error happened, generally __FILE__
* @param line the line where the error happened, generally __LINE__
*/
void ofxNetworkLogError(int err, const char* file=__FILE__, int line=__LINE__-1);

/**
* @brief Logs the last network error and returns it;
*
* @deprecated use ofxNetworkLogError(ofxNetworkGetLastError(), file, line) instead
*
* @param file the file where the error happened, generally __FILE__
* @param line the line where the error happened, generally __LINE__
* @return int the last network error
*/
OF_DEPRECATED_MSG("use ofxNetworkLogError(ofxNetworkGetLastError(), file, line) instead", int ofxNetworkCheckErrno(const char* file, int line) );


32 changes: 16 additions & 16 deletions addons/ofxNetwork/src/ofxTCPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ bool ofxTCPClient::send(string message){
message = partialPrevMsg + message + messageDelimiter;
message += (char)0; //for flash
int ret = TCPClient.SendAll( message.c_str(), message.length() );
int errorCode = 0;
if(ret<0) errorCode = ofxNetworkCheckError();
int errorCode = ofxNetworkGetLastError();
if( ret<0 ) ofxNetworkLogError(errorCode);
if( isClosingCondition(ret, errorCode) ){
ofLogWarning("ofxTCPClient") << "send(): client disconnected";
close();
Expand Down Expand Up @@ -161,8 +161,8 @@ bool ofxTCPClient::sendRawMsg(const char * msg, int size){
tmpBuffSend.append(messageDelimiter.c_str(),messageDelimiter.size());

int ret = TCPClient.SendAll( tmpBuffSend.getData(), tmpBuffSend.size() );
int errorCode = 0;
if(ret<0) errorCode = ofxNetworkCheckError();
int errorCode = ofxNetworkGetLastError();
if( ret<0 ) ofxNetworkLogError(errorCode);
if( isClosingCondition(ret, errorCode) ){
ofLogWarning("ofxTCPClient") << "sendRawMsg(): client disconnected";
close();
Expand All @@ -187,8 +187,8 @@ bool ofxTCPClient::sendRawMsg(const char * msg, int size){
bool ofxTCPClient::sendRaw(string message){
if( message.length() == 0) return false;
int ret = TCPClient.SendAll(message.c_str(), message.length());
int errorCode = 0;
if(ret<0) errorCode = ofxNetworkCheckError();
int errorCode = ofxNetworkGetLastError();
if( ret<0 ) ofxNetworkLogError(errorCode);
if( isClosingCondition(ret, errorCode) ){
ofLogError("ofxTCPClient") << "sendRawBytes(): sending failed";
close();
Expand All @@ -202,8 +202,8 @@ bool ofxTCPClient::sendRaw(string message){
bool ofxTCPClient::sendRawBytes(const char* rawBytes, const int numBytes){
if( numBytes <= 0) return false;
int ret = TCPClient.SendAll(rawBytes, numBytes);
int errorCode = 0;
if(ret<0) errorCode = ofxNetworkCheckError();
int errorCode = ofxNetworkGetLastError();
if( ret<0 ) ofxNetworkLogError(errorCode);
if( isClosingCondition(ret, errorCode) ){
ofLogError("ofxTCPClient") << "sendRawBytes(): sending failed";
close();
Expand Down Expand Up @@ -255,8 +255,8 @@ string ofxTCPClient::receive(){
}

// check for connection reset or disconnection
int errorCode = 0;
if(length<0) errorCode = ofxNetworkCheckError();
int errorCode = ofxNetworkGetLastError();
if( length<0 ) ofxNetworkLogError(errorCode);
if(isClosingCondition(length,errorCode)){
close();
if(tmpStr.length()==0) {
Expand Down Expand Up @@ -323,8 +323,8 @@ int ofxTCPClient::receiveRawMsg(char * receiveBuffer, int numBytes){
//--------------------------
int ofxTCPClient::receiveRawBytes(char * receiveBuffer, int numBytes){
messageSize = TCPClient.Receive(receiveBuffer, numBytes);
int errorCode = 0;
if(messageSize<0) errorCode = ofxNetworkCheckError();
int errorCode = ofxNetworkGetLastError();
if(messageSize<0) ofxNetworkLogError(errorCode);
// 0 is not an error... -1 is
if(isClosingCondition(messageSize, errorCode)){
close();
Expand All @@ -335,8 +335,8 @@ int ofxTCPClient::receiveRawBytes(char * receiveBuffer, int numBytes){
//--------------------------
int ofxTCPClient::peekReceiveRawBytes(char * receiveBuffer, int numBytes){
messageSize = TCPClient.PeekReceive(receiveBuffer, numBytes);
int errorCode = 0;
if(messageSize<0) errorCode = ofxNetworkCheckError();
int errorCode = ofxNetworkGetLastError();
if(messageSize<0) ofxNetworkLogError(errorCode);
if(isClosingCondition(messageSize, errorCode)){
close();
}
Expand All @@ -346,8 +346,8 @@ int ofxTCPClient::peekReceiveRawBytes(char * receiveBuffer, int numBytes){
//--------------------------
string ofxTCPClient::receiveRaw(){
messageSize = TCPClient.Receive(tmpBuff, TCP_MAX_MSG_SIZE);
int errorCode = 0;
if(messageSize<0) errorCode = ofxNetworkCheckError();
int errorCode = ofxNetworkGetLastError();
if(messageSize<0) ofxNetworkLogError(errorCode);
if(isClosingCondition(messageSize, errorCode)){
close();
}else if(messageSize>=0 && messageSize<TCP_MAX_MSG_SIZE) {
Expand Down
62 changes: 33 additions & 29 deletions addons/ofxNetwork/src/ofxTCPManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ bool ofxTCPManager::Close()
#endif
{
// if it's reported we're not/no longer a socket, let it fall through and be invalidated
int Error = ofxNetworkCheckError();
if ( Error != OFXNETWORK_ERROR(NOTSOCK) )
{
int err = ofxNetworkGetLastError();
if( err != OFXNETWORK_ERROR(NOTSOCK) ){
ofxNetworkLogError( err, __FILE__, __LINE__-2 );
return(false);
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ bool ofxTCPManager::Create()

bool ret = (m_hSocket != INVALID_SOCKET);

if(!ret) ofxNetworkCheckError();
if(!ret) ofxNetworkLogLastError();

return ret;
}
Expand All @@ -141,7 +141,7 @@ bool ofxTCPManager::Listen(int iMaxConnections)
if (m_hSocket == INVALID_SOCKET) return(false);
m_iMaxConnections = iMaxConnections;
bool ret = (listen(m_hSocket, iMaxConnections)!= SOCKET_ERROR);
if(!ret) ofxNetworkCheckError();
if(!ret) ofxNetworkLogLastError();
return ret;
}

Expand All @@ -158,13 +158,13 @@ bool ofxTCPManager::Bind(unsigned short usPort, bool bReuse)
if (bReuse) {
int enable = 1;
if (setsockopt(m_hSocket,SOL_SOCKET,SO_REUSEADDR,(char*)&enable,sizeof(int)) < 0){
ofxNetworkCheckError();
ofxNetworkLogLastError();
return false;
}
}

if (::bind(m_hSocket,(struct sockaddr*)&local,sizeof(local))){
ofxNetworkCheckError();
ofxNetworkLogLastError();
return false;
}
return true;
Expand All @@ -189,15 +189,15 @@ bool ofxTCPManager::Accept(ofxTCPManager& sConnect)
FD_SET(m_hSocket, &fd);
timeval tv= {(time_t)m_dwTimeoutAccept, 0};
if(select(0, &fd, NULL, NULL, &tv) == 0) {
ofxNetworkCheckError();
ofxNetworkLogLastError();
return(false);
}
}

iSize= sizeof(sockaddr_in);
sConnect.m_hSocket = accept(m_hSocket, (sockaddr*)&addr, &iSize);
bool ret = (sConnect.m_hSocket != INVALID_SOCKET);
if(!ret && !m_closing) ofxNetworkCheckError();
if(!ret && !m_closing) ofxNetworkLogLastError();
return ret;
}

Expand Down Expand Up @@ -226,20 +226,24 @@ bool ofxTCPManager::Connect(const char *pAddrStr, unsigned short usPort)
}

int ret = connect(m_hSocket, (sockaddr *)&addr_in, sizeof(sockaddr));
int err = 0;
if(ret<0) err = ofxNetworkCheckError();
// set a timeout
if (ret < 0 && (err == OFXNETWORK_ERROR(INPROGRESS) || err == OFXNETWORK_ERROR(WOULDBLOCK)) && m_dwTimeoutConnect != NO_TIMEOUT) {
ret = WaitSend(m_dwTimeoutConnect, 0);
if(ret == 0) {
socklen_t len = sizeof err;
if (getsockopt(m_hSocket, SOL_SOCKET, SO_ERROR, (char*)&err, &len)<0){
ret = SOCKET_ERROR;
}else if(err != 0) {
ret = SOCKET_ERROR;
}
int err = ofxNetworkGetLastError(); int errline=__LINE__;
if( ret < 0 ){
// set a timeout
if ((err == OFXNETWORK_ERROR(INPROGRESS) || err == OFXNETWORK_ERROR(WOULDBLOCK)) && m_dwTimeoutConnect != NO_TIMEOUT) {
ret = WaitSend(m_dwTimeoutConnect, 0);
if(ret == 0) {
socklen_t len = sizeof err;
if (getsockopt(m_hSocket, SOL_SOCKET, SO_ERROR, (char*)&err, &len)<0){
ret = SOCKET_ERROR;
}else if(err != 0) {
ret = SOCKET_ERROR;
}
}
}else{
ofxNetworkLogError( err, __FILE__, errline );
}
}
}


if(m_dwTimeoutConnect != NO_TIMEOUT){
SetNonBlocking(wasBlocking);
Expand Down Expand Up @@ -312,7 +316,7 @@ bool ofxTCPManager::SetNonBlocking(bool useNonBlocking)

bool ret = (retVal >= 0);
if(!ret){
ofxNetworkCheckError();
ofxNetworkLogLastError();
nonBlocking = prevNonBlocking;
}

Expand Down Expand Up @@ -500,7 +504,7 @@ bool ofxTCPManager::GetRemoteAddr(LPINETADDR pInetAddr)

iSize= sizeof(sockaddr);
bool ret = (getpeername(m_hSocket, (sockaddr *)pInetAddr, &iSize) != SOCKET_ERROR);
if(!ret) ofxNetworkCheckError();
if(!ret) ofxNetworkLogLastError();
return ret;
}

Expand All @@ -517,7 +521,7 @@ bool ofxTCPManager::GetInetAddr(LPINETADDR pInetAddr)

iSize= sizeof(sockaddr);
bool ret = (getsockname(m_hSocket, (sockaddr *)pInetAddr, &iSize) != SOCKET_ERROR);
if(!ret) ofxNetworkCheckError();
if(!ret) ofxNetworkLogLastError();
return ret;
}

Expand Down Expand Up @@ -558,7 +562,7 @@ int ofxTCPManager::GetReceiveBufferSize() {
int sizeBuffer=0;
size = sizeof(int);
int ret = getsockopt(m_hSocket, SOL_SOCKET, SO_RCVBUF, (char*)&sizeBuffer, &size);
if(ret==-1) ofxNetworkCheckError();
if(ret==-1) ofxNetworkLogLastError();
return sizeBuffer;
}

Expand All @@ -568,7 +572,7 @@ bool ofxTCPManager::SetReceiveBufferSize(int sizeInByte) {
if ( setsockopt(m_hSocket, SOL_SOCKET, SO_RCVBUF, (char*)&sizeInByte, sizeof(sizeInByte)) == 0){
return true;
}else{
ofxNetworkCheckError();
ofxNetworkLogLastError();
return false;
}
}
Expand All @@ -585,7 +589,7 @@ int ofxTCPManager::GetSendBufferSize() {
int sizeBuffer=0;
size = sizeof(int);
int ret = getsockopt(m_hSocket, SOL_SOCKET, SO_SNDBUF, (char*)&sizeBuffer, &size);
if(ret==-1) ofxNetworkCheckError();
if(ret==-1) ofxNetworkLogLastError();
return sizeBuffer;
}

Expand All @@ -595,7 +599,7 @@ bool ofxTCPManager::SetSendBufferSize(int sizeInByte) {
if ( setsockopt(m_hSocket, SOL_SOCKET, SO_SNDBUF, (char*)&sizeInByte, sizeof(sizeInByte)) == 0){
return true;
}else{
ofxNetworkCheckError();
ofxNetworkLogLastError();
return false;
}
}
Expand Down
Loading

0 comments on commit 0f4a403

Please sign in to comment.