buf *getblk(int blknum){ while(&h_head[blknum % 4] != NULL){ buf *buffer = Search(blknum); if(buffer != NULL){ assert(buffer != NULL); if(IsStatus(buffer, STAT_LOCKED)){ //sleep(); printf("SCENARIO 5\n"); printf("Process goes to sleep\n"); AddStatus(buffer, STAT_WAITED); return NULL; continue; } //scenario 1 printf("SCENARIO 1\n"); MakeStatus(buffer, (STAT_LOCKED | STAT_VALID)); RemFromFreeList(buffer); return buffer; } else{ //if(IsInFreeList(buffer)){ if(IsEmptyFree()){ //scenario 4 //sleep(); printf("SCENARIO 4\n"); printf("Process goes to sleep\n"); return buffer; //continue; } //RemFromFreeList(buffer); //RemoveFromFree() buf *ref = ref_free_head(); if(CheckStatus(ref, STAT_DWR)){ //scenario 3 printf("SCENARIO 3\n"); //asynchronous write buffer to disk; buf *prev = ref -> free_bp; buf *next = ref -> free_fp; prev -> free_fp = next; next -> free_bp = prev; MakeStatus(ref, STAT_LOCKED | STAT_VALID | STAT_KRDWR | STAT_OLD); continue; } //scenario 2 printf("SCENARIO 2\n"); buf *additionalbuf = remove_free_head(); RemStatus(additionalbuf, STAT_VALID); additionalbuf -> blkno = blknum; AddToHash(additionalbuf); printf("Kernel HDD access occuring\n"); AddStatus(additionalbuf, STAT_KRDWR); printf("Kernel HDD access finished\n"); RemStatus(additionalbuf, STAT_KRDWR); AddStatus(additionalbuf, STAT_VALID); return additionalbuf; } } printf("BUFFER NOT FOUND\n"); return NULL; }
bool Connections::IsShutdownable() { if (IsStatus(CONNSTATUS_CLOSE)) { if (0 == InterlockedCompareExchange((LONG*)&_IoPostCount, 0, 0)) { return true; } } return false; }
//----------------------------------------------------------------------------------- // 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; }
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; }
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; }
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; }
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; }
//----------------------------------------------------------------------------------- // Purpose : // Return : //----------------------------------------------------------------------------------- // Called by App TH, Worker TH ( CompleteSend, PushPacket ) //----------------------------------------------------------------------------------- int CNtlConnection::PostSend() { FUNCTION_BEGIN(); if( false == IsStatus( STATUS_ACTIVE ) ) { Disconnect( false ); return NTL_SUCCESS; //return NTL_ERR_NET_CONNECTION_STATUS_WRONG; } DWORD dwFlags = 0; DWORD dwSendBytes = 0; m_sendContext.Reset(); m_sendContext.iomode = IOMODE_SEND; m_sendContext.wsabuf.buf = (char*) m_sendBuffer.GetQueuePopPtr(); #if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__ bool bIsIf = false; #endif if( m_sendBuffer.GetPopPos() + m_sendBuffer.GetCurSize() >= m_sendBuffer.GetQueueSize() ) { #if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__ bIsIf = true; #endif // [9/13/2007 zeroera] : ¼öÁ¤ : ¼º´É °³¼±À» À§ÇØ ¼öÁ¤ÇÒ ºÎºÐ m_sendContext.wsabuf.len = m_sendBuffer.GetQueueSize() - m_sendBuffer.GetPopPos(); //if( m_sendBuffer.GetQueueSize() <= m_sendBuffer.GetPopPos() ) //{ // NTL_LOG_ASSERT("m_sendBuffer.GetQueueSize() <= m_sendBuffer.GetPopPos(), m_sendBuffer.GetQueueSize() = %d, m_sendBuffer.GetPopPos() = %d", m_sendBuffer.GetQueueSize(), m_sendBuffer.GetPopPos()); // Disconnect( false ); // return NTL_SUCCESS; //} } else { m_sendContext.wsabuf.len = m_sendBuffer.GetCurSize(); //if( m_sendBuffer.GetCurSize() <= 0 ) //{ // NTL_LOG_ASSERT("m_sendBuffer.GetCurSize() <= 0, m_sendBuffer.GetCurSize() = %d", m_sendBuffer.GetCurSize()); // Disconnect( false ); // return NTL_SUCCESS; //} } IncreasePostIoCount(); int rc = m_socket.SendEx( &m_sendContext.wsabuf, 1, &dwSendBytes, dwFlags, &m_sendContext); if( NTL_SUCCESS != rc ) { DecreasePostIoCount(); NTL_LOGDL( LOG_NETWORK, "Session[%X] SendEx Function Failed: (%d)%s", this, rc, NtlGetErrorMessage( rc ) ); return rc; } #if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__ m_cConsolLog.WriteBotSystemLog( reinterpret_cast< void * >( m_sendContext.wsabuf.buf ) , m_sendContext.wsabuf.len , bIsIf , &m_sendBuffer , m_nCharacterPCID); #endif//__SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__ //NTL_PRINT( PRINT_SYSTEM, "Session[%X]\tPostSend Complete SendBufferSize[%d]", this, m_sendBuffer.GetCurSize() ); return NTL_SUCCESS; }
//----------------------------------------------------------------------------------- // Purpose : // Return : //----------------------------------------------------------------------------------- // Called by Acceptor TH, Connector TH //----------------------------------------------------------------------------------- int CNtlConnection::Create(CNtlNetwork * pNetwork) { FUNCTION_BEGIN(); if( IsStatus( STATUS_CREATE ) ) { NTL_LOG_ASSERT("true == IsStatus( STATUS_CREATE )"); return NTL_ERR_NET_CONNECTION_STATUS_WRONG; } if( NULL == pNetwork ) { NTL_LOG_ASSERT("NULL == pNetwork"); return NTL_ERR_SYS_INPUT_PARAMETER_WRONG; } if( NULL != m_pNetworkRef ) { NTL_LOG_ASSERT("NULL != m_pNetworkRef, m_pNetworkRef = %016x", m_pNetworkRef); return NTL_ERR_SYS_OBJECT_ALREADY_CREATED; } if( false == m_recvBuffer.Create( GetMaxRecvPacketCount(), GetMaxRecvPacketSize() ) ) { return NTL_ERR_SYS_MEMORY_ALLOC_FAIL; } if( false == m_sendBuffer.Create( GetMaxSendPacketCount(), GetMaxRecvPacketSize() ) ) { return NTL_ERR_SYS_MEMORY_ALLOC_FAIL; } int rc = m_socket.Create( CNtlSocket::eSOCKET_TCP ); if( NTL_SUCCESS != rc ) { return rc; } rc = m_socket.SetKeepAlive( CONNECTION_KEEP_ALIVE_TIME, CONNECTION_KEEP_ALIVE_INTERVAL ); if( NTL_SUCCESS != rc ) { return rc; } rc = m_socket.SetLinger( true, 0 ); if( NTL_SUCCESS != rc ) { return rc; } #if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__ m_pPacketEncoder = NULL; #else m_pPacketEncoder = GetPacketEncoder(); #endif m_pNetworkRef = pNetwork; SetStatus( STATUS_CREATE ); return NTL_SUCCESS; }
//----------------------------------------------------------------------------------- // Purpose : // Return : //----------------------------------------------------------------------------------- // Called by App TH, Worker TH ( Send ) //----------------------------------------------------------------------------------- int CNtlConnection::PushPacket(CNtlPacket * pPacket) { FUNCTION_BEGIN(); if( false == IsStatus( STATUS_ACTIVE ) ) { return NTL_ERR_NET_CONNECTION_STATUS_WRONG; } if( false == pPacket->IsValidPacket() ) { return NTL_ERR_NET_PACKET_INVALID; } m_mutexSend.Lock(); // ¹ØÀ¸·Î ³»¸®¸é µ¿±âÈ°¡ ±úÁø´Ù [5/30/2008 SGpro] if( pPacket->GetPacketLen() > GetMaxSendPacketSize() ) { m_mutexSend.Unlock(); return NTL_ERR_NET_PACKET_EXCEED_ALLOWED_SIZE; } if( m_sendBuffer.GetPushAvailableSize() <= pPacket->GetUsedSize() ) { if( false == IsSetControlFlag( CONTROL_FLAG_USE_SEND_QUEUE ) ) { m_mutexSend.Unlock(); return NTL_ERR_NET_SESSION_SEND_BUFFER_OVERFLOW; } else { CNtlPacket * pQueuedPacket = new CNtlPacket( *pPacket ); if( NULL == pQueuedPacket ) { m_mutexSend.Unlock(); return NTL_ERR_SYS_MEMORY_ALLOC_FAIL; } if( false == m_sendQueue.PushPacket( pQueuedPacket ) ) { SAFE_DELETE( pQueuedPacket ); m_mutexSend.Unlock(); return NTL_ERR_NET_SESSION_SEND_BUFFER_OVERFLOW; } m_mutexSend.Unlock(); return NTL_SUCCESS; } } //m_mutexSend.Lock(); //¿©±â¿¡ µÎ¸é µ¿±âÈ°¡ ±úÁø´Ù [5/30/2008 SGpro] memcpy( m_sendBuffer.GetQueuePushPtr(), pPacket->GetPacketBuffer(), pPacket->GetUsedSize() ); // [9/10/2007 zeroera] : ¼³¸í : È¿À²¼ºÀ» À§ÇØ ÆÐŶ ¹öÆÛ¿¡ ÀÖ´Â µ¥ÀÌŸ¸¦ °¡Áö°í ¾ÏÈ£È ÇÑ´Ù CNtlPacket sendPacket( m_sendBuffer.GetQueuePushPtr() ); BYTE bySequence = (BYTE) ( m_dwPacketSendCount & PACKET_MAX_SEQUENCE ); SetSequence( sendPacket.GetPacketHeader(), bySequence ); int rc = NTL_SUCCESS; #if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__ #else if( m_pPacketEncoder ) { rc = m_pPacketEncoder->TxEncrypt( sendPacket ); if( NTL_SUCCESS != rc ) { m_mutexSend.Unlock(); return NTL_ERR_NET_PACKET_ENCRYPT_FAIL; } } #endif m_sendBuffer.IncreasePushPos( pPacket->GetUsedSize() ); IncreasePacketSend(); if( false == m_bSending ) { rc = PostSend(); if( NTL_SUCCESS != rc ) { m_mutexSend.Unlock(); return rc; } m_bSending = true; } m_mutexSend.Unlock(); return NTL_SUCCESS; }
bool command::IsUserStatKur(){ return (IsStatus(ArrayOfCommand[1]) && IsKurcaci(ArrayOfCommand[2]) && IsMark(ArrayOfCommand[3]) && JumlahString == 2); }