- 论坛徽章:
- 0
|
原帖由 queue 于 2007-12-11 18:57 发表 ![]()
没有实现类似之前的 SPServer 那样的一个框架吗?在目前的基础上,要实现类似 SPServer 那样的一个框架,应该不会很复杂了。
按目前的结构,继续使用 send_fd 的方式应该是最简单的做法。类似 UNP 的 27. ...
用 send_fd 的方法实现了一个 server 框架。
http://spprocpool.googlecode.com/files/spprocpool-0.2.src.tar.gz
为了与 UNP 27 章的其他类型 server 对比,实现了类似的一个测试案例 testinetserver 。
同时为了方便测试,把 UNP 的测试 client 也放入包里面了,改名为 testinetclient 。
UNP 书上列出的数据是指 server 端通过 getrusage 得到的时间统计,而不是 client 端统计到的响应时间。
为了更直观的观察,在 testinetclient 中加入了响应时间的统计。
- class SP_ProcUnpService : public SP_ProcInetService {
- public:
- SP_ProcUnpService() {}
- virtual ~SP_ProcUnpService() {}
- virtual void handle( int sockfd ) {
- int ntowrite;
- ssize_t nread;
- char line[MAXLINE], result[MAXN];
- for ( ; ; ) {
- if ( (nread = read(sockfd, line, MAXLINE)) == 0) {
- return; /* connection closed by other end */
- }
- /* line from client specifies #bytes to write back */
- ntowrite = atol(line);
- if ((ntowrite <= 0) || (ntowrite > MAXN)) {
- syslog( LOG_WARNING, "WARN: client request for %d bytes", ntowrite);
- exit( -1 );
- }
- SP_ProcPduUtils::writen(sockfd, result, ntowrite);
- }
- }
- };
- class SP_ProcUnpServiceFactory : public SP_ProcInetServiceFactory {
- public:
- SP_ProcUnpServiceFactory() {}
- virtual ~SP_ProcUnpServiceFactory() {}
- virtual SP_ProcInetService * create() const {
- return new SP_ProcUnpService();
- }
- };
- void sig_int(int signo)
- {
- SP_ProcPduUtils::print_cpu_time();
- kill( 0, SIGUSR1 );
- }
-
- int main( int argc, char * argv[] )
- {
- SP_ProcInetServer server( "", 1770, new SP_ProcUnpServiceFactory() );
- signal( SIGINT, sig_int );
- server.runForever();
- return 0;
- }
复制代码
[ 本帖最后由 iunknown 于 2007-12-16 19:59 编辑 ] |
|