Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C++

A Simple and Easy to Use IOCP Server Framework

Rate me:
Please Sign up or sign in to vote.
3.43/5 (8 votes)
30 Jun 2008LGPL33 min read 44.2K   1.7K   35  
An introduction to SPServer Framework
/*
 * Copyright 2008 Stephen Liu
 * For license terms, see the file COPYING along with this library.
 */

#ifndef __splfserver_hpp__
#define __splfserver_hpp__

#include "spthread.hpp"

class SP_IocpEventArg;
class SP_ThreadPool;
class SP_HandlerFactory;
class SP_CompletionHandler;
class SP_IOChannelFactory;

typedef struct tagSP_IocpAcceptArg SP_IocpAcceptArg_t;

// leader/follower thread pool server
class SP_IocpLFServer {
public:
	SP_IocpLFServer( const char * bindIP, int port, SP_HandlerFactory * handlerFactory );
	~SP_IocpLFServer();

	void setTimeout( int timeout );
	void setMaxConnections( int maxConnections );
	void setMaxThreads( int maxThreads );
	void setReqQueueSize( int reqQueueSize, const char * refusedMsg );
	void setIOChannelFactory( SP_IOChannelFactory * ioChannelFactory );

	void shutdown();
	int isRunning();

	// return -1 : cannot listen on ip:port, 0 : ok
	int run();

	void runForever();

private:
	char mBindIP[ 64 ];
	int mPort;
	int mIsShutdown;
	int mIsRunning;

	SP_IocpAcceptArg_t * mAcceptArg;

	SP_IocpEventArg * mEventArg;

	int mMaxThreads;
	SP_ThreadPool * mThreadPool;

	SP_CompletionHandler * mCompletionHandler;

	sp_thread_mutex_t mMutex;

	void handleOneEvent();

	static void lfHandler( void * arg );

	static void sigHandler( int, short, void * arg );

	static sp_thread_result_t SP_THREAD_CALL acceptThread( void * arg );
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions