summaryrefslogtreecommitdiffstats
path: root/Master/Masterarbeit/src/XorayaPluginExecutor/TcpServer.cpp
blob: 3ba91fd0489a638e3f53cdc8fea0e02c6e7ef3a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*!
 * \file TcpServer.cpp
 * \author S. Eisenhauer
 * \date 27.10.2011
 * \brief Implementation of CTcpServer
 */
#include <boost/bind.hpp>
#include "CPluginExecutor.h"
#include "global.h"
#include "TcpServer.h"

CTcpServer::CTcpServer(CPluginExecutor* pxExecutor
                       ,boost::asio::io_service& xIoService
                       ,int32_t i32Port)
:m_xAcceptor(
			 xIoService,
             boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),i32Port)
             )
 ,m_pxExecutor(pxExecutor)
 ,m_xIoService(xIoService)
{
	DEBUG_PRINT("entry");
    vStartAccept();
    DEBUG_PRINT("exit");
}

CTcpServer::~CTcpServer(void)
{
	DEBUG_PRINT("entry");
	vStop();
	DEBUG_PRINT("exit");
}

void CTcpServer::vStartAccept()
{
    CTcpConnection::tConnectionPtr pNewCon
        = CTcpConnection::pxCreate(xGetIoService(),m_pxExecutor);
		if(pNewCon)
		{
    	DEBUG_PRINT("new accept");
    }
    else
    {
    	ERROR_PRINT("could not get new connection object");
    	return;
  	}
    
    m_xAcceptor.async_accept(pNewCon->xGetSocket(),
        boost::bind(
            &CTcpServer::vAcceptHandler,
            this,
            pNewCon,
            boost::asio::placeholders::error
        )
    );
}

void CTcpServer::vAcceptHandler(CTcpConnection::tConnectionPtr pNewCon, const boost::system::error_code& error)
{
    if(!error)
    {
    	pNewCon->vSetActive(true);
        pNewCon->vWaitForClientRequest();
        vStartAccept();
    }
}
void CTcpServer::vStop()
{
	DEBUG_PRINT("entry");
	xGetIoService().stop();
	DEBUG_PRINT("exit");
}

void CTcpServer::vUploadLogMessage(const tstLogMessage& stLogMsg)
{
//	DEBUG_PRINT("entry");

	tConIter xConIter(g_astActiveConnections.begin());

	// find free memory for a new connection
	for(; xConIter != g_astActiveConnections.end(); ++xConIter)
	{
		CTcpConnection::tConnectionPtr pxCon = &(*xConIter);
		if(pxCon->boIsActive())
		{
			xGetIoService().post(boost::bind(&CTcpConnection::vUploadLogMessage, pxCon, stLogMsg));
		}
	}
//	DEBUG_PRINT("exit");
}