Ejemplo n.º 1
0
CIOBuffer *CIOBuffer::Allocator::Allocate()
{
    CCriticalSection::Owner lock( m_criticalSection );

    CIOBuffer *pBuffer = 0;

    if ( !m_freeList.Empty() )
    {
        pBuffer = m_freeList.PopNode();

        pBuffer->AddRef();
    }
    else
    {
        pBuffer = new( m_bufferSize )CIOBuffer( *this, m_bufferSize );

        if ( !pBuffer )
        {
            throw CException( _T("CIOBuffer::Allocator::Allocate()"), _T("Out of memory") );
        }

        OnBufferCreated();
    }

    m_activeList.PushNode( pBuffer );

    OnBufferAllocated();

    return pBuffer;
}
Ejemplo n.º 2
0
HRESULT CServer::OnDataRecieved()
{
   if (!m_pSocketServer)
   {
      return Error(L"Server hasn't been initialised - programming error!");
   }

   if (!m_pIDataInit || !m_pIData)
   {
      return Error(L"Internal error: failed to create Data object");
   }
   
   CCOMSocketServer::Socket *pSocket = m_pSocketServer->GetSocket();

   CIOBuffer *pBuffer = m_pSocketServer->GetBuffer();

   if (!pSocket || !pBuffer)
   {
      return Error(L"Internal error: pSocket or pBuffer is 0");
   }

   ISocket *pISocket = reinterpret_cast<ISocket*>(pSocket->GetUserPtr());

   HRESULT hr = m_pIDataInit->Init(pBuffer->GetBuffer(), pBuffer->GetUsed());

   if (SUCCEEDED(hr))
   {  
      Fire_OnDataReceived(pISocket, m_pIData);   // this can stall if the handler doesnt return - 
                                                 // the ATL implementation is difficult to multi thread...
   }

   return hr;
}
Ejemplo n.º 3
0
// if user wants result to buffer?
void QXpkLib::getToBuffer(QByteArray &Array)
{
	CIOBuffer *pOut = m_pMaster->getResult();
	
	// reserve out-buffer and copy what we have
	Array.resize(pOut->GetCurrentPos());
	::memcpy(Array.data(), pOut->GetBegin(), pOut->GetCurrentPos());
}
Ejemplo n.º 4
0
void CSocketServer::PostAbortiveClose( Socket *pSocket )
{
	CIOBuffer *pBuffer = Allocate();
	
	pBuffer->SetUserData( IO_Close );
	
	pSocket->AddRef();
	
	m_iocp.PostStatus( (ULONG_PTR)pSocket, 0, pBuffer );
}
Ejemplo n.º 5
0
// 각각의 메모리 풀에서 하나 가져온다.
//***************************************
CIOBuffer * SCMemoryManager::AllocPerRecvBuffer()
{
	CIOBuffer * pRecvBuffer = NULL;

	pRecvBuffer = m_pRecvBufferMemPool->Alloc();
	pRecvBuffer->m_WSABuf.buf = pRecvBuffer->GetRecvBuffer();
	pRecvBuffer->m_WSABuf.len = MAX_BUFFER_LEN;

	return pRecvBuffer;
}
Ejemplo n.º 6
0
CIOBuffer *CIOBuffer::SplitBuffer( size_t bytesToRemove )
{
    CIOBuffer *pNewBuffer = m_allocator.Allocate();

    pNewBuffer->AddData( m_buffer_ptr, bytesToRemove );

    m_used -= bytesToRemove;

    memmove( m_buffer_ptr, m_buffer_ptr + bytesToRemove, m_used );

    return pNewBuffer;
}
Ejemplo n.º 7
0
void CVwRegressor::load_regressor(char* file)
{
	CIOBuffer source;
	int32_t fd = source.open_file(file, 'r');

	if (fd < 0)
		SG_SERROR("Unable to open file for loading regressor!\n")

	// Read version info
	vw_size_t v_length;
	source.read_file((char*)&v_length, sizeof(v_length));
	char* t = SG_MALLOC(char, v_length);
	source.read_file(t,v_length);
	if (strcmp(t,env->vw_version) != 0)
	{
		SG_FREE(t);
		SG_SERROR("Regressor source has an incompatible VW version!\n")
	}
Ejemplo n.º 8
0
void CSocketClient::Write( const char *pData, size_t dataLength )
{
	if ( INVALID_SOCKET != m_connectSocket &&
		dataLength > 0 &&
		pData )
	{
		CIOBuffer *pBuffer = Allocate();
		
		/*
		 * Call to unqualified virtual function
		 */
		PreWrite( pBuffer, pData, dataLength );

		pBuffer->AddData( pData, dataLength );

		/*
		 * Begin to send data
		 */
		pBuffer->SetupWrite();
		
		DWORD dwFlags = 0;
		DWORD dwSendNumBytes = 0;
		
		if ( SOCKET_ERROR == ::WSASend(
					m_connectSocket,
					pBuffer->GetWSABUF(), 
					1, 
					&dwSendNumBytes,
					dwFlags,
					pBuffer, 
					NULL) )
		{
			DWORD lastError = ::WSAGetLastError();
			
			if ( ERROR_IO_PENDING != lastError )
			{
				Output( _T("CSocketClient::Write() - WSASend: ") + GetLastErrorMessage( lastError ) );
				
				if ( lastError == WSAECONNABORTED || 
					lastError == WSAECONNRESET ||
					lastError == WSAEDISCON)
				{
					StopConnections();
				}
			}
		}
		
		if ( pBuffer->GetUsed() != pBuffer->GetWSABUF()->len )
		{
			/*
			 * Call to unqualified virtual function
			 */
			//OnError(_T("CSocketClient::WriteCompleted - Socket write where not all data was written"));
		}

		pBuffer->Release();
	}
}
Ejemplo n.º 9
0
void CSocketServer::Write(
				   Socket *pSocket,
				   const char *pData,
				   size_t dataLength, 
				   bool thenShutdown)
{
	if ( !pSocket || !pData || dataLength <= 0 )
	{
		return;
	}
   /*
    * Post a write request to the iocp so that the actual socket write gets performed by
	* one of our IO threads...
	*/

   CIOBuffer *pBuffer = Allocate();
   
   /*
    * Call to unqualified virtual function
    */
#ifdef NETWORK_DEBUG
//{
   PreWrite( pSocket, pBuffer, pData, dataLength + PACK_HEADER_LEN + sizeof(BYTE) );
//}
#else
//{
   PreWrite( pSocket, pBuffer, pData, dataLength );
//}
#endif // NETWORK_DEBUG

   pBuffer->AddData( pData, dataLength );
   
#ifdef NETWORK_DEBUG
//{
	const BYTE *pPackData = pBuffer->GetBuffer();

	PACK_HEADER ph = {0};

	memcpy( (BYTE *)&ph, pPackData, PACK_HEADER_LEN );

	pBuffer->AddData( (BYTE *)&ph, PACK_HEADER_LEN );
	pBuffer->AddData( 0xAA );
//}
#endif // NETWORK_DEBUG

   pBuffer->SetUserData( IO_Write_Request );

   pSocket->AddRef();

   m_iocp.PostStatus( (ULONG_PTR)pSocket, thenShutdown, pBuffer );
}
Ejemplo n.º 10
0
//////////////////
//
// XPK fileformat:
// chunk-based, crunched data in chunks
//
// Chunk format:
// * chunk header
// - 1 byte for chunk type
// - 1 byte for chunk header checksum ?
// - 2 bytes for chunk checksum
// - 2/4 bytes for chunk (compressed) length
// - 2/4 bytes for uncompressed length?
// * chunk data
// - amount of packed data given in chunk header..
//
// Note: chunk header may have different size in different file
// according to flags (if "long" sizes are used for chunks).
//
//
void XpkTags::ReadChunks(CIOBuffer &Buffer)
{
	m_pFirst = new XpkChunk();
	m_pFirst->m_nDataOffset = Buffer.GetCurrentPos();

	XpkChunk *pCurrent = m_pFirst;
	while (Buffer.IsEnd() == false)
	{
		bool isValidHeader = false;
		if (m_streamHeader.xsh_Flags & XPKSTREAMF_LONGHEADERS)
		{
			XpkChunkHdrLong *pHdr = (XpkChunkHdrLong*)Buffer.GetAtCurrent();
			
			pCurrent->m_Type = pHdr->xchl_Type;
			pCurrent->m_HChecksum = pHdr->xchl_HChk;
			pCurrent->m_ChunkChecksum = Swap2(pHdr->xchl_CChk);
			pCurrent->m_ChunkLength = Swap4(pHdr->xchl_CLen);
			pCurrent->m_UnLen = Swap4(pHdr->xchl_ULen);
			
			pCurrent->m_nDataOffset += sizeof(XpkChunkHdrLong);
			isValidHeader = verifyHeaderLong(pHdr);
		}
		else
		{
			XpkChunkHdrWord *pHdr = (XpkChunkHdrWord*)Buffer.GetAtCurrent();
			
			pCurrent->m_Type = pHdr->xchw_Type;
			pCurrent->m_HChecksum = pHdr->xchw_HChk;
			pCurrent->m_ChunkChecksum = Swap2(pHdr->xchw_CChk);
			pCurrent->m_ChunkLength = Swap2(pHdr->xchw_CLen);
			pCurrent->m_UnLen = Swap2(pHdr->xchw_ULen);

			pCurrent->m_nDataOffset += sizeof(XpkChunkHdrWord);
			isValidHeader = verifyHeaderWord(pHdr);
		}
		
		// TODO: need header checksum verification somewhere around here..
		if (isValidHeader == false)
		{
			// TODO: exception or skip ?
		}
		
		if (pCurrent->m_Type != XPKCHUNK_RAW
			&& pCurrent->m_Type != XPKCHUNK_PACKED
			&& pCurrent->m_Type != XPKCHUNK_END)
		{
			// TODO: exception or skip ?
		}
		
		// move to actual data of chunk (according to chunk header size)
		Buffer.SetCurrentPos(pCurrent->m_nDataOffset);

		// "end-of-file" chunk? (empty)
		if (pCurrent->m_Type == XPKCHUNK_END)
		{
			return;
		}
		
		// TODO:
		// .. process chunk
		// -> walkthrough in XPK-master now..
		
		// offset to start of next chunk:
		// start of data in current + size of data in current
		size_t nNextChunkOffset = pCurrent->m_nDataOffset + pCurrent->m_ChunkLength;
		Buffer.SetCurrentPos(nNextChunkOffset);
		
		pCurrent->m_pNext = new XpkChunk(pCurrent);
		pCurrent->m_pNext->m_nDataOffset = nNextChunkOffset;
		pCurrent = pCurrent->m_pNext;
	}
}
Ejemplo n.º 11
0
//////////////////
//
// XPK fileformat:
// chunk-based, crunched data in chunks
// 
// File header:
// starts as common IFF-style header:
// - 4-byte ID, 'XPKF'
// - 4-byte int for filesize minus header (8)
// - 4-byte sub-type (cruncher-ID) e.g. 'SQSH'
// - 4-byte int for total uncompressed length ?
// - first 16-bytes of original file
// - 1 byte for flags
// - 1 byte for header checksum ?
// - 1 byte for minor version of cruncher/library ?
//  - actually version required of sub-library..
// - 1 byte for major version of cruncher/library ?
//
bool XpkTags::ReadStreamHeader(CIOBuffer &Buffer)
{
	// should have enough data to actually parse file header
	if (Buffer.GetSize() < sizeof(XpkStreamHeader))
	{
		return false;
	}
	
	if (isXpkFile(Buffer.GetBegin()) == false)
	{
		return false;
	}
	Buffer.SetCurrentPos(0); // start at beginning if not already..
	
	// set default
	m_formatType = XPKMODE_UPSTD;
	
	// "XPKF", 0x58504b46 (XPK_COOKIE, magic number)
	// note: support also XFD-packed files? ("XFDD")
	m_streamHeader.xsh_PackID = GetULong(Buffer.GetNext(4));
	
	if (m_streamHeader.xsh_PackID != MakeTag("XPKF"))
	// this too ? && m_streamHeader.xsh_PackID != MakeTag("XFDD"))
	{
		return false;
	}
	
	// file length without IFF header (type+length = 8) ?
	m_streamHeader.xsh_CompressedLen = GetULong(Buffer.GetNext(4));
	
	// keep packer type as type name/ID,
	// just access as-is
	m_typeName.assign((char*)Buffer.GetAtCurrent(), 4);
	
	// packer type, e.g. "SQSH", "NUKE", "RLEN"..
	m_streamHeader.xsh_PackerType = GetULong(Buffer.GetNext(4));
	
	// TODO: check supported types..? if != MakeTag()..
	// -> caller/parent should do (knows libraries..)
	
	// uncompressed length?
	m_streamHeader.xsh_UnpackedLen = GetULong(Buffer.GetNext(4));

	// first 16 bytes of original file
	::memcpy(m_streamHeader.xsh_Initial, Buffer.GetNext(16), 16);
	
	// flags
	m_streamHeader.xsh_Flags = Buffer.GetNextByte();
	
	/*
	// also check "XFDD", 0x58464444 in file ID?
	if (m_streamHeader.xsh_Flags & XMF_XFD && m_streamHeader.xsh_PackID == MakeTag("XFDD"))
	{
		m_formatType = XPKMODE_UPXFD;
	}
	
	if (m_streamHeader.xsh_Flags & XMF_PASSTHRU)
	{
		// unpacked?
		m_formatType = XPKMODE_UPUP;
	}
	
	if (m_streamHeader.xsh_Flags & XPKSTREAMF_PASSWORD)
	{
		// password-protected file?
	}
	*/
	
	// ..no idea.. header checksum?
	m_streamHeader.xsh_HeaderChk = Buffer.GetNextByte();
	
	// minor&major version of XPK master/cruncher?
	m_streamHeader.xsh_SubVrs = Buffer.GetNextByte(); // sub-library version required?
	m_streamHeader.xsh_MasVrs = Buffer.GetNextByte();
	
	// TODO: remove later, debug-test..
	if (Buffer.GetCurrentPos() != sizeof(XpkStreamHeader))
	{
		throw IOException("Read size does not match stream-header size");
	}
	
	// non-zero header checksum? (note where doing checksumming..)
    if (hchecksum(Buffer.GetBegin(), sizeof(XpkStreamHeader)) != 0)
    {
		throw ArcException("Header checksum error", m_streamHeader.xsh_HeaderChk);
    }
    
    // has extended header?
    if (m_streamHeader.xsh_Flags & XPKSTREAMF_EXTHEADER)
    {
		// size of extended header if present?
		m_extHeaderLen = GetUWord(Buffer.GetNext(2));
		
		/*
		// this done above..		
	    if(!hookread(xbuf, XIO_READ, &exthlen, sizeof(UWORD)))
			goto Abort; -> exception
			
		// no read, just skip it??
        if(!hookread(xbuf, XIO_READ, NULL, exthlen))
			goto Abort; -> exception
			
		// later it is read anyway, why not just directly when detected?
			
		*/
		
		// increment by length-field size anyway..
        m_extHeaderLen += sizeof(uint16_t);	/* for unwinding while XpkExamine */
        
        // note: increase buffer position by size of extended header?
	}

	// header parsed, should be valid file?	
	return true;
}
Ejemplo n.º 12
0
CIOBuffer *CGameClient::ProcessDataStream( OnlineGameLib::Win32::CIOBuffer *pBuffer)
{
	bool done;
	
//	DEBUG_ONLY( Output( _T("ProcessDataStream:\n") + DumpData( pBuffer->GetBuffer(), pBuffer->GetUsed(), 40 ) ) );
	
	do
	{
		done = true;
		
		const size_t used = pBuffer->GetUsed();
		
		if ( used >= GetMinimumMessageSize() )
		{
			const size_t messageSize = GetMessageSize( pBuffer );
			
			if ( messageSize == 0 )
			{
				/*
				 * havent got a complete message yet.
				
				 * we null terminate our messages in the buffer, so we need to reserve
				 * a byte of the buffer for this purpose...
				 */
				
				if ( used == ( pBuffer->GetSize() - 1 ) )
				{
					Output( _T("Too much data!") );
					
					/*
					 * Write this message and then shutdown the sending side of the socket.
					 */
					Output( "found error and close this connection!" );
		
					StopConnections();
					
					/*
					 * throw the rubbish away
					 */
					pBuffer->Empty();
					
					done = true;
				}
			}
			else if ( used == messageSize )
			{
				Output( _T("Got complete, distinct, message") );
				/*
				 * we have a whole, distinct, message
				 */
				
				pBuffer->AddData(0);   // null terminate the command string;
				
				ProcessCommand( pBuffer );
				
				pBuffer->Empty();
				
				done = true;
			}
			else if (used > messageSize)
			{
				Output(_T("Got message plus extra data"));
				/*
				 * we have a message, plus some more data
				 * 
				 * allocate a new buffer, copy the extra data into it and try again...
				 */
				
				CIOBuffer *pMessage = pBuffer->SplitBuffer( messageSize );
				
				pMessage->AddData(0);   // null terminate the command string;
				
				ProcessCommand( pMessage );
				
				pMessage->Release();
				
				/*
				 * loop again, we may have another complete message in there...
				 */
				
				done = false;
			}
		}
	}
	while ( !done );
	
	/*
	 * not enough data in the buffer, reissue a read into the same buffer to collect more data
	 */
	return pBuffer;
}
Ejemplo n.º 13
0
	void CTcpSocket::on_inter_close(int errid, bool bconnecting)
	{
		if (m_status != status_connect && m_status != status_common) return;

		XH_GUARD([&]{
			closetcpsocket(m_socket);
			m_socket = INVALID_SOCKET;

			if (m_io)
			{
				if ((m_bassign & 0x01) == 0x01)
					m_io->Del_Event(&m_ev_read);
				if ((m_bassign & 0x02) == 0x02)
					m_io->Del_Event(&m_ev_write);

				m_bassign = 0;
				m_io->Del_TcpSocket(this);
			}
		});

		m_status = status_null;

		switch (m_type)
		{
		case type_listener:
			if (m_lcb)
			{
				if (bconnecting)
				{
					m_lcb->On_Listen(GetSocketID(), errid);
				}

				m_lcb->On_Close(GetSocketID(), errid);
			}
			break;
		case type_accepter:
			if (m_acb)
			{
				if (bconnecting)
				{
					m_acb->On_Connect(GetSocketID(), errid);
				}

				while (!m_wait_send.empty())
				{
					CIOBuffer* tmpbuf = m_wait_send.front();
					XH_GUARD([&]{tmpbuf->Release(); });

					m_acb->On_Send(GetSocketID(), errid, tmpbuf);

					m_wait_send.pop();
				}

				while (!m_wait_recv.empty())
				{
					CIOBuffer* tmpbuf = m_wait_recv.front();
					XH_GUARD([&]{tmpbuf->Release(); });

					m_acb->On_Recv(GetSocketID(), errid, tmpbuf);

					m_wait_recv.pop();
				}

				m_acb->On_Close(GetSocketID(), errid);

				// 异常调度行为检查
				while (!m_wait_send.empty())
				{
					CIOBuffer* tmpbuf = m_wait_send.front();
					XH_GUARD([&]{tmpbuf->Release(); });

					m_wait_send.pop();
				}
				while (!m_wait_recv.empty())
				{
					CIOBuffer* tmpbuf = m_wait_recv.front();
					XH_GUARD([&]{tmpbuf->Release(); });

					m_wait_recv.pop();
				}
			}
			break;
		case type_connecter:
			if (m_ccb)
			{
				if (bconnecting)
				{
					m_ccb->On_Connect(GetSocketID(), errid);
				}

				while (!m_wait_send.empty())
				{
					CIOBuffer* tmpbuf = m_wait_send.front();
					XH_GUARD([&]{tmpbuf->Release(); });

					m_ccb->On_Send(GetSocketID(), errid, tmpbuf);

					m_wait_send.pop();
				}

				while (!m_wait_recv.empty())
				{
					CIOBuffer* tmpbuf = m_wait_recv.front();
					XH_GUARD([&]{tmpbuf->Release(); });

					m_ccb->On_Recv(GetSocketID(), errid, tmpbuf);

					m_wait_recv.pop();
				}

				m_ccb->On_Close(GetSocketID(), errid);

				// 异常调度行为检查
				while (!m_wait_send.empty())
				{
					CIOBuffer* tmpbuf = m_wait_send.front();
					XH_GUARD([&]{tmpbuf->Release(); });

					m_wait_send.pop();
				}
				while (!m_wait_recv.empty())
				{
					CIOBuffer* tmpbuf = m_wait_recv.front();
					XH_GUARD([&]{tmpbuf->Release(); });

					m_wait_recv.pop();
				}
			}
			break;
		default:
			break;
		}

		XH_LOG_INFO(logname_base, "[id:" << m_id << "] tcpsocket closed by errid : "<<errid);
	}
Ejemplo n.º 14
0
	void CTcpSocket::on_inter_send(short flag)
	{
		if (m_status == status_connect)
		{
			if ((flag&EV_WRITE) == EV_WRITE)
			{
				m_status = status_common;

				switch (m_type)
				{
				case type_accepter:
					if (m_acb)
					{
						m_acb->On_Connect(GetSocketID(), tcp_ok);
					}
					break;
				case type_connecter:
					if (m_ccb)
					{
						m_ccb->On_Connect(GetSocketID(), tcp_ok);
					}
					break;
				default:
					break;
				}
			}
			else
			{
				XH_LOG_ERROR(logname_base, "[id:" << m_id << "] tcpsocket connect timeout");
				on_inter_close(tcp_connectfail_timeout, true);

				return;
			}
		}
		else
		{
			while (!m_wait_send.empty())
			{
				CIOBuffer* buff = m_wait_send.front();

				if ( buff->AvailRead()==0 )
				{
					XH_LOG_ERROR(logname_base, "[id:" << m_id << "] tcpsocket::send close cmd");
					on_inter_close(tcp_close_byreset, false);
					return;
				}

				bool bneedbreak = false;
				int sendlen = ::send(m_socket, buff->GetCurRead(), buff->AvailRead(), 0);

				if ( sendlen>0 )
				{
					buff->SeekRead(sendlen);
				}
				else if (sendlen<0)
				{
					if (isnoblockerr())
					{
						bneedbreak = true;
					}
					else
					{
						XH_LOG_ERROR(logname_base, "[id:" << m_id << "] tcpsocket::send failed send - 1");
						on_inter_close(tcp_sendfail_senderr, false);
						return;
					}
				}
				else
				{
					bneedbreak = true;
				}

				//已经写完了,则回调通知
				if (buff->AvailRead()==0)
				{
					switch (m_type)
					{
					case type_accepter:
						if (m_acb)
						{
							m_acb->On_Send(GetSocketID(), tcp_ok, buff);
						}
						break;
					case type_connecter:
						if (m_ccb)
						{
							m_ccb->On_Send(GetSocketID(), tcp_ok, buff);
						}
						break;
					default:
						break;
					}

					m_wait_send.pop();
					buff->Release();
				}

				if (bneedbreak) break;
			}
		}

		if (m_wait_send.empty())
		{
			m_io->Del_Event(&m_ev_write);
		}
	}
Ejemplo n.º 15
0
	void CTcpSocket::on_inter_recv(short flag)
	{
		if (m_wait_recv.empty())
		{
			m_io->Del_Event(&m_ev_read);
			return;
		}

		CIOBuffer* recvbuff = m_wait_recv.front();

		int recvlen = ::recv(m_socket, recvbuff->GetCurWrite(), recvbuff->AvailWrite(), 0);
		if (recvlen == 0)
		{
			XH_LOG_INFO(logname_base, "[id:" << m_id << "] tcpsocket::recv failed, closed by peer");
			on_inter_close(tcp_recvfail_closedbypeer, false);
			return;
		}
		else if (recvlen < 0)
		{
			if (isnoblockerr())
			{
				;
			}
			else
			{
				XH_LOG_WARN(logname_base, "[id:" << m_id << "] tcpsocket::recv failed, recv - 1");
				on_inter_close(tcp_recvfail_recverr, false);
			}
			return;
		}

		bool bneedcb = false;
		recvbuff->SeekWrite(recvlen);
		// 如果是readsome
		if ( recvbuff->GetTag()==1 )
		{
			bneedcb = true;
		}
		// 如果是读固定长度
		else
		{
			// 如果读全了
			if (recvbuff->AvailWrite()==0)
			{
				bneedcb = true;
			}
		}

		if ( !bneedcb )
		{
			return;
		}

		switch (m_type)
		{
		case type_accepter:
			if (m_acb)
			{
				m_acb->On_Recv(GetSocketID(), tcp_ok, recvbuff);
			}
			break;
		case type_connecter:
			if (m_ccb)
			{
				m_ccb->On_Recv(GetSocketID(), tcp_ok, recvbuff);
			}
			break;
		default:
			break;
		}

		m_wait_recv.pop();
		recvbuff->Release();

		// 没有可以接收的缓存,则删除事件
		if (m_wait_recv.empty())
		{
			m_io->Del_Event(&m_ev_read);
		}
	}
Ejemplo n.º 16
0
int CSocketServer::WorkerThread::Run()
{
	try
	{
		while ( true )
		{
			/*
			 * Continually loop to service io completion packets
			 */
			
			bool closeSocket = false;
			
			DWORD dwIoSize = 0;
			Socket *pSocket = 0;
			CIOBuffer *pBuffer = 0;
			
			try
			{
				m_iocp.GetStatus( (PDWORD_PTR)&pSocket, &dwIoSize, (OVERLAPPED **)&pBuffer );
			}
			catch (const CWin32Exception &e)
			{
				if ( e.GetError() != ERROR_NETNAME_DELETED &&
					e.GetError() != WSA_OPERATION_ABORTED )
				{
					throw;
				}
				
				Output( _T("IOCP error [client connection dropped] - ") +
					GetLastErrorMessage( ::WSAGetLastError() ) );
				
				closeSocket = true;
			}
			
			if ( !pSocket )
			{
				/*
				 * A completion key of 0 is posted to the iocp to request us to shut down...
				 */
				
				break;
			}
			
			/*
			 * Call to unqualified virtual function
			 */
			OnBeginProcessing();
			
			if ( pBuffer )
			{
				const IO_Operation operation = static_cast<IO_Operation>( pBuffer->GetUserData() );
				
				switch ( operation )
				{
				case IO_Read_Request:
					
					Read( pSocket, pBuffer );
					
					break;
					
				case IO_Read_Completed :
					
					if ( 0 != dwIoSize )
					{
						pBuffer->Use( dwIoSize );
						
						//DEBUG_ONLY( Output(_T("RX: ") + ToString(pBuffer) + _T("\n") + DumpData(reinterpret_cast<const BYTE*>( pBuffer->GetWSABUF()->buf), dwIoSize, 40) ) );
						
						/*
						 * Call to unqualified virtual function
						 */
						ReadCompleted( pSocket, pBuffer );
					}
					else
					{
						/*
						 * client connection dropped...
						 */
						
						Output( _T("ReadCompleted - 0 bytes - client connection dropped") );
						
						closeSocket = true;
					}
					
					pSocket->Release();
					pBuffer->Release();
					
					break;
					
				case IO_Write_Request :
					
					Write( pSocket, pBuffer );
					
					if ( dwIoSize != 0 )
					{
						/*
						 * final write, now shutdown send side of connection
						 */
						pSocket->Shutdown( SD_SEND );
					}
					
					break;
					
				case IO_Write_Completed :
					
					pBuffer->Use( dwIoSize );
					
					//DEBUG_ONLY( Output(_T("TX: ") + ToString(pBuffer) + _T("\n") + DumpData(reinterpret_cast<const BYTE*>( pBuffer->GetWSABUF()->buf), dwIoSize, 40) ) );
					
					/*
					 * Call to unqualified virtual function
					 */
					WriteCompleted( pSocket, pBuffer );
					
					pSocket->Release();
					pBuffer->Release();
					
					break;
					
				case IO_Close :
					
					AbortiveClose( pSocket );
					
					pSocket->Release();
					pBuffer->Release();
					
					break;
					
				default :
					/*
					 * all to unqualified virtual function
					 */
					OnError( _T("CSocketServer::WorkerThread::Run() - Unexpected operation") );
					break;
				} 
			}
			else
			{
				/*
				 * Call to unqualified virtual function
				 */
				OnError( _T("CSocketServer::WorkerThread::Run() - Unexpected - pBuffer is 0") );
			}
			
			if ( closeSocket )
			{
				pSocket->Close();
			}
			
			/*
			 * Call to unqualified virtual function
			 */
			OnEndProcessing();
      } 
   }
   catch(const CException &e)
   {
	   /*
	    * Call to unqualified virtual function
		*/
	   OnError( _T("CSocketServer::WorkerThread::Run() - Exception: ") + e.GetWhere() + _T(" - ") + e.GetMessage() );
   }
   catch(...)
   {
	   /*
	    * Call to unqualified virtual function
		*/
	   OnError( _T("CSocketServer::WorkerThread::Run() - Unexpected exception") );
   }
   
   return 0;
}
Ejemplo n.º 17
0
void CVwRegressor::load_regressor(char* file)
{
	CIOBuffer source;
	int32_t fd = source.open_file(file, 'r');

	if (fd < 0)
		SG_SERROR("Unable to open file for loading regressor!\n");

	// Read version info
	size_t v_length;
	source.read_file((char*)&v_length, sizeof(v_length));
	char t[v_length];
	source.read_file(t,v_length);
	if (strcmp(t,env->vw_version) != 0)
		SG_SERROR("Regressor source has an incompatible VW version!\n");

	// Read min and max label
	source.read_file((char*)&env->min_label, sizeof(env->min_label));
	source.read_file((char*)&env->max_label, sizeof(env->max_label));

	// Read num_bits, multiple sources are not supported
	size_t local_num_bits;
	source.read_file((char *)&local_num_bits, sizeof(local_num_bits));

	if ((size_t) env->num_bits != local_num_bits)
		SG_SERROR("Wrong number of bits in regressor source!\n");

	env->num_bits = local_num_bits;

	size_t local_thread_bits;
	source.read_file((char*)&local_thread_bits, sizeof(local_thread_bits));

	env->thread_bits = local_thread_bits;

	int32_t len;
	source.read_file((char *)&len, sizeof(len));

	// Read paired namespace information
	DynArray<char*> local_pairs;
	for (; len > 0; len--)
	{
		char pair[3];
		source.read_file(pair, sizeof(char)*2);
		pair[2]='\0';
		local_pairs.push_back(pair);
	}

	env->pairs = local_pairs;

	// Initialize the weight vector
	if (weight_vectors)
		SG_FREE(weight_vectors);
	init(env);

	size_t local_ngram;
	source.read_file((char*)&local_ngram, sizeof(local_ngram));
	size_t local_skips;
	source.read_file((char*)&local_skips, sizeof(local_skips));

	env->ngram = local_ngram;
	env->skips = local_skips;

	// Read individual weights
	size_t stride = env->stride;
	while (true)
	{
		uint32_t hash;
		ssize_t hash_bytes = source.read_file((char *)&hash, sizeof(hash));
		if (hash_bytes <= 0)
			break;

		float32_t w = 0.;
		ssize_t weight_bytes = source.read_file((char *)&w, sizeof(float32_t));
		if (weight_bytes <= 0)
			break;

		size_t num_threads = env->num_threads();

		weight_vectors[hash % num_threads][(hash*stride)/num_threads]
			= weight_vectors[hash % num_threads][(hash*stride)/num_threads] + w;
	}
	source.close_file();
}
Ejemplo n.º 18
0
int CSocketClient::Run()
{
	try
	{		
		HANDLE handlesToWaitFor[2];
		
		handlesToWaitFor[0] = m_shutdownEvent.GetEvent();
		handlesToWaitFor[1] = m_successConnectionsEvent.GetEvent();
		
		while ( !m_shutdownEvent.Wait( 0 ) )
		{
			DWORD waitResult = ::WaitForMultipleObjects( 2, handlesToWaitFor, false, INFINITE );
			
			if ( waitResult == WAIT_OBJECT_0 )
			{
				/*
				 * Time to shutdown
				 */
				break;
			}
			else if ( waitResult == WAIT_OBJECT_0 + 1 )
			{
				/*
				 * Allocate a buffer for required read
				 */
				CIOBuffer *pReadContext = Allocate();

				while ( !m_shutdownEvent.Wait( 0 ) && m_successConnectionsEvent.Wait( 0 ) )
				{
					if ( m_eventSelect.WaitForEnumEvent( m_connectSocket, 1000 ) )
					{
						/*
						 * Find some events and process it
						 */
						
						/*
						 * A event to connect
						 */
						if ( m_eventSelect.IsConnect() )
						{
							OnConnect();						
						}

						/*
						 * A event to close
						 */
						if ( m_eventSelect.IsClose() )
						{
							OnClose();						
						}

						/*
						 * A event to read
						 */
						if ( m_eventSelect.IsRead() )
						{
							OnRead( pReadContext );
						}

						/*
						 * A event to write
						 */
						if ( m_eventSelect.IsWrite() )
						{
							OnWrite();
						}
					}
				} // while (...

				pReadContext->Release();
			}
			else
			{
				/*
				 * Call to unqualified virtual function
				 */
				OnError( _T("CSocketClient::Run() - WaitForMultipleObjects: ") + GetLastErrorMessage( ::GetLastError() ) );
			}
			
		} // while ( ... 		
	}
	catch( const CException &e )
	{
		/*
		 * Call to unqualified virtual function
		 */
		OnError( _T("CSocketClient::Run() - Exception: ") + e.GetWhere() + _T(" - ") + e.GetMessage() );
	}
	catch(...)
	{
		/*
		 * Call to unqualified virtual function
		 */
		OnError( _T("CSocketClient::Run() - Unexpected exception") );
	}
	
	/*
	 * Call to unqualified virtual function
	 */
	OnShutdownComplete();
	
	return 0;
}
Ejemplo n.º 19
0
void CThreadPoolWorkerThread::Process(
	ULONG_PTR completionKey,
	DWORD operation,
	OVERLAPPED *pOverlapped)
{
	CSocketServer::Socket *pSocket = reinterpret_cast<CSocketServer::Socket *>(completionKey);
	CIOBuffer *pBuffer = static_cast<CIOBuffer *>(pOverlapped);

	try
	{
		switch (operation)
		{
		case CThreadPool::ConnectionEstablished:

			OnConnectionEstablished(pSocket, pBuffer);

			break;

		case CThreadPool::ReadCompleted:

			ProcessMessage(pSocket, pBuffer);

			break;

		case CThreadPool::ConnectionClosing:

			OnConnectionClosing(pSocket);

			break;

		case CThreadPool::ConnectionClosed:

			OnConnectionClosed(pSocket);

			break;

		default:

			// do nothing

			break;
		}
	}
	catch (const CException &e)
	{
		Output(_T("Process - Exception - ") + e.GetWhere() + _T(" - ") + e.GetMessage());
		std::string strMsg = e.GetWhere() + _T(" - ") + e.GetMessage();
		pSocket->Write(strMsg.c_str(), strMsg.size());
		pSocket->Shutdown();
	}
	catch (...)
	{
		Output(_T("Process - Unexpected exception"));
		std::string strMsg = _T("Process - Unexpected exception");
		pSocket->Write(strMsg.c_str(), strMsg.size());
		pSocket->Shutdown();
	}

	pSocket->Release();

	if (pBuffer)
	{
		pBuffer->Release();
	}
}
Ejemplo n.º 20
0
int CSocketServer::Run()
{
	try
	{
		vector<WorkerThread *> workers;
		
		workers.reserve( m_numThreads );
		
		for ( size_t i = 0; i < m_numThreads; ++i )
		{
			/*
			 * Call to unqualified virtual function
			 */
			WorkerThread *pThread = CreateWorkerThread( m_iocp ); 
			
			workers.push_back( pThread );
			
			pThread->Start();
		}
		
		HANDLE handlesToWaitFor[2];
		
		handlesToWaitFor[0] = m_shutdownEvent.GetEvent();
		handlesToWaitFor[1] = m_acceptConnectionsEvent.GetEvent();
		
		while ( !m_shutdownEvent.Wait( 0 ) )
		{
			DWORD waitResult = ::WaitForMultipleObjects( 2, handlesToWaitFor, false, INFINITE );
			
			if ( waitResult == WAIT_OBJECT_0 )
			{
				/*
				 * Time to shutdown
				 */
				break;
			}
			else if ( waitResult == WAIT_OBJECT_0 + 1 )
			{
				/*
				 * accept connections
				 */
				
				while ( !m_shutdownEvent.Wait( 0 ) && m_acceptConnectionsEvent.Wait( 0 ) )
				{
					CIOBuffer *pAddress = Allocate();
					
					int addressSize = (int)pAddress->GetSize();
					
					SOCKET acceptedSocket = ::WSAAccept(
									m_listeningSocket, 
									reinterpret_cast<sockaddr *>(const_cast<BYTE *>( pAddress->GetBuffer() ) ), 
									&addressSize, 
									0, 
									0);
					
					pAddress->Use( addressSize );
					
					if ( acceptedSocket != INVALID_SOCKET )
					{
						Socket *pSocket = AllocateSocket( acceptedSocket );
						
						/*
						 * Call to unqualified virtual function
						 */
						OnConnectionEstablished( pSocket, pAddress );
					}
					else if ( m_acceptConnectionsEvent.Wait( 0 ) )
					{
						/*
						 * Call to unqualified virtual function
						 */
						OnError( _T("CSocketServer::Run() - WSAAccept:") + GetLastErrorMessage( ::WSAGetLastError() ) );
					}
					
					pAddress->Release();
				}
			}
			else
			{
				/*
				 * Call to unqualified virtual function
				 */
				OnError( _T("CSocketServer::Run() - WaitForMultipleObjects: ") + GetLastErrorMessage( ::GetLastError() ) );
			}
		}
		
		for ( i = 0; i < m_numThreads; ++i )
		{
			workers[i]->InitiateShutdown();
		}  
		
		for ( i = 0; i < m_numThreads; ++i )
		{
			workers[i]->WaitForShutdownToComplete();
			
			delete workers[i];
			
			workers[i] = 0;
		}  
	}
	catch( const CException &e )
	{
		/*
		 * Call to unqualified virtual function
		 */
		OnError( _T("CSocketServer::Run() - Exception: ") + e.GetWhere() + _T(" - ") + e.GetMessage() );
	}
	catch(...)
	{
		/*
		 * Call to unqualified virtual function
		 */
		OnError( _T("CSocketServer::Run() - Unexpected exception") );
	}
	
	/*
	 * Call to unqualified virtual function
	 */
	OnShutdownComplete();
	
	return 0;
}