spiocpdispatcher.hpp
1.9 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
/*
* Copyright 2008 Stephen Liu
* For license terms, see the file COPYING along with this library.
*/
#ifndef __spiocpdispatcher_hpp__
#define __spiocpdispatcher_hpp__
#include "spporting.hpp"
#include "spthread.hpp"
class SP_CompletionHandler;
class SP_Handler;
class SP_Message;
class SP_BlockingQueue;
class SP_TimerHandler;
class SP_IOChannel;
class SP_Response;
class SP_IocpEventArg;
class SP_IocpMsgQueue;
class SP_IocpDispatcher {
public:
SP_IocpDispatcher( SP_CompletionHandler * completionHandler, int maxThreads = 64 );
~SP_IocpDispatcher();
void setTimeout( int timeout );
int getSessionCount();
int getReqQueueLength();
void shutdown();
int isRunning();
/**
* @brief create a thread to run event loop
* @return 0 : OK, -1 : Fail, cannot create thread
*/
int dispatch();
/**
* @brief register a fd into dispatcher
* @param needStart : 1 - call handler::start, 0 - don't call handler::start
* @return 0 : OK, -1 : Fail, invalid fd
* @note handler will be deleted by dispatcher when the session is close
*/
int push( int fd, SP_Handler * handler, int needStart = 1 );
int push( int fd, SP_Handler * handler, SP_IOChannel * ioChannel, int needStart = 1 );
/**
* @brief register a timer into dispatcher
* @param timeout : the interval for the timer
* @note handler will be deleted by dispatcher when the timer is terminated
*/
int push( const struct timeval * timeout, SP_TimerHandler * handler );
/**
* @brief push a response
*/
int push( SP_Response * response );
private:
int mIsShutdown;
int mIsRunning;
int mMaxThreads;
SP_IocpEventArg * mEventArg;
SP_CompletionHandler * mCompletionHandler;
SP_IocpMsgQueue * mPushQueue;
int start();
static sp_thread_result_t SP_THREAD_CALL eventLoop( void * arg );
static void onPush( void * queueData, void * arg );
static void outputCompleted( void * arg );
static void onTimer( void * arg );
static void timer( void * arg );
};
#endif