aboutsummaryrefslogtreecommitdiff
path: root/src/tests/multiserver.cpp
blob: f22147d2eda3ab2c17671f29e53fa65d9b4b518b (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
#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;
}