提交 1aaa0760af89f641fd9a4cdc431744764ce96eea

作者 LJH 李佳桓
1 个父辈 2ec5f42d

add

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