-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_context.h
109 lines (74 loc) · 1.99 KB
/
tg_context.h
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef TG_CONTEXT_H
#define TG_CONTEXT_H
#include "tg_service.h"
#define DEFAULT_MAX_CLIENTS 20
#define DEFAULT_LISTEN_PORT 6666
typedef struct{
int port;
char * listen_ip;
char * name;
int version;
int maxClients;
} TGServer;
typedef struct{
TGServer * pServer;
int logLevel;
int curClientCounts;
int listenfd;
//The descriptor is uesd to local administration communication
int localAdminfd;
} TGContext;
#ifdef LINUX
#include <pthread.h>
typedef pid_t tg_pid;
#elif WIN32
typedef int tg_pid;
#endif
typedef struct{
tg_pid pid;
int sockfd;
int readPipe;
int writePipe;
char * clientIP;
int port;
} TGClient;
extern TGClient ** g_clients;
extern TGContext * g_context;
extern struct TGService ** g_services;
void init();
void release();
void initClient(TGClient ** ppClient);
#define FIND_CLIENT_SUCCESS 0
#define FIND_CLIENT_NOT_FOUND -1
// find client in g_clients array
// according to process id. if find it, set pointer to ppClient.
// return 0 SUCCESS and find it
// reutrn -1 not found client
int findClient(const tg_pid pid, TGClient ** ppClient);
// add client to global clients array(g_clients)
// it will copy client data
void addClient(TGClient * pClient);
// close client connection
// and remove client from global clients array
void closeClient(const tg_pid pid);
// release client memory
// close client all sock descriptior
void destroyClient(TGClient * pCli);
//close all client
void closeAllClient();
//get activate client counts
int getCurrentClientCounts();
// get current connected client
// and return every client one line
// Note: after use data, must free ppClientInfo data
// pid ip port
void getCurrentClientsInfo(char ** ppCliInfo);
// send signal to client
// return 0 send successfully
// return -1 send error client pipe closed
int sendSignalToClient(TGClient * pCli,char * msg);
// send signal to all activite clients
// return 0 send successfully
// return >0 that failed client counts
int sendSignalToAllClients(char * msg);
#endif