Exemple #1
0
bool RemoteConnect( void )
{
    int     baud_limit;     /* maximum baud that BOTH sides can achieve */
    byte    dummy;          /* hold values that we don't need here */
    byte    MaxBaud2;       /* MaxBaud at the other machine */

    SendBlkNo = ReceiveBlkNo = 0;
    LastResponse = SDATA_NAK;
    if( !SetSyncTime() )
        return( FALSE );
    /* establish baud limit */
#ifdef SERVER
    if( !WaitReceive( &dummy, 1, &MaxBaud2, SEC( 2 ) ) ) {
        return( FALSE );
    }
    if( !BlockSend( 1, (byte *)&MaxBaud, SEC( 2 ) ) ) {
        return( FALSE );
    }
#else
    if( !BlockSend( 1, (byte *)&MaxBaud, SEC( 2 ) ) ) {
        return( FALSE );
    }
    if( !WaitReceive( &dummy, 1, &MaxBaud2, SEC( 2 ) ) ) {
        return( FALSE );
    }
#endif
    /* MaxBaud2 now contains the other side's baud rate limit */
    if( MaxBaud > MaxBaud2 ) {
        baud_limit = MaxBaud;
    } else {
        baud_limit = MaxBaud2;
    }

    BaudCounter = baud_limit;
    if( !Speed() ) 
        return( FALSE );
#ifdef SERVER
    {
        char    buff[128];

        if( BaudCounter == MIN_BAUD ) {
            strcpy( buff, "pre-set" );
        } else {
            strcpy( buff, BaudTable[BaudCounter].name );
        }
        strcat( buff, " baud" );
        ServMessage( buff );
    }
#endif
    return( TRUE );
}
//--------------------------------------------------------------------------------
//	returns number of bytes wiating or SOCKET_ERROR if error
int	ofxUDPManager::PeekReceive()
{
	if (m_hSocket == INVALID_SOCKET){
		ofLogError("INVALID_SOCKET");
		return SOCKET_ERROR;
	}

	if (m_dwTimeoutReceive	!= NO_TIMEOUT){
		auto ret = WaitReceive(m_dwTimeoutReceive,0);
		if(ret!=0){
			return ret;
		}
	}

	//	we can use MSG_PEEK, but we still need a large buffer (udp protocol max is 64kb even if max for this socket is less)
	//	don't want a 64kb stack item here, so instead read how much can be read (note: not queue size, there may be more data-more packets)
	#ifdef TARGET_WIN32
			unsigned long size = 0;
			int retVal = ioctlsocket(m_hSocket,FIONREAD,&size);
	#else
			int size  = 0;
			int retVal = ioctl(m_hSocket,FIONREAD,&size);
	#endif
	
	//	error
	if ( retVal != 0 )
	{
		//assert( Result == SOCKET_ERROR );
		//	report error
		ofxNetworkCheckError();
		return SOCKET_ERROR;
	}

	return size;
}
Exemple #3
0
int Interpreteur( char *Buffer, struct Global *g)
{
  char arg0[80],arg1[80],arg2[80],arg3[80],arg4[80] ;
  int rc ;
  bzero(arg0,sizeof(arg1));
  bzero(arg1,sizeof(arg1));
  bzero(arg2,sizeof(arg2));
  bzero(arg3,sizeof(arg3));
  bzero(arg4,sizeof(arg4));
  fprintf(stderr,"Interpreteur ;") ;
  sscanf(Buffer,"%s %s %s %s %s\n",arg0,arg1,arg2,arg3,arg4) ;
  fprintf(stderr,"arg0 %s \n",arg0 ) ;

  /* ATTENTIO? NE PAS OUBLIER ELSE SINON ERREUR DE SYTAXE */
  if (strcmp(arg0,"connexion")==0)  rc=Connexion(Buffer,arg1,arg2,arg3, arg4,g) ;
  else 
  if (strcmp(arg0,"ip")==0 &&( strcmp(arg1,"address")==0) ) rc=IpAddress(Buffer,arg1,arg2,arg3, arg4,g) ;
  else
  if (strcmp(arg0,"netmask")==0 ) rc = Netmask(Buffer,arg1,arg2,arg3, arg4,g) ;
  else 
 if (strcmp(arg0,"gateway")==0 ) rc = Gateway(Buffer,arg1,arg2,arg3, arg4,g) ;
  else
 if (strcmp(arg0,"dns")==0 ) rc = Dns(Buffer,arg1,arg2,arg3, arg4,g) ;
 else
 if (strcmp(arg0,"ports")==0 ) rc = Ports(Buffer,arg1,arg2,arg3, arg4,g) ;
 else
 if (strcmp(arg0,"ping")==0 ) rc = Ping(Buffer,arg1,arg2,arg3, arg4,g) ;
 else
 if (strcmp(arg0,"send")==0 ) rc = Send(Buffer,arg1,arg2,arg3, arg4,g) ;
 else
 if (strcmp(arg0,"bind")==0)  rc = Bind(Buffer,arg1,arg2,arg3, arg4,g) ;
 else
 if (strcmp(arg0,"nslookup")==0 )  rc= NsLookup(Buffer,arg1,arg2,arg3, arg4,g) ;
 else
 if (strcmp(arg0,"waitreceive")==0 ) rc=WaitReceive(g) ;
 else
 if (strcmp(arg0,"read")==0) rc = LireMessage( g ) ;
 else
 if (strcmp(arg0,"exit")==0) exit(0);
 else
 if (strcmp(arg0,"ipconfig")==0) rc = AfficherParametres( g ) ;
 else
 if (strcmp(arg0,"help")==0) rc = Help() ;
 else
 if (Blanc1013(Buffer))
    {
     fprintf(stderr,"Ligne vide \n" ) ;
     rc =  1 ;
    }
 else
    rc = 0 ;
 fprintf(stderr,"fin Interpeteur\n") ;
 return(rc) ;
}
//--------------------------------------------------------------------------------
/// Return values:
/// SOCKET_TIMEOUT indicates timeout
/// SOCKET_ERROR in case of a problem.
///
int ofxTCPManager::Receive(char* pBuff, const int iSize)
{
    if (m_hSocket == INVALID_SOCKET) return(SOCKET_ERROR);

	if (m_dwTimeoutReceive	!= NO_TIMEOUT){
		auto ret = WaitReceive(m_dwTimeoutReceive,0);
		if(ret!=0){
			return ret;
		}
	}
	return recv(m_hSocket, pBuff, iSize, 0);
}
//--------------------------------------------------------------------------------
///	Return values:
///	SOCKET_TIMEOUT indicates timeout
///	SOCKET_ERROR in	case of	a problem.
int	ofxUDPManager::Receive(char* pBuff, const int iSize)
{
	if (m_hSocket == INVALID_SOCKET){
		ofLogError("ofxUDPManager") << "INVALID_SOCKET";
		return(SOCKET_ERROR);

	}

	if (m_dwTimeoutReceive	!= NO_TIMEOUT){
		auto ret = WaitReceive(m_dwTimeoutReceive,0);
		if(ret!=0){
			return ret;
		}
	}

	#ifndef TARGET_WIN32
		socklen_t nLen= sizeof(sockaddr);
	#else
		int	nLen= sizeof(sockaddr);
	#endif

	int	ret=0;

	memset(pBuff, 0, iSize);
	ret= recvfrom(m_hSocket, pBuff,	iSize, 0, (sockaddr *)&saClient, &nLen);

	if (ret	> 0)
	{
		//ofLogNotice("ofxUDPManager") << "received from: " << inet_ntoa((in_addr)saClient.sin_addr);
		canGetRemoteAddress= true;
	}
	else
	{
		canGetRemoteAddress = false;

		//	if the network error is WOULDBLOCK, then return 0 instead of SOCKET_ERROR as it's not really a problem, just no data.
		int SocketError = ofxNetworkCheckError();
		if ( SocketError == OFXNETWORK_ERROR(WOULDBLOCK) )
			return 0;
	}

	return ret;
	//	return(recvfrom(m_hSocket, pBuff, iSize, 0));
}
Exemple #6
0
trap_retval RemoteGet( byte *rec, trap_elen max_len )
{
    unsigned        timeout;             /* time limit for getting the data */
    unsigned char   err;                 /* storing the # of Errors the other side
                                            experience in sending data block */
    int             result;              /* result of WaitReceive() operation */

    timeout = FOREVER;

    /* Get data block */
    result = WaitReceive( &err, max_len, rec, timeout );
    if( !result )
        return( 0 );

    if( err > MAX_ERRORS ) {    /* too many Errors */
        ReSync();
        BytesReceived = 0;
    }
    return( BytesReceived );
}
//--------------------------------------------------------------------------------
/// Return values:
/// SOCKET_TIMEOUT indicates timeout
/// SOCKET_ERROR in case of a problem.
int ofxTCPManager::ReceiveAll(char* pBuff, const int iSize)
{
	if (m_hSocket == INVALID_SOCKET) return(SOCKET_ERROR);

	auto timestamp = ofGetElapsedTimeMicros();
	auto timeleftSecs = m_dwTimeoutReceive;
	auto timeleftMicros = 0;
	int totalBytes=0;

	do {
		if (m_dwTimeoutReceive	!= NO_TIMEOUT){
			auto ret = WaitReceive(timeleftSecs, timeleftMicros);
			if(ret!=0){
				return ret;
			}
		}
		int ret = recv(m_hSocket, pBuff+totalBytes, iSize-totalBytes, 0);
		if (ret==0 && totalBytes != iSize){
			return SOCKET_ERROR;
		}
		if (ret < 0){
			return SOCKET_ERROR;
		}
		totalBytes += ret;

		if (m_dwTimeoutReceive	!= NO_TIMEOUT){
			auto now = ofGetElapsedTimeMicros();
			auto diff = now - timestamp;
			if(diff > m_dwTimeoutReceive){
				return SOCKET_TIMEOUT;
			}
			float timeFloat = m_dwTimeoutSend - diff/1000000.;
			timeleftSecs = timeFloat;
			timeleftMicros = (timeFloat - timeleftSecs) * 1000000;
		}
	}while(totalBytes < iSize);

	return totalBytes;
}