Exemplo n.º 1
0
void CUDTSocketListner::Process()
{
	for(;;) // repeat untill all pending connections are accepted
	{
		/*timeval tv;
		tv.tv_sec = 0;
		tv.tv_usec = 0;
		UDT::UDSET readfds;
		UD_ZERO(&readfds);
		UD_SET(m_Server, &readfds);
		int res	= UDT::select(0, &readfds, NULL, NULL, &tv);
		if (!((res != UDT::ERROR) && (UD_ISSET(m_Server, &readfds))))
			break;*/
		
		sockaddr_in6 sa; // sockaddr_in is smaller
		int sa_len = sizeof(sa); 
		UDTSOCKET Client = UDT::accept(m_Server, (sockaddr*)&sa, &sa_len);
		if (UDT::INVALID_SOCK == Client)
		{
			LogLine(LOG_ERROR, L"accept: %S", UDT::getlasterror().getErrorMessage());
			break;
		}
		else if(Client == NULL)
			break;
		
		ConfigSocket(Client);

		uint64_t SendKey = 0;
		int KeySize = sizeof(SendKey);
		UDT::getsockopt(Client, 0, UDT_SENDKEY, &SendKey, &KeySize);

		CSafeAddress Address((sockaddr*)&sa, sa_len, sa_len == sizeof(sockaddr_in) ? CSafeAddress::eUDT_IP4 : CSafeAddress::eUDT_IP6);
		Address.SetPassKey(SendKey);
		GetParent<CSmartSocket>()->AddSessions(Address, new CUDTSocketSession(this, Client, Address));
	}

	const uint64 Size = 0xFFFF;
	char Buffer[Size];
	for(;;) // repeat untill all data is read
	{
		sockaddr_in6 sa; // sockaddr_in is smaller
		int sa_len = sizeof(sa); 
		uint64_t RecvKey = 0;
		int Recived = UDT::recvfrom(m_Server, Buffer, Size, (sockaddr*)&sa, &sa_len, &RecvKey);
		if (UDT::ERROR == Recived)
		{
			LogLine(LOG_ERROR, L"recvfrom: %S", UDT::getlasterror().getErrorMessage());
			break;
		}
		else if(Recived == 0)
			break; // nothing more to be recived
		CBuffer Packet(Buffer, Recived, true);

		CSafeAddress Address((sockaddr*)&sa, sa_len, sa_len == sizeof(sockaddr_in) ? CSafeAddress::eUDT_IP4 : CSafeAddress::eUDT_IP6);
		Address.SetPassKey(RecvKey);
		ReceiveFrom(Packet, Address);
	}
}
Exemplo n.º 2
0
CSocketSession* CUDTSocketListner::CreateSession(const CSafeAddress& Address, bool bRendevouz, bool bEmpty)
{
	if(bEmpty)
		return new CUDTSocketSession(this, UDT::INVALID_SOCK, Address);

	sockaddr_in6 sa; // sockaddr_in is smaller
	int sa_len = sizeof(sa);
	Address.ToSA((sockaddr*)&sa, &sa_len);

	UDTSOCKET Client = UDT::socket(m_bIPv6 ? AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_IP);

	CSmartSocket* pSocket = GetParent<CSmartSocket>();
	if(uint64 RecvKey = pSocket->GetRecvKey())
	{
		UDT::setsockopt(Client, 0, UDT_RECVKEY, &RecvKey, sizeof(RecvKey));
		bool Obfuscate = true;
		UDT::setsockopt(Client, 0, UDT_OBFUSCATE, &Obfuscate, sizeof(Obfuscate));
	}
	if(uint64 SendKey = Address.GetPassKey())
		UDT::setsockopt(Client, 0, UDT_SENDKEY, &SendKey, sizeof(SendKey));

	ConfigSocket(Client);
	
	bool reuse_addr = true; // Note: this is true by default anyways
	UDT::setsockopt(Client, 0, UDT_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
	if(bRendevouz)
	{
		bool value = true;
		UDT::setsockopt(Client, 0, UDT_RENDEZVOUS, &value, sizeof(value));
	}
	if (UDT::ERROR == UDT::bind(Client, m_sa, m_bIPv6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in)))
	{
		LogLine(LOG_ERROR, L"bind: %S", UDT::getlasterror().getErrorMessage());
		return NULL;
	}
	if (UDT::ERROR == UDT::connect(Client, (sockaddr*)&sa, sa_len))
	{
		LogLine(LOG_ERROR, L"connect: %S", UDT::getlasterror().getErrorMessage());
		return NULL;
	}

	return new CUDTSocketSession(this, Client, Address);
}
Exemplo n.º 3
0
BOOL CSocketIHM::bReadConfig(LPCTSTR pszFileName,CListStream *pListStream)
{
	BOOL bReturn = TRUE;
	unsigned short nNumPort;
	long lSizeDataMax;


	bReturn = CThreadInterface::bReadConfig(pszFileName);
	// taille du buffer
	lSizeDataMax = iGetPrivateProfileInt(_T("Config"),_T("m_lSizeDataMax"),1024,pszFileName);
	// port TCP/IP
	nNumPort = iGetPrivateProfileInt(_T("Config"),_T("m_nNumPort"),8192,pszFileName);
	ConfigSocket(nNumPort,lSizeDataMax,_T("localhost"));
	
	if (bReturn) bReturn = m_ElemCycleStep.bReadConfig(_T("0"),pszFileName,pListStream);

	if (!bReturn) 
	{
		TRACE_DEBUG(eDebug,eConfig,_T(__FILE__),_T(__FUNCTION__),__LINE__,_T("BOOL CSocketIHM::bReadConfig"));
		TRACE_DEBUG_IHM(eError,eConfig,eErrorSocketReadConfig);
	}

	return (bReturn);
}