正在显示
1 个修改的文件
包含
174 行增加
和
0 行删除
src/server/spserver/testiocpecho.cpp
0 → 100644
1 | +/* | |
2 | + * Copyright 2008 Stephen Liu | |
3 | + * For license terms, see the file COPYING along with this library. | |
4 | + */ | |
5 | + | |
6 | +#include <winsock2.h> | |
7 | +#include <windows.h> | |
8 | +#include <stdio.h> | |
9 | +#include <assert.h> | |
10 | + | |
11 | +#define _CRTDBG_MAP_ALLOC | |
12 | +#include <stdlib.h> | |
13 | +#include <crtdbg.h> | |
14 | + | |
15 | +#include "spwin32iocp.hpp" | |
16 | +#include "spiocpserver.hpp" | |
17 | +#include "spiocplfserver.hpp" | |
18 | + | |
19 | +#include "spsession.hpp" | |
20 | +#include "spbuffer.hpp" | |
21 | +#include "spmsgdecoder.hpp" | |
22 | +#include "sprequest.hpp" | |
23 | +#include "sphandler.hpp" | |
24 | +#include "sputils.hpp" | |
25 | +#include "spgetopt.h" | |
26 | + | |
27 | +#pragma comment(lib,"ws2_32") | |
28 | +#pragma comment(lib,"mswsock") | |
29 | +#pragma comment(lib,"advapi32") | |
30 | + | |
31 | +class SP_EchoHandler : public SP_Handler { | |
32 | +public: | |
33 | + SP_EchoHandler(){} | |
34 | + virtual ~SP_EchoHandler(){} | |
35 | + | |
36 | + // return -1 : terminate session, 0 : continue | |
37 | + virtual int start( SP_Request * request, SP_Response * response ) { | |
38 | + request->setMsgDecoder( new SP_MultiLineMsgDecoder() ); | |
39 | + response->getReply()->getMsg()->append( | |
40 | + "Welcome to line echo server, enter 'quit' to quit.\r\n" ); | |
41 | + | |
42 | + return 0; | |
43 | + } | |
44 | + | |
45 | + // return -1 : terminate session, 0 : continue | |
46 | + virtual int handle( SP_Request * request, SP_Response * response ) { | |
47 | + SP_MultiLineMsgDecoder * decoder = (SP_MultiLineMsgDecoder*)request->getMsgDecoder(); | |
48 | + SP_CircleQueue * queue = decoder->getQueue(); | |
49 | + | |
50 | + int ret = 0; | |
51 | + for( ; NULL != queue->top(); ) { | |
52 | + char * line = (char*)queue->pop(); | |
53 | + | |
54 | + if( 0 != strcasecmp( line, "quit" ) ) { | |
55 | + response->getReply()->getMsg()->append( line ); | |
56 | + response->getReply()->getMsg()->append( "\r\n" ); | |
57 | + } else { | |
58 | + response->getReply()->getMsg()->append( "Byebye\r\n" ); | |
59 | + ret = -1; | |
60 | + } | |
61 | + | |
62 | + free( line ); | |
63 | + } | |
64 | + | |
65 | + return ret; | |
66 | + } | |
67 | + | |
68 | + virtual void error( SP_Response * response ) {} | |
69 | + | |
70 | + virtual void timeout( SP_Response * response ) {} | |
71 | + | |
72 | + virtual void close() {} | |
73 | +}; | |
74 | + | |
75 | +class SP_EchoHandlerFactory : public SP_HandlerFactory { | |
76 | +public: | |
77 | + SP_EchoHandlerFactory() {} | |
78 | + virtual ~SP_EchoHandlerFactory() {} | |
79 | + | |
80 | + virtual SP_Handler * create() const { | |
81 | + return new SP_EchoHandler(); | |
82 | + } | |
83 | +}; | |
84 | + | |
85 | +void IncreaseConnections() | |
86 | +{ | |
87 | + SetProcessWorkingSetSize( GetCurrentProcess(), | |
88 | + 10 * 1024 * 1024, 400 * 1024 * 1024 ); | |
89 | + | |
90 | + DWORD minSize = 0, maxSize = 0; | |
91 | + GetProcessWorkingSetSize( GetCurrentProcess(), &minSize, &maxSize ); | |
92 | + printf( "WorkingSetSize min %d(%d), max %d(%d)\n", | |
93 | + minSize, minSize / 4096, maxSize, maxSize / 4096 ); | |
94 | + | |
95 | + HKEY hKey; | |
96 | + | |
97 | + /* http://support.microsoft.com/default.aspx?scid=kb;EN-US;314053 */ | |
98 | + printf("Writing to registry... \n"); | |
99 | + if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, | |
100 | + "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Winsock", 0, | |
101 | + KEY_WRITE, &hKey ) != ERROR_SUCCESS ) { | |
102 | + printf( "RegOpenKeyEx fail, errno %d\n", GetLastError() ); | |
103 | + } else { | |
104 | + DWORD dwCon = 0xfffffe; | |
105 | + if( RegSetValueEx( hKey, "TcpNumConnections", 0, REG_DWORD, | |
106 | + (const BYTE *) &dwCon, sizeof(dwCon) ) != ERROR_SUCCESS ) { | |
107 | + printf( "RegSetValueEx fail, errno %d\n", GetLastError() ); | |
108 | + } | |
109 | + RegCloseKey( hKey ); | |
110 | + } | |
111 | +} | |
112 | + | |
113 | +int main( int argc, char * argv[] ) | |
114 | +{ | |
115 | + int port = 3333, maxThreads = 4, maxConnections = 20000; | |
116 | + int timeout = 120, reqQueueSize = 10000; | |
117 | + const char * serverType = "lf"; | |
118 | + | |
119 | + extern char *optarg ; | |
120 | + int c ; | |
121 | + | |
122 | + while( ( c = getopt ( argc, argv, "p:t:o:c:q:s:v" )) != EOF ) { | |
123 | + switch ( c ) { | |
124 | + case 'p' : | |
125 | + port = atoi( optarg ); | |
126 | + break; | |
127 | + case 't': | |
128 | + maxThreads = atoi( optarg ); | |
129 | + break; | |
130 | + case 'c': | |
131 | + maxConnections = atoi( optarg ); | |
132 | + break; | |
133 | + case 'o': | |
134 | + timeout = atoi( optarg ); | |
135 | + break; | |
136 | + case 'q': | |
137 | + reqQueueSize = atoi( optarg ); | |
138 | + break; | |
139 | + case 's': | |
140 | + serverType = optarg; | |
141 | + break; | |
142 | + case '?' : | |
143 | + case 'v' : | |
144 | + printf( "Usage: %s [-p <port>] [-t <threads>] [-c <connections>] " | |
145 | + "[-o <timeout>] [-q <queue size>] [-s <hahs|lf>]\n", argv[0] ); | |
146 | + exit( 0 ); | |
147 | + } | |
148 | + } | |
149 | + | |
150 | + if( 0 != sp_initsock() ) assert( 0 ); | |
151 | + | |
152 | + //Warning: This modifies your operating system. Use it at your own risk. | |
153 | + //IncreaseConnections(); | |
154 | + | |
155 | + sp_syslog( LOG_DEBUG, "server type %s", serverType ); | |
156 | + | |
157 | + if( 0 == strcasecmp( serverType, "hahs" ) ) { | |
158 | + SP_IocpServer server( "", port, new SP_EchoHandlerFactory() ); | |
159 | + server.setTimeout( timeout ); | |
160 | + server.setMaxThreads( maxThreads ); | |
161 | + server.setReqQueueSize( reqQueueSize, "Byebye\r\n" ); | |
162 | + server.setMaxConnections( maxConnections ); | |
163 | + server.runForever(); | |
164 | + } else { | |
165 | + SP_IocpLFServer server( "", port, new SP_EchoHandlerFactory() ); | |
166 | + server.setTimeout( timeout ); | |
167 | + server.setMaxThreads( maxThreads ); | |
168 | + server.setReqQueueSize( reqQueueSize, "Byebye\r\n" ); | |
169 | + server.setMaxConnections( maxConnections ); | |
170 | + server.runForever(); | |
171 | + } | |
172 | + | |
173 | + return 0; | |
174 | +} | ... | ... |
请
注册
或
登录
后发表评论