testecho.cpp
2.1 KB
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
91
92
93
94
/*
* Copyright 2007 Stephen Liu
* For license terms, see the file COPYING along with this library.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include "spporting.hpp"
#include "spmsgdecoder.hpp"
#include "spbuffer.hpp"
#include "spserver.hpp"
#include "sphandler.hpp"
#include "spresponse.hpp"
#include "sprequest.hpp"
#include "sputils.hpp"
class SP_EchoHandler : public SP_Handler {
public:
SP_EchoHandler(){}
virtual ~SP_EchoHandler(){}
// return -1 : terminate session, 0 : continue
virtual int start( SP_Request * request, SP_Response * response ) {
request->setMsgDecoder( new SP_MultiLineMsgDecoder() );
response->getReply()->getMsg()->append(
"Welcome to line echo server, enter 'quit' to quit.\r\n" );
return 0;
}
// return -1 : terminate session, 0 : continue
virtual int handle( SP_Request * request, SP_Response * response ) {
SP_MultiLineMsgDecoder * decoder = (SP_MultiLineMsgDecoder*)request->getMsgDecoder();
SP_CircleQueue * queue = decoder->getQueue();
int ret = 0;
for( ; NULL != queue->top(); ) {
char * line = (char*)queue->pop();
if( 0 != strcasecmp( line, "quit" ) ) {
response->getReply()->getMsg()->append( line );
response->getReply()->getMsg()->append( "\r\n" );
} else {
response->getReply()->getMsg()->append( "Byebye\r\n" );
ret = -1;
}
free( line );
}
return ret;
}
virtual void error( SP_Response * response ) {}
virtual void timeout( SP_Response * response ) {}
virtual void close() {}
};
class SP_EchoHandlerFactory : public SP_HandlerFactory {
public:
SP_EchoHandlerFactory() {}
virtual ~SP_EchoHandlerFactory() {}
virtual SP_Handler * create() const {
return new SP_EchoHandler();
}
};
//---------------------------------------------------------
int main( int argc, char * argv[] )
{
sp_openlog( "testecho", LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER );
int port = 3333;
assert( 0 == sp_initsock() );
SP_Server server( "", port, new SP_EchoHandlerFactory() );
server.setMaxConnections( 10000 );
server.setReqQueueSize( 10000, "Server busy!" );
server.runForever();
return 0;
}