Esempio n. 1
0
/*
 * Receives data and say who the sender is. (blocking function)
 */
bool CUdpSock::receivedFrom( uint8 *buffer, uint& len, CInetAddress& addr, bool throw_exception )
{
	// Receive incoming message
	sockaddr_in saddr;
	socklen_t saddrlen = sizeof(saddr);

	len = ::recvfrom( _Sock, (char*)buffer, len , 0, (sockaddr*)&saddr, &saddrlen );

	// If an error occurs, the saddr is not valid
	// When the remote socket is closed, get sender's address to know who is quitting
	addr.setSockAddr( &saddr );

	// Check for errors (after setting the address)
	if ( ((int)len) == SOCKET_ERROR )
	{
		if ( throw_exception )
			throw ESocket( "Cannot receive data" );
		return false;
	}

	_BytesReceived += len;
	if ( _Logging )
	{
		LNETL0_DEBUG( "LNETL0: Socket %d received %d bytes from %s", _Sock, len, addr.asString().c_str() );
	}
	return true;
}
void CDummyTcpSock::connect( const CInetAddress& addr )
{
	_RemoteAddr = addr;
	_Sock = 100;

	_BytesReceived = 0;
	_BytesSent = 0;

	//CSynchronized<bool>::CAccessor sync( &_SyncConnected );
	//sync.value() = true;
	_Connected = true;

	LNETL0_DEBUG( "LNETL0: Socket connected to %s", addr.asString().c_str() );
}
Esempio n. 3
0
void CClient::updateStat ()
{
	// write each pong in a file
	string ha = Address.hostName();
	if (ha.empty())
	{
		ha = Address.ipAddress();
	}
	string fn = StatPathName + ConnectionName + "_" + ha + "_" + getDate() + ".stat";
	
	string line;
	line += "NbPing " + toString(NbPing) + " ";
	line += "NbPong " + toString(NbPong) + " ";
	if (NbPong == 0)
		line += "MeanPongTime <Undef> ";
	else
		line += "MeanPongTime " + toString(MeanPongTime/NbPong) + " ";
	line += "NbDuplicated " + toString(NbDuplicated) + " ";

	FILE *fp = fopen (fn.c_str(), "at");
	if (fp == NULL)
	{
		nlwarning ("Can't open stat file name '%s'", fn.c_str());
	}
	else
	{
		if (FirstWrite)
		{
			//nlassert (!Address.hostName().empty())
			fprintf (fp, "HostAddress: %s\n", Address.asString().c_str());
			FirstWrite = false;
		}
		
		fprintf (fp, "%s\n", line.c_str());
		fclose (fp);
	}

	nlinfo (line.c_str());

	CMessage msgout("INFO");
	msgout.serial(line);
	CallbackServer->send (msgout, From);

	NbPing = NbPong = MeanPongTime = NbDuplicated = 0;
}
Esempio n. 4
0
/*
 * Sends a message
 */
void CUdpSock::sendTo( const uint8 *buffer, uint len, const CInetAddress& addr )
{

	//  Send
	if ( ::sendto( _Sock, (const char*)buffer, len, 0, (sockaddr*)(addr.sockAddr()), sizeof(sockaddr) ) != (sint32)len )
	{
		throw ESocket( "Unable to send datagram" );
	}
	_BytesSent += len;

	if ( _Logging )
	{
		LNETL0_DEBUG( "LNETL0: Socket %d sent %d bytes to %s", _Sock, len, addr.asString().c_str() );
	}

	// If socket is unbound, retrieve local address
	if ( ! _Bound )
	{
		setLocalAddress();
		_Bound = true;
	}

#ifdef NL_OS_WINDOWS
	// temporary by ace to know size of SO_MAX_MSG_SIZE
	static bool first = true;
	if (first)
	{
		uint MMS, SB;
		int  size = sizeof (MMS);
		getsockopt (_Sock, SOL_SOCKET, SO_SNDBUF, (char *)&SB, &size);
		getsockopt (_Sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&MMS, &size);
		LNETL0_INFO ("LNETL0: The udp SO_MAX_MSG_SIZE=%u, SO_SNDBUF=%u", MMS, SB);
		first = false;
	}
#endif
}