Sleipnir
src/serverclient.cpp
00001 /*****************************************************************************
00002 * This file is provided under the Creative Commons Attribution 3.0 license.
00003 *
00004 * You are free to share, copy, distribute, transmit, or adapt this work
00005 * PROVIDED THAT you attribute the work to the authors listed below.
00006 * For more information, please see the following web page:
00007 * http://creativecommons.org/licenses/by/3.0/
00008 *
00009 * This file is a component of the Sleipnir library for functional genomics,
00010 * authored by:
00011 * Curtis Huttenhower (chuttenh@princeton.edu)
00012 * Mark Schroeder
00013 * Maria D. Chikina
00014 * Olga G. Troyanskaya (ogt@princeton.edu, primary contact)
00015 *
00016 * If you use this library, the included executable tools, or any related
00017 * code in your work, please cite the following publication:
00018 * Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and
00019 * Olga G. Troyanskaya.
00020 * "The Sleipnir library for computational functional genomics"
00021 *****************************************************************************/
00022 #include "stdafx.h"
00023 #include "serverclient.h"
00024 #include "server.h"
00025 
00026 namespace Sleipnir {
00027 
00028 void* CServerClientImpl::StartRoutine( void* pArg ) {
00029     CServerClientImpl*  pClient;
00030 
00031     pClient = (CServerClientImpl*)pArg;
00032     pClient->StartRoutine( );
00033     delete pClient;
00034 
00035     return NULL; }
00036 
00037 void CServerClientImpl::StartRoutine( ) {
00038 
00039     do
00040         ReadMessage( );
00041     while( m_vecbMessage.size( ) && m_pClient->ProcessMessage( m_vecbMessage ) );
00042 
00043     g_CatSleipnir( ).info( "Client 0x%08x disconnected", m_pClient ); }
00044 
00045 CServerClientImpl::CServerClientImpl( SOCKET iSocket, IServerClient* pClient ) : m_iSocket(iSocket),
00046     m_pClient(pClient) { }
00047 
00048 CServerClientImpl::~CServerClientImpl( ) {
00049 
00050     closesocket( m_iSocket );
00051     m_pClient->Destroy( ); }
00052 
00053 void CServerClientImpl::ReadMessage( ) {
00054     unsigned char   acBuffer[ c_iBuffer ];
00055     size_t          iRead, iTotal, iPrev;
00056 
00057     m_vecbMessage.clear( );
00058     if( !( iRead = recv( m_iSocket, (char*)acBuffer, c_iBuffer, 0 ) ) || ( iRead == -1 ) ||
00059         ( iRead < sizeof(uint32_t) ) )
00060         return;
00061     iTotal = *(uint32_t*)acBuffer;
00062     m_vecbMessage.resize( max( iTotal, iPrev = iRead - sizeof(uint32_t) ) );
00063     copy( acBuffer + sizeof(uint32_t), acBuffer + iRead, m_vecbMessage.begin( ) );
00064     while( ( iPrev < iTotal ) && ( iRead = recv( m_iSocket, (char*)acBuffer, c_iBuffer, 0 ) ) ) {
00065         if( ( iPrev + iRead ) >= m_vecbMessage.size( ) )
00066             m_vecbMessage.resize( iPrev + iRead );
00067         copy( acBuffer, acBuffer + iRead, m_vecbMessage.begin( ) + iPrev );
00068         iPrev += iRead; } }
00069 
00070 }