push sockt&epoll samples
[socket_samples.git] / socketserver.h
blob134a12e1efab0a683eba161cd0cc374293a0586a
1 #ifndef __Q_SOCKET_SERVER__
2 #define __Q_SOCKET_SERVER__
3 #include <errno.h>
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #include <netinet/tcp.h>
7 #include <arpa/inet.h>
8 #include <sys/types.h>
9 #include <string.h>
10 #include <sys/epoll.h>
11 #include <pthread.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h>
17 #define SOCKET_ERROR -1
18 #define INVALID_SOCKET -1
19 typedef int SOCKET;
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
41 typedef struct {
42 WORD passive;
43 WORD port;//uint16_t
44 WORD minThreads;
45 WORD maxThreads;
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;
54 SOCKET server_s;
55 SOCKADDR_IN serv_addr;
56 int epollFD;//main epoller.
57 int BSendEpollFD;//For blocking send.
58 }QSocketServer;
60 typedef struct {
61 SOCKET client_s;
62 SOCKADDR_IN client_addr;
63 uint32_t curEvents;
65 char buf[MAX_BUF_SIZE];
66 DWORD numberOfBytesTransferred;
67 char * data;
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);
77 #endif