1 #ifndef __Q_SOCKET_SERVER__
2 #define __Q_SOCKET_SERVER__
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #include <netinet/tcp.h>
10 #include <sys/epoll.h>
17 #define SOCKET_ERROR -1
18 #define INVALID_SOCKET -1
20 typedef struct sockaddr_in SOCKADDR_IN
;
21 typedef unsigned short WORD
;
22 typedef unsigned int DWORD
;
24 #define QSS_SIO_KEEPALIVE_VALS_TIMEOUT 30*60
25 #define QSS_SIO_KEEPALIVE_VALS_INTERVAL 5
26 #define QSS_SIO_KEEPALIVE_VALS_COUNT 3
27 #define MAX_THREADS 100
28 #define MAX_THREADS_MIN 10
29 #define MIN_WORKER_WAIT_TIMEOUT 20*1000
30 #define MAX_WORKER_WAIT_TIMEOUT 60*MIN_WORKER_WAIT_TIMEOUT
31 #define MAX_THREADPOOLS 32
33 #define MAX_BUF_SIZE 1024
34 /* ulimit -n opened FDs per process.记得修改哦,否则还是select效果,就不是epoll效果了哦,呵呵*/
35 #define BLOCKING_SEND_TIMEOUT 20
37 typedef void (*CSocketLifecycleCallback
)(int cs
,int lifecycle
);//lifecycle:0:OnAccepted,-1:OnClose
38 typedef int (*BlockingSender_t
)(void * senderBase
,int cs
, void * buf
, size_t nbs
);
39 typedef int (*InternalProtocolHandler
)(struct epoll_event
* event
,BlockingSender_t _blockingSender
,void * senderBase
);//return -1:SOCKET_ERROR
46 pthread_spinlock_t g_spinlock
;//PTHREAD_PROCESS_PRIVATE
47 volatile int lifecycleStatus
;//0-created,1-starting, 2-running,3-stopping,4-exitSignaled,5-stopped
48 int workerWaitTimeout
;//wait timeout
49 volatile int workerCounter
;
50 volatile int currentBusyWorkers
;
51 volatile int CSocketsCounter
;
52 CSocketLifecycleCallback cslifecb
;
53 InternalProtocolHandler protoHandler
;
55 SOCKADDR_IN serv_addr
;
56 int epollFD
;//main epoller.
57 int BSendEpollFD
;//For blocking send.
62 SOCKADDR_IN client_addr
;
65 char buf
[MAX_BUF_SIZE
];
66 DWORD numberOfBytesTransferred
;
69 int BSendEpollFDRelated
;
70 pthread_mutex_t writableLock
;
71 pthread_cond_t writableMonitor
;
72 }QSSEPollEvent
;//for per connection
74 int createSocketServer(QSocketServer
** qss_ptr
,WORD passive
,WORD port
,CSocketLifecycleCallback cslifecb
,InternalProtocolHandler protoHandler
,WORD minThreads
,WORD maxThreads
,int workerWaitTimeout
);
75 int startSocketServer(QSocketServer
*qss
);
76 int shutdownSocketServer(QSocketServer
*qss
);