正在显示
1 个修改的文件
包含
94 行增加
和
0 行删除
src/server/spserver/testecho.cpp
0 → 100644
1 | +/* | ||
2 | + * Copyright 2007 Stephen Liu | ||
3 | + * For license terms, see the file COPYING along with this library. | ||
4 | + */ | ||
5 | + | ||
6 | +#include <stdio.h> | ||
7 | +#include <stdlib.h> | ||
8 | +#include <string.h> | ||
9 | +#include <signal.h> | ||
10 | +#include <assert.h> | ||
11 | + | ||
12 | +#include "spporting.hpp" | ||
13 | + | ||
14 | +#include "spmsgdecoder.hpp" | ||
15 | +#include "spbuffer.hpp" | ||
16 | + | ||
17 | +#include "spserver.hpp" | ||
18 | +#include "sphandler.hpp" | ||
19 | +#include "spresponse.hpp" | ||
20 | +#include "sprequest.hpp" | ||
21 | +#include "sputils.hpp" | ||
22 | + | ||
23 | +class SP_EchoHandler : public SP_Handler { | ||
24 | +public: | ||
25 | + SP_EchoHandler(){} | ||
26 | + virtual ~SP_EchoHandler(){} | ||
27 | + | ||
28 | + // return -1 : terminate session, 0 : continue | ||
29 | + virtual int start( SP_Request * request, SP_Response * response ) { | ||
30 | + request->setMsgDecoder( new SP_MultiLineMsgDecoder() ); | ||
31 | + response->getReply()->getMsg()->append( | ||
32 | + "Welcome to line echo server, enter 'quit' to quit.\r\n" ); | ||
33 | + | ||
34 | + return 0; | ||
35 | + } | ||
36 | + | ||
37 | + // return -1 : terminate session, 0 : continue | ||
38 | + virtual int handle( SP_Request * request, SP_Response * response ) { | ||
39 | + SP_MultiLineMsgDecoder * decoder = (SP_MultiLineMsgDecoder*)request->getMsgDecoder(); | ||
40 | + SP_CircleQueue * queue = decoder->getQueue(); | ||
41 | + | ||
42 | + int ret = 0; | ||
43 | + for( ; NULL != queue->top(); ) { | ||
44 | + char * line = (char*)queue->pop(); | ||
45 | + | ||
46 | + if( 0 != strcasecmp( line, "quit" ) ) { | ||
47 | + response->getReply()->getMsg()->append( line ); | ||
48 | + response->getReply()->getMsg()->append( "\r\n" ); | ||
49 | + } else { | ||
50 | + response->getReply()->getMsg()->append( "Byebye\r\n" ); | ||
51 | + ret = -1; | ||
52 | + } | ||
53 | + | ||
54 | + free( line ); | ||
55 | + } | ||
56 | + | ||
57 | + return ret; | ||
58 | + } | ||
59 | + | ||
60 | + virtual void error( SP_Response * response ) {} | ||
61 | + | ||
62 | + virtual void timeout( SP_Response * response ) {} | ||
63 | + | ||
64 | + virtual void close() {} | ||
65 | +}; | ||
66 | + | ||
67 | +class SP_EchoHandlerFactory : public SP_HandlerFactory { | ||
68 | +public: | ||
69 | + SP_EchoHandlerFactory() {} | ||
70 | + virtual ~SP_EchoHandlerFactory() {} | ||
71 | + | ||
72 | + virtual SP_Handler * create() const { | ||
73 | + return new SP_EchoHandler(); | ||
74 | + } | ||
75 | +}; | ||
76 | + | ||
77 | +//--------------------------------------------------------- | ||
78 | + | ||
79 | +int main( int argc, char * argv[] ) | ||
80 | +{ | ||
81 | + sp_openlog( "testecho", LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER ); | ||
82 | + | ||
83 | + int port = 3333; | ||
84 | + | ||
85 | + assert( 0 == sp_initsock() ); | ||
86 | + | ||
87 | + SP_Server server( "", port, new SP_EchoHandlerFactory() ); | ||
88 | + server.setMaxConnections( 10000 ); | ||
89 | + server.setReqQueueSize( 10000, "Server busy!" ); | ||
90 | + server.runForever(); | ||
91 | + | ||
92 | + return 0; | ||
93 | +} | ||
94 | + |
请
注册
或
登录
后发表评论