Exemplo n.º 1
0
void OutConnValidator::OnInput()
{
		m_ts.Update();
		
		assert(m_buf.Size() < 68);
		t_byte buf[68];
		size_t read_n = 68 - m_buf.Size();
		assert(read_n <= 68);

		int rn = m_stream_sock.Recv(buf, read_n);
		assert(rn <= 68);

		if(rn > 0)
		{
				m_buf.Insert(buf, rn);
				assert(m_buf.Size() <= 68);

				if(m_buf.Size() == 68)
				{
						check_connection_handshake();
						GetSelector()->RemoveHandler(this);
				}
		}else if(rn == -1 && NetGetLastError() == EWOULDBLOCK)
		{
				//等待下次
		}else
		{
				DEBUG_PRINT0("OutConnValidator::OnInput connection abort\n");
				//网络连接断开
				MsgSpace::PostMessageEx(m_task_id, new NetConnectPeerFailed(m_peer_entry));
				GetSelector()->RemoveHandler(this);
		}
}
Exemplo n.º 2
0
void OutConnValidator::OnOutput()
{
		m_ts.Update();
		
		assert(m_buf.Size() > 0);
		int wn = m_stream_sock.Send(m_buf.Data(),m_buf.Size());

		if(wn > 0)
		{
				m_buf.Erase(wn);
				if(m_buf.Size() == 0)
				{
						Mask(NetSpace::INPUT_MASK|NetSpace::TIMER_MASK);
				}
		}else if(wn == -1 && NetGetLastError() == EWOULDBLOCK)
		{
				//等待下一次可写
		}else
		{
				//失败
				DEBUG_PRINT0("OutConnValidator::OnOutput() failed\n");
				MsgSpace::PostMessageEx(m_task_id, new NetConnectPeerFailed(m_peer_entry));
				GetSelector()->RemoveHandler(this);
		}

}
Exemplo n.º 3
0
NetRetCode NetSocketListener::StartListening(NetCallBack functionPointer)
{
	// Ensure that we are bound 
	NetRetCode ret = NetOk;
	if ((ret = Bind()) != NetOk)
		return ret;

	NetIpAddress clientaddr;
	memset(&clientaddr, 0, sizeof(clientaddr));
		
	// Make sure incoming arguments make sense
	if (functionPointer == (NetCallBack)NULL)
	{
		return NetBadArgs;
	}
	
	// Signal that we should be listening
	m_listening = true;

	NetUdpPacket *packet = new NetUdpPacket();
	
	while (m_listening)
	{
		NetSocketLenType clientAddrLen = sizeof(packet->m_clientAddress);
		
		packet->m_length = recvfrom(m_sockfd, packet->m_data, MAX_PACKET_SIZE, 0, 
			                        (struct sockaddr *)&packet->m_clientAddress, &clientAddrLen);
		            
	    if(packet->m_length <= 0)
	    {   
            int errorCode = NetGetLastError();            
	        DebugOutNetSocketError( NetGetLastError() );
			// Cycle round and reuse the packet
	    }
	    else
	    {
			// Call function pointer with datagram data (type is NetUdpPacket) -
			// the function pointed to must free NetUdpPacket passed in
			(*functionPointer)(packet);
			packet = new NetUdpPacket();
	    }
	}
	
	delete packet;

	return NetOk;
}
Exemplo n.º 4
0
void InConnValidator::OnInput()
{
		m_ts.Update();
		t_byte buf[68];

		int read_len = 68 - m_buf.Size();
		
		assert(read_len <= 68);

		int rn = m_stream_sock.Recv(buf, read_len);

		if(rn > 0)
		{
				m_buf.Insert(buf, rn);
				assert(m_buf.Size() <= 68);

				if(m_buf.Size() == 68)
				{
						if(!check_connection_handshake())
						{
								//DEBUG_PRINT0("check_connection_handshake failed\n");
								GetSelector()->RemoveHandler(this);
								return;
						}else
						{
								assert(m_buf.Size() == 68);
								Mask(NetSpace::OUTPUT_MASK|NetSpace::TIMER_MASK);
						}
				}
		}else if(rn == -1 && NetGetLastError() == EWOULDBLOCK)
		{
				//等待下次;
		}else
		{
				DEBUG_PRINT0("accepted peer connection abort\n");
				GetSelector()->RemoveHandler(this);
		}
}
Exemplo n.º 5
0
void InConnValidator::OnOutput()
{
		m_ts.Update();
		
		int wn = m_stream_sock.Send(m_buf.Data(), m_buf.Size());
		
		if(wn > 0)
		{
				m_buf.Erase(wn);
				if(m_buf.IsEmpty())
				{
						MsgSpace::PostMessageEx(m_task_id, new NetConnEstablished(m_stream_sock.Duplicate(), REMOTE, m_peer_id, m_infohash, m_peer_entry, m_ext_info));
						GetSelector()->RemoveHandler(this);
				}
		}else if(wn == -1 && NetGetLastError() == EWOULDBLOCK)
		{
				//等待下次;
		}else
		{
				DEBUG_PRINT0("accepted peer connection abort\n");
				GetSelector()->RemoveHandler(this);
		}
}
// Write method using select
NetRetCode NetSocket::WriteData(void *bufAsVoid, int bufLen, int *numActualBytes)
{
	NetRetCode ret = NetOk;
	char *buf = (char *)bufAsVoid;
	int bytesLeft = bufLen;
	int bytesSent = 0;
	int err = 0;
	unsigned int timeout = 0;
	unsigned int timedout = 0;
	int haveAllData = 0;
	
	struct timeval timeVal;
	long timeoutSeconds = (long)((m_polltime*1000) / 100000);
	long timeoutUSeconds = (long)((m_polltime*1000) % 100000);
	
	while ((bytesLeft > 0) && (!timedout))
	{
		FD_ZERO(&m_listener);
		FD_SET(m_sockfd, &m_listener);
		timeVal.tv_sec  = timeoutSeconds;
		timeVal.tv_usec = timeoutUSeconds;
		haveAllData = -1;
		
		switch (select(m_sockfd + 1, (fd_set *)0, &m_listener, (fd_set *)0, &timeVal))
		{
			case NET_SOCKET_ERROR:
				NetDebugOut("WriteData select call failed");
				return NetFailed;
			case 0:
				if (numActualBytes)
				{
					haveAllData = ((*numActualBytes == bufLen) ? 1 : 0);
				}
				ret = CheckTimeout(&timeout, &timedout, haveAllData);
				continue;
			default:
				if (!FD_ISSET(m_sockfd, &m_listener))
				{
					return NetFailed;
				}
				
				bytesSent = sendto(m_sockfd, (char *)buf, bytesLeft, 0,
								(struct sockaddr *)&m_to, sizeof(m_to));
				err = NetGetLastError();
				if (NetIsSocketError(bytesSent) && NetIsReset(err))
				{
					bytesLeft = 0;
					shutdown(m_sockfd, NCSD_SEND);
					ret = NetClientDisconnect;
				}
				else if ((bytesSent > 0) || NetIsBlockingError(err))
				{
					bytesLeft -= bytesSent;
					buf += bytesSent;
					if (numActualBytes)
					{
						*numActualBytes += bytesSent;
						haveAllData = ((*numActualBytes == bufLen) ? 1 : 0);
					}
					ret = CheckTimeout(&timeout, &timedout, haveAllData);
				}
				else
				{
					NetDebugOut("WriteData write call failed");
					bytesLeft = 0;
					shutdown(m_sockfd, NCSD_SEND);
					ret = NetFailed;
				}
				break;
		}
	}
	
	return ret;
}