提交 1f50a4b425393e4795068b110a6f515fbc0d2196

作者 LJH 李佳桓
1 个父辈 cfe1508f

add

  1 +/*
  2 + * Copyright 2008 Stephen Liu
  3 + * For license terms, see the file COPYING along with this library.
  4 + */
  5 +
  6 +#ifndef __spiocpdispatcher_hpp__
  7 +#define __spiocpdispatcher_hpp__
  8 +
  9 +#include "spporting.hpp"
  10 +#include "spthread.hpp"
  11 +
  12 +class SP_CompletionHandler;
  13 +class SP_Handler;
  14 +class SP_Message;
  15 +class SP_BlockingQueue;
  16 +class SP_TimerHandler;
  17 +class SP_IOChannel;
  18 +class SP_Response;
  19 +
  20 +class SP_IocpEventArg;
  21 +class SP_IocpMsgQueue;
  22 +
  23 +class SP_IocpDispatcher {
  24 +public:
  25 + SP_IocpDispatcher( SP_CompletionHandler * completionHandler, int maxThreads = 64 );
  26 + ~SP_IocpDispatcher();
  27 +
  28 + void setTimeout( int timeout );
  29 +
  30 + int getSessionCount();
  31 + int getReqQueueLength();
  32 +
  33 + void shutdown();
  34 + int isRunning();
  35 +
  36 + /**
  37 + * @brief create a thread to run event loop
  38 + * @return 0 : OK, -1 : Fail, cannot create thread
  39 + */
  40 + int dispatch();
  41 +
  42 + /**
  43 + * @brief register a fd into dispatcher
  44 + * @param needStart : 1 - call handler::start, 0 - don't call handler::start
  45 + * @return 0 : OK, -1 : Fail, invalid fd
  46 + * @note handler will be deleted by dispatcher when the session is close
  47 + */
  48 + int push( int fd, SP_Handler * handler, int needStart = 1 );
  49 +
  50 + int push( int fd, SP_Handler * handler, SP_IOChannel * ioChannel, int needStart = 1 );
  51 +
  52 + /**
  53 + * @brief register a timer into dispatcher
  54 + * @param timeout : the interval for the timer
  55 + * @note handler will be deleted by dispatcher when the timer is terminated
  56 + */
  57 + int push( const struct timeval * timeout, SP_TimerHandler * handler );
  58 +
  59 + /**
  60 + * @brief push a response
  61 + */
  62 + int push( SP_Response * response );
  63 +
  64 +private:
  65 + int mIsShutdown;
  66 + int mIsRunning;
  67 + int mMaxThreads;
  68 +
  69 + SP_IocpEventArg * mEventArg;
  70 + SP_CompletionHandler * mCompletionHandler;
  71 +
  72 + SP_IocpMsgQueue * mPushQueue;
  73 +
  74 + int start();
  75 +
  76 + static sp_thread_result_t SP_THREAD_CALL eventLoop( void * arg );
  77 +
  78 + static void onPush( void * queueData, void * arg );
  79 +
  80 + static void outputCompleted( void * arg );
  81 +
  82 + static void onTimer( void * arg );
  83 + static void timer( void * arg );
  84 +};
  85 +
  86 +#endif
  87 +
... ...
注册登录 后发表评论