Exemplo n.º 1
0
int Connections::CompleteConnect(DWORD TransferedBytes)
{
    FUNCTION_BEGIN();

    UNREFERENCED_PARAMETER(TransferedBytes);

    _socket.GetPeerAddr(_remoteAddr);
    _socket.GetLocalAddr(_localAddr);
    _IsConnected = true;

    int rc = _NetworkRef->Associate(this, true);
    if (0 != rc)
    {
        Logger::Log("Session[%X] CompleteConnect Error[%d]\n", this, rc);
        return rc;
    }

    SetStatus(CONNSTATUS_ACTIVE);
    _ConnectTime = GetTickCount();
    _ConnectorRef->OnConnected();
    _NetworkRef->PostNetEventMessage((WPARAM)NETEVENT_CONNECT, (LPARAM)this);


    rc = PostRecv();
    if (0 != rc)
    {
        return rc;
    }

    return 0;
}
Exemplo n.º 2
0
void Connections::Init()
{
    FUNCTION_BEGIN();

    _status = CONNSTATUS_INIT;

    _NetworkRef = NULL;
    _AcceptorRef = NULL;
    _ConnectorRef = NULL;
    _IoPostCount = 0;

    _recvBuffer.Clear();
    _sendBuffer.Clear();
    _recvContext.Clear();
    _sendContext.Clear();
    _recvContext.param = this;
    _sendContext.param = this;
    _PacketEncoder = NULL;

    _ControlFlag = 0;
    _AliveTime = 0;

    _IsSending = false;
    _IsDisconnect = false;
    _IsConnected = false;

    _ConnectTime = 0;
    _BytesRecvSize = 0;
    _BytesSendSize = 0;
    _BytesRecvSizeMax = 0;
    _BytesSendSizeMax = 0;
    _PacketRecvCount = 0;
    _PacketSendCount = 0;
}
Exemplo n.º 3
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Dispatcher TH, Application TH, Acceptor TH, Connector TH, 
//-----------------------------------------------------------------------------------
void CNtlConnection::Destroy()
{
	FUNCTION_BEGIN();

	if( false == ExchangeStatus( STATUS_DESTROY, false, STATUS_DESTROY ) )
	{
		NTL_LOG_ASSERT( "Status( STATUS_DESTROY )" );
		return;
	}


	m_socket.Shutdown( SD_BOTH );

	m_socket.Close();


	if( m_pAcceptorRef )
	{
		m_pAcceptorRef->OnDisconnected( m_bConnected );
	}

	if( m_pConnectorRef )
	{
		m_pConnectorRef->OnDisconnected( m_bConnected );
	}
}
Exemplo n.º 4
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
void CNtlConnection::Init()
{
	FUNCTION_BEGIN();

	m_status = STATUS_INIT;

	m_pNetworkRef	= NULL;
	m_pAcceptorRef	= NULL;
	m_pConnectorRef	= NULL;
	m_dwIoPostCount = 0;

	m_recvBuffer.Clear();
	m_sendBuffer.Clear();
	m_recvContext.Clear();
	m_sendContext.Clear();
	m_recvContext.param = this;
	m_sendContext.param = this;
	m_pPacketEncoder = NULL;

	m_dwControlFlag = 0;
	m_dwAliveTime = 0;

	m_bSending = false;
	m_bDisconnect = false;
	m_bConnected = false;

	m_dwConnectTime = 0;
	m_dwBytesRecvSize = 0;
	m_dwBytesSendSize = 0;
	m_dwBytesRecvSizeMax = 0;
	m_dwBytesSendSizeMax = 0;
	m_dwPacketRecvCount = 0;
	m_dwPacketSendCount = 0;
}
Exemplo n.º 5
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( CompleteAccept )
//-----------------------------------------------------------------------------------
void CNtlConnection::SetAddress(SOCKADDR_IN * pLocalAddr, SOCKADDR_IN * pRemoteAddr)
{
	FUNCTION_BEGIN();


	m_localAddr	= *pLocalAddr;
	m_remoteAddr = *pRemoteAddr;
}
Exemplo n.º 6
0
int Connections::Shutdown()
{
    FUNCTION_BEGIN();

    SetStatus(CONNSTATUS_SHUTDOWN);

    return 0;
}
Exemplo n.º 7
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Application TH, Monitor TH
//-----------------------------------------------------------------------------------
int CNtlConnection::Shutdown()
{
	FUNCTION_BEGIN();


	SetStatus( STATUS_SHUTDOWN );


	return NTL_SUCCESS;
}
Exemplo n.º 8
0
void Connections::Close(bool Force)
{
    UNREFERENCED_PARAMETER(Force);

    FUNCTION_BEGIN();

    if (false == ExchangeStatus(CONNSTATUS_CLOSE, false, CONNSTATUS_CLOSE))
    {
        return;
    }
}
Exemplo n.º 9
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( IOCP )
//-----------------------------------------------------------------------------------
int CNtlConnection::CompleteAccept(DWORD dwTransferedBytes)
{
	FUNCTION_BEGIN();

	UNREFERENCED_PARAMETER( dwTransferedBytes );

	SOCKADDR_IN * pLocalAddr = NULL;
	SOCKADDR_IN * pRemoteAddr = NULL;
	int nLocalAddrLen = 0;
	int nRemoteAddrLen = 0;

	m_socket.GetAcceptExSockaddrs(	m_recvContext.wsabuf.buf,
									0,
									sizeof(SOCKADDR_IN) + 16,
									sizeof(SOCKADDR_IN) + 16,
									(SOCKADDR**) &pLocalAddr,
									&nLocalAddrLen,
									(SOCKADDR**) &pRemoteAddr,
									&nRemoteAddrLen);

	SetAddress( pLocalAddr, pRemoteAddr );
	ZeroMemory( m_recvContext.wsabuf.buf, sizeof(SOCKADDR_IN) + 16 + sizeof(SOCKADDR_IN) + 16 );
	m_bConnected = true;

	int rc = m_pNetworkRef->Associate( this, true );
	if( NTL_SUCCESS != rc )
	{
		NTL_LOGDL( LOG_NETWORK, "Session[%X] Associate Error[%d:%s]", this, rc, NtlGetErrorMessage(rc) );
		return rc;
	}


	SetStatus( STATUS_ACTIVE );
	m_dwConnectTime = GetTickCount();


	m_pAcceptorRef->OnAccepted();


	m_pNetworkRef->PostNetEventMessage( (WPARAM)NETEVENT_ACCEPT, (LPARAM)this );


	rc = PostRecv();
	if( NTL_SUCCESS != rc )
	{
		return rc;
	}


	//NTL_PRINT(PRINT_SYSTEM, "Session[%X]\tCompleteAccept Called Local[%s:%u] Remote[%s:%u]", pSession, GetLocalIP(), GetLocalPort(), GetRemoteIP(), GetRemotePort());


	return NTL_SUCCESS;
}
Exemplo n.º 10
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( CompleteRecv, CompleteAccept )
//-----------------------------------------------------------------------------------
int CNtlConnection::PostRecv()
{
	FUNCTION_BEGIN();

	if( false == IsStatus( STATUS_ACTIVE ) )
	{
		Disconnect( false );
		return NTL_SUCCESS;
		//return NTL_ERR_NET_CONNECTION_STATUS_WRONG;
	}


	DWORD dwFlags = 0;
	DWORD dwTransferedBytes	= 0;

	m_recvContext.Reset();
	m_recvContext.iomode = IOMODE_RECV;
	m_recvContext.wsabuf.buf = (char*) m_recvBuffer.GetQueuePushPtr();
	m_recvContext.wsabuf.len = m_recvBuffer.GetPushAvailableSize();


	//  [1/20/2007 zeroera] : ¼³¸í : recv buffer overflow ÀÌ°÷¿¡ °É¸®¸é ÀûÀýÇÑ ¹öÇÁ »çÀÌÁ Á¶Á¤ÇÒ °ÍÀ» °í·ÁÇÏÁö¸¸ Fake PacketÀ» º¸³»´Â Ŭ¶óÀ̾ðÆ®¸¦ ´ëºñÇÏ¿© ÀûÀýÇÑ ´ëó¸¦ ÇÒ °Í
	if( m_recvBuffer.GetPushAvailableSize() <= 0 )
	{
		Disconnect( false );

		NTL_LOGDL( LOG_NETWORK, "Session[%X] m_recvBuffer.GetPushAvailableSize() <= 0, m_recvBuffer.GetPushAvailableSize() = %d", this, m_recvBuffer.GetPushAvailableSize() );
		return NTL_ERR_NET_SESSION_RECV_BUFFER_OVERFLOW;
	}



	IncreasePostIoCount();

	int rc = m_socket.RecvEx(	&m_recvContext.wsabuf,
		1,
		&dwTransferedBytes,
		&dwFlags,
		&m_recvContext);

	if( NTL_SUCCESS != rc )
	{
		DecreasePostIoCount();

		NTL_LOGDL( LOG_NETWORK, "Session[%X] RecvEx Function Failed (%d)%s", this, rc, NtlGetErrorMessage( rc ) );
		return rc;
	}

	//NTL_PRINT( PRINT_SYSTEM, "Session[%X]\tPostRecv Complete Success recvBufferSize[%d]", this, m_recvBuffer.GetCurSize() );


	return NTL_SUCCESS;
}
Exemplo n.º 11
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Connector TH ( Run )
//-----------------------------------------------------------------------------------
int CNtlConnection::PostConnect(CNtlConnector* pConnector)
{
	FUNCTION_BEGIN();


	if( NULL == pConnector )
	{
		return NTL_ERR_SYS_INPUT_PARAMETER_WRONG;
	}


	SetStatus( STATUS_CONNECT );


	m_pConnectorRef = pConnector;


#if !defined( __USE_CONNECTEX__ )
	int rc = m_socket.Connect( pConnector->GetConnectAddr() );
	if( NTL_SUCCESS != rc )
	{
		//NTL_LOGDL( LOG_NETWORK, "Session[%X] Connect Function Failed: (%d)%s", this, rc, NtlGetErrorMessage( rc ) );
		return rc;
	}

#else

	DWORD dwBytes = 0;

	m_recvContext.Reset();
	m_recvContext.iomode = IOMODE_CONNECT;
	m_recvContext.wsabuf.buf	= (char*) m_sendBuffer.GetQueuePushPtr();
	m_recvContext.wsabuf.len	= 0;
	IncreasePostIoCount();

	int rc = m_socket.ConnectEx(	pConnector->GetConnectAddr(),
		sizeof(struct sockaddr_in),
		m_recvContext.wsabuf.buf,
		0,
		&dwBytes,
		&m_sendContext );

	if( NTL_SUCCESS != rc )
	{
		DecreasePostIoCount();
		return rc;
	}

#endif

	return NTL_SUCCESS;
}
Exemplo n.º 12
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH, Application TH ( PostRecv/PostSend/IOCP transfered 0 )
//-----------------------------------------------------------------------------------
void CNtlConnection::Close(bool bForce)
{
	UNREFERENCED_PARAMETER( bForce ); 

	FUNCTION_BEGIN();

	if( false == ExchangeStatus( STATUS_CLOSE, false, STATUS_CLOSE ) )
	{
		return;
	}

	// ÀÌ ¹ØÀ¸·Î´Â ÁøÇà ºÒ°¡ÇÏ´Ù. Processor·Î ÀÎÇØ ¸Þ¸ð¸®¿¡¼­ ¼Ò¸ê‰Î
}
Exemplo n.º 13
0
int Connections::CompleteAccept(DWORD TransferedBytes)
{
    FUNCTION_BEGIN();

    UNREFERENCED_PARAMETER(TransferedBytes);

    SOCKADDR_IN* LocalAddr = NULL;
    SOCKADDR_IN* RemoteAddr = NULL;
    int LocalAddrLen = 0;
    int RemoteAddrLen = 0;

    _socket.GetAcceptExSockaddrs(_recvContext.wsabuf.buf,
                                 0,
                                 sizeof(SOCKADDR_IN) + 16,
                                 sizeof(SOCKADDR_IN) + 16,
                                 (SOCKADDR**)&LocalAddr,
                                 &LocalAddrLen,
                                 (SOCKADDR**)&RemoteAddr,
                                 &RemoteAddrLen);

    SetAddress(LocalAddr, RemoteAddr);
    ZeroMemory(_recvContext.wsabuf.buf, sizeof(SOCKADDR_IN) + 16 + sizeof(SOCKADDR_IN) + 16);
    _IsConnected = true;

    int rc = _NetworkRef->Associate(this, true);
    if (0 != rc)
    {
        Logger::Log("Session[%X] Associate Error[%d]\n", this, rc);
        return rc;
    }

    SetStatus(CONNSTATUS_ACTIVE);
    _ConnectTime = GetTickCount();

    Logger::Log("Client connected %s:%d [%d]\n", GetRemoteIP(), GetRemotePort(), this);

    _AcceptorRef->OnAccepted();

    _NetworkRef->PostNetEventMessage((WPARAM)NETEVENT_ACCEPT, (LPARAM)this);

    rc = PostRecv();
    if (0 != rc)
    {
        return rc;
    }

    return 0;
}
Exemplo n.º 14
0
int Connections::PostConnect(Connector* pConnector)
{
    FUNCTION_BEGIN();


    if (NULL == pConnector)
    {
        return 1;
    }

    SetStatus(CONNSTATUS_CONNECT);

    _ConnectorRef = pConnector;


#if !defined( __USE_CONNECTEX__ )
    int rc = _socket.Connect(pConnector->GetConnectAddr());
    if (0 != rc)
    {
        return rc;
    }
#else
    DWORD Bytes = 0;

    _recvContext.Reset();
    _recvContext.iomode = IOMODE_CONNECT;
    _recvContext.wsabuf.buf = (char*)_sendBuffer.GetQueuePushPtr();
    _recvContext.wsabuf.len = 0;
    IncreasePostIoCount();

    int rc = _socket.ConnectEx(pConnector->GetConnectAddr(),
                               sizeof(struct sockaddr_in),
                               _recvContext.wsabuf.buf,
                               0,
                               &Bytes,
                               &_sendContext);

    if (0 != rc)
    {
        DecreasePostIoCount();
        return rc;
    }

#endif

    return 0;
}
Exemplo n.º 15
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Acceptor TH ( Run )
//-----------------------------------------------------------------------------------
int CNtlConnection::PostAccept(CNtlAcceptor* pAcceptor)
{
	FUNCTION_BEGIN();

	if( NULL == pAcceptor )
	{
		return NTL_ERR_SYS_INPUT_PARAMETER_WRONG;
	}


	SetStatus( STATUS_ACCEPT );


	m_pAcceptorRef = pAcceptor;


	DWORD dwBytes = 0;

	m_recvContext.Reset();
	m_recvContext.iomode = IOMODE_ACCEPT;
	m_recvContext.wsabuf.buf = (char*) m_recvBuffer.GetQueuePushPtr();
	m_recvContext.wsabuf.len = 0;

	IncreasePostIoCount();
	m_pAcceptorRef->IncreaseCurAcceptingCount();


	int rc = pAcceptor->GetListenSocket().AcceptEx(	m_socket,
		m_recvContext.wsabuf.buf,
		0,
		sizeof(SOCKADDR_IN) + 16,
		sizeof(SOCKADDR_IN) + 16,
		&dwBytes,
		&m_recvContext );

	if( NTL_SUCCESS != rc )
	{
		DecreasePostIoCount();
		m_pAcceptorRef->DecreaseCurAcceptingCount();

		NTL_LOGDL( LOG_NETWORK, "Session[%X] AcceptEx Function Failed: (%d)%s", this, rc, NtlGetErrorMessage( rc ) );
		return rc;
	}


	return NTL_SUCCESS;
}
Exemplo n.º 16
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( CompleteRecv )
//-----------------------------------------------------------------------------------
bool CNtlConnection::PopPacket(CNtlPacket * pPacket)
{
	FUNCTION_BEGIN();

	if( NULL == pPacket )
	{
		NTL_LOG_ASSERT("NULL == pPacket");
		return false;
	}

#if 0
	WORD wPacketLength = 0;
	if( false == MakeSureCompletedPacket( &m_recvBuffer, &wPacketLength ) )
	{
		return false;
	}
	
	pPacket->Attach( m_recvBuffer.GetQueueWorkPtr(), wPacketLength );


	if( false == pPacket->IsValidPacket() )
	{
		return false;
	}


	m_recvBuffer.IncreaseWorkPos( pPacket->GetUsedSize() );

#else

	int nPacketLen = GetPacketLen( m_recvBuffer.GetQueuePopPtr() );

	pPacket->AttachData( m_recvBuffer.GetQueuePopPtr(), (WORD) (GetHeaderSize() + nPacketLen) );

	if( false == pPacket->IsValidPacket() )
	{
		return false;
	}


#endif


	return true;
}
Exemplo n.º 17
0
int Connections::Disconnect(bool Graceful)
{
    FUNCTION_BEGIN();

    if (Graceful)
    {
        _socket.Shutdown(SD_SEND);
    }
    else
    {
        _socket.Shutdown(SD_BOTH);
        _socket.Close();
    }

    _IsDisconnect = true;

    return 0;
}
Exemplo n.º 18
0
int Connections::PostSend()
{
    FUNCTION_BEGIN();

    if (false == IsStatus(CONNSTATUS_ACTIVE))
    {
        Disconnect(false);
        return 0;
    }


    DWORD Flags = 0;
    DWORD SendBytes = 0;

    _sendContext.Reset();
    _sendContext.iomode = IOMODE_SEND;
    _sendContext.wsabuf.buf = (char*)_sendBuffer.GetQueuePopPtr();

    if (_sendBuffer.GetPopPos() + _sendBuffer.GetCurSize() >= _sendBuffer.GetQueueSize())
    {
        _sendContext.wsabuf.len = _sendBuffer.GetQueueSize() - _sendBuffer.GetPopPos();
    }
    else
    {
        _sendContext.wsabuf.len = _sendBuffer.GetCurSize();
    }

    IncreasePostIoCount();

    int rc = _socket.SendEx(&_sendContext.wsabuf,
                            1,
                            &SendBytes,
                            Flags,
                            &_sendContext);

    if (0 != rc)
    {
        DecreasePostIoCount();
        Logger::Log("Session[%X] SendEx Function Failed: (%d)\n", this, rc);
        return rc;
    }

    return 0;
}
Exemplo n.º 19
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Application TH, Monitor TH
//-----------------------------------------------------------------------------------
int CNtlConnection::Disconnect(bool bGraceful)
{
	FUNCTION_BEGIN();

	if( bGraceful )
	{
		m_socket.Shutdown( SD_SEND );
	}
	else
	{
		m_socket.Shutdown( SD_BOTH );
		m_socket.Close();
	}

	m_bDisconnect = true;


	return NTL_SUCCESS;
}
Exemplo n.º 20
0
int Connections::PostAccept(Acceptor* pAcceptor)
{
    FUNCTION_BEGIN();

    if (NULL == pAcceptor)
    {
        return 1;
    }

    SetStatus(CONNSTATUS_ACCEPT);

    _AcceptorRef = pAcceptor;

    DWORD Bytes = 0;

    _recvContext.Reset();
    _recvContext.iomode = IOMODE_ACCEPT;
    _recvContext.wsabuf.buf = (char*)_recvBuffer.GetQueuePushPtr();
    _recvContext.wsabuf.len = 0;

    IncreasePostIoCount();
    _AcceptorRef->IncreaseCurAcceptingCount();


    int rc = pAcceptor->GetListenSocket().AcceptEx(_socket,
             _recvContext.wsabuf.buf,
             0,
             sizeof(SOCKADDR_IN) + 16,
             sizeof(SOCKADDR_IN) + 16,
             &Bytes,
             &_recvContext);

    if (0 != rc)
    {
        DecreasePostIoCount();
        _AcceptorRef->DecreaseCurAcceptingCount();
        Logger::Log("Session[%X] AcceptEx Function Failed: (%d)\n", this, rc);
        return rc;
    }

    return 0;
}
Exemplo n.º 21
0
int Connections::PostRecv()
{
    FUNCTION_BEGIN();

    if (false == IsStatus(CONNSTATUS_ACTIVE))
    {
        Disconnect(false);
        return 0;
    }

    DWORD Flags = 0;
    DWORD TransferedBytes = 0;

    _recvContext.Reset();
    _recvContext.iomode = IOMODE_RECV;
    _recvContext.wsabuf.buf = (char*)_recvBuffer.GetQueuePushPtr();
    _recvContext.wsabuf.len = _recvBuffer.GetPushAvailableSize();

    if (_recvBuffer.GetPushAvailableSize() <= 0)
    {
        Disconnect(false);
        Logger::Log("Session[%X] _recvBuffer.GetPushAvailableSize() <= 0, _recvBuffer.GetPushAvailableSize() = %d\n", this, _recvBuffer.GetPushAvailableSize());
        return -1; //ERR_NET_SESSION_RECV_BUFFER_OVERFLOW
    }

    IncreasePostIoCount();

    int rc = _socket.RecvEx(&_recvContext.wsabuf,
                            1,
                            &TransferedBytes,
                            &Flags,
                            &_recvContext);

    if (0 != rc)
    {
        DecreasePostIoCount();
        Logger::Log("Session[%X] RecvEx Function Failed (%d)/n", this, rc);
        return rc;
    }

    return 0;
}
Exemplo n.º 22
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( IOCP )
//-----------------------------------------------------------------------------------
int CNtlConnection::CompleteConnect(DWORD dwTransferedBytes)
{
	FUNCTION_BEGIN();

	UNREFERENCED_PARAMETER( dwTransferedBytes );

	m_socket.GetPeerAddr( m_remoteAddr );
	m_socket.GetLocalAddr( m_localAddr );
	m_bConnected = true;

	int rc = m_pNetworkRef->Associate( this, true );
	if( NTL_SUCCESS != rc )
	{
		NTL_LOGDL( LOG_NETWORK, "Session[%X] CompleteConnect Error[%d:%s]", this, rc, NtlGetErrorMessage(rc) );
		return rc;
	}


	SetStatus( STATUS_ACTIVE );
	m_dwConnectTime = GetTickCount();


	m_pConnectorRef->OnConnected();


	m_pNetworkRef->PostNetEventMessage( (WPARAM)NETEVENT_CONNECT, (LPARAM)this );	


	rc = PostRecv();
	if( NTL_SUCCESS != rc )
	{
		return rc;
	}



	//NTL_PRINT(PRINT_SYSTEM, "Session[%X]\tCompleteConnect Called Local[%s:%u] Remote[%s:%u]", pSession, GetLocalIP(), GetLocalPort(),	GetRemoteIP(), GetRemotePort() );


	return NTL_SUCCESS;
}
Exemplo n.º 23
0
bool Connections::PopPacket(Packet* pPacket)
{
    FUNCTION_BEGIN();

    if (NULL == pPacket)
    {
        Logger::Log("NULL == pPacket\n");
        return false;
    }

    int PacketLen = GetPacketLen(_recvBuffer.GetQueuePopPtr());

    pPacket->AttachData(_recvBuffer.GetQueuePopPtr(), (WORD)(GetHeaderSize() + PacketLen));

    if (false == pPacket->IsValidPacket())
    {
        return false;
    }

    return true;
}
Exemplo n.º 24
0
void Connections::Destroy()
{
    FUNCTION_BEGIN();

    if (false == ExchangeStatus(CONNSTATUS_DESTROY, false, CONNSTATUS_DESTROY))
    {
        Logger::Log("Status( CONNSTATUS_DESTROY )");
        return;
    }

    _socket.Shutdown(SD_BOTH);
    _socket.Close();

    if (_AcceptorRef)
    {
        _AcceptorRef->OnDisconnected(_IsConnected);
    }

    if (_ConnectorRef)
    {
        _ConnectorRef->OnDisconnected(_IsConnected);
    }
}
Exemplo n.º 25
0
int Connections::Create(Network* pNetwork)
{
    FUNCTION_BEGIN();


    if (IsStatus(CONNSTATUS_CREATE))
    {
        Logger::Log("true == IsStatus( STATUS_CREATE )\n");
        return 1;
    }


    if (NULL == pNetwork)
    {
        Logger::Log("NULL == pNetwork\n");
        return 2;
    }


    if (NULL != _NetworkRef)
    {
        Logger::Log("NULL != _NetworkRef, _NetworkRef = %016x\n", _NetworkRef);
        return 3;
    }


    if (false == _recvBuffer.Create(GetMaxRecvPacketCount(), GetMaxRecvPacketSize()))
    {
        return 4;
    }


    if (false == _sendBuffer.Create(GetMaxSendPacketCount(), GetMaxRecvPacketSize()))
    {
        return 5;
    }


    int rc = _socket.Create();
    if (0 != rc)
    {
        return rc;
    }


    rc = _socket.SetKeepAlive(CONNECTION_KEEP_ALIVE_TIME, CONNECTION_KEEP_ALIVE_INTERVAL);
    if (0 != rc)
    {
        return rc;
    }


    rc = _socket.SetLinger(true, 0);
    if (0 != rc)
    {
        return rc;
    }

    _PacketEncoder = GetPacketEncoder();

    _NetworkRef = pNetwork;


    SetStatus(CONNSTATUS_CREATE);

    return 0;
}
Exemplo n.º 26
0
int Connections::PushPacket(Packet* pPacket)
{
    FUNCTION_BEGIN();

    if (false == IsStatus(CONNSTATUS_ACTIVE))
    {
        return 1;//ERR_NET_CONNECTION_STATUS_WRONG
    }


    if (false == pPacket->IsValidPacket())
    {
        return 2;//ERR_NET_PACKET_INVALID
    }

    _mutexSend.Lock();

    if (pPacket->GetPacketLen() > GetMaxSendPacketSize())
    {
        _mutexSend.Unlock();
        return 3;//ERR_NET_PACKET_EXCEED_ALLOWED_SIZE
    }


    if (_sendBuffer.GetPushAvailableSize() <= pPacket->GetUsedSize())
    {
        if (false == IsSetControlFlag(CONTROL_FLAG_USE_SEND_QUEUE))
        {
            _mutexSend.Unlock();
            return 4;//ERR_NET_SESSION_SEND_BUFFER_OVERFLOW
        }
        else
        {
            Packet * QueuedPacket = new Packet(*pPacket);
            if (NULL == QueuedPacket)
            {
                _mutexSend.Unlock();
                return 5;//ERR_SYS_MEMORY_ALLOC_FAIL
            }

            if (false == _sendQueue.PushPacket(QueuedPacket))
            {
                SAFE_DELETE(QueuedPacket);

                _mutexSend.Unlock();
                return 6;//ERR_NET_SESSION_SEND_BUFFER_OVERFLOW;
            }

            _mutexSend.Unlock();
            return 0;
        }
    }

    memcpy(_sendBuffer.GetQueuePushPtr(), pPacket->GetPacketBuffer(), pPacket->GetUsedSize());

    Packet sendPacket(_sendBuffer.GetQueuePushPtr());
    BYTE Sequence = (BYTE)(_PacketSendCount & PACKET_MAX_SEQUENCE);
    SetSequence(sendPacket.GetPacketHeader(), Sequence);

    int rc = 0;

    if (_PacketEncoder)
    {
        rc = _PacketEncoder->TxEncrypt(sendPacket);
        if (0 != rc)
        {
            _mutexSend.Unlock();
            return 7;//ERR_NET_PACKET_ENCRYPT_FAIL
        }
    }

    _sendBuffer.IncreasePushPos(pPacket->GetUsedSize());
    IncreasePacketSend();

    if (false == _IsSending)
    {
        rc = PostSend();
        if (0 != rc)
        {
            _mutexSend.Unlock();
            return rc;
        }

        _IsSending = true;
    }

    _mutexSend.Unlock();

    return 0;
}
Exemplo n.º 27
0
void Connections::SetAddress(SOCKADDR_IN* LocalAddr, SOCKADDR_IN* RemoteAddr)
{
    FUNCTION_BEGIN();
    _localAddr = *LocalAddr;
    _remoteAddr = *RemoteAddr;
}
Exemplo n.º 28
0
Connections::~Connections()
{
    FUNCTION_BEGIN();

    Destroy();
}
Exemplo n.º 29
0
int Connections::CompleteSend(DWORD TransferedBytes)
{
    UNREFERENCED_PARAMETER(TransferedBytes);

    FUNCTION_BEGIN();


    IncreaseBytesSend(TransferedBytes);


    _mutexSend.Lock();
    _sendBuffer.IncreasePopPos(TransferedBytes);

    int rc = 0;
    if (IsSetControlFlag(CONTROL_FLAG_USE_SEND_QUEUE))
    {
        Packet * SendPacket = _sendQueue.PeekPacket();
        while (SendPacket)
        {
            if (_sendBuffer.GetPushAvailableSize() <= SendPacket->GetUsedSize())
            {
                break;
            }

            BYTE Sequence = (BYTE)(_PacketSendCount & PACKET_MAX_SEQUENCE);
            SetSequence(SendPacket->GetPacketHeader(), Sequence);

            if (_PacketEncoder)
            {
                rc = _PacketEncoder->TxEncrypt(*SendPacket);
                if (0 != rc)
                {
                    _mutexSend.Unlock();

                    Logger::Log("Session[%X] TxEncrypt Error[%d]\n", this, rc);
                    return 1; //ERR_NET_PACKET_ENCRYPT_FAIL
                }
            }

            memcpy(_sendBuffer.GetQueuePushPtr(), SendPacket->GetPacketBuffer(), SendPacket->GetUsedSize());
            _sendBuffer.IncreasePushPos(SendPacket->GetUsedSize());
            IncreasePacketSend();

            _sendQueue.PopPacket();
            SAFE_DELETE(SendPacket);

            SendPacket = _sendQueue.PeekPacket();
        }
    }


    if (_sendBuffer.GetCurSize())
    {
        rc = PostSend();
        if (0 != rc)
        {
            _IsSending = false;
            _mutexSend.Unlock();
            Disconnect(false);

            Logger::Log("Session[%X] PostSend Error[%d]\n", this, rc);
            return 0;
        }
    }
    else
    {
        _IsSending = false;
    }


    _mutexSend.Unlock();

    return 0;
}
Exemplo n.º 30
0
Connections::Connections()
{
    FUNCTION_BEGIN();

    Init();
}