aboutsummaryrefslogtreecommitdiff
path: root/src/tests/multiserver.cpp
blob: c4a00aba4a58bf82df992d8f030454d37087151a (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
/*
 * Copyright (C) 2007-2008 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 )
	{
		pClient->write( pClient->getInput() );
		pClient->seek( pClient->getInputSize() );
	}
};

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

	virtual void onNewData( Bu::Client *pClient )
	{
		Bu::FString sTmp = pClient->getInput();
		for( int j = 0; j < sTmp.getSize(); 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 );
		pClient->seek( pClient->getInputSize() );
	}
};

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;
}