aboutsummaryrefslogtreecommitdiff
path: root/src/tests/multiserver.cpp
blob: 85971b5963859a258352e5e2a59fd112ccd28b0a (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
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/multiserver.h"
#include "bu/protocol.h"
#include "bu/client.h"

class ProtocolRaw : public Bu::Protocol
{
public:
	virtual void onNewConnection( Bu::Client *pClient )
	{
		pClient->write("Raw echo\n");
	}

	virtual void onNewData( Bu::Client *pClient )
	{
		char buf[1024];
		while( pClient->hasInput() )
		{
			int iAmnt = pClient->read( buf, 1024 );
			pClient->write( buf, iAmnt );
		}
	}
};

class ProtocolRot13 : public Bu::Protocol
{
public:
	virtual void onNewConnection( Bu::Client *pClient )
	{
		pClient->write("Rot13 echo\n");
	}

	virtual void onNewData( Bu::Client *pClient )
	{
		while( pClient->hasInput() )
		{
			char sTmp[1024];
			int iAmnt = pClient->read( sTmp, 1024 );
			for( int j = 0; j < iAmnt; j++ )
			{
				if( sTmp[j] >= 'a' && sTmp[j] <= 'z' )
				{
					sTmp[j] = ((sTmp[j]-'a'+13)%26) + 'a';
				}
				else if( sTmp[j] >= 'A' && sTmp[j] <= 'Z' )
				{
					sTmp[j] = ((sTmp[j]-'A'+13)%26) + 'A';
				}
			}
			pClient->write( sTmp, iAmnt );
		}
	}
};

int main()
{
	Bu::MultiServer msMain;

	msMain.addProtocol( Bu::genProtocol<ProtocolRaw>, 5550 );
	msMain.addProtocol( Bu::genProtocol<ProtocolRot13>, 5551 );
	msMain.setTimeout( 5, 0 );

	for(;;)
	{
		msMain.scan();
	}

	return 0;
}