/
SocketServer.c
147 lines (111 loc) · 3.22 KB
/
SocketServer.c
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Usage:
* ------
*
* Start several telnet clients:
* $ telnet localhost 1337
*
* Then type "info", "active" or "exit".
*/
#import <Main.h>
#import <Server.h>
#import <Socket.h>
#import <Logger.h>
#import <Integer.h>
size_t activeConn = 0;
// ----------------
// CustomConnection
// ----------------
#define self CustomConnection
class {
size_t id;
Logger *logger;
Connection_Client *client;
};
def(void, Init, Connection_Client *client, Logger *logger) {
this->id = activeConn;
activeConn++;
this->client = client;
this->logger = logger;
String strAddr = NetworkAddress_ToString(client->conn->addr);
String strPort = Integer_ToString(client->conn->addr.port);
String strFd = Integer_ToString(client->conn->fd);
Logger_Info(this->logger,
$("Incoming TCP connection from %:%, fd=%"),
strAddr.rd, strPort.rd, strFd.rd);
String_Destroy(&strAddr);
String_Destroy(&strPort);
String_Destroy(&strFd);
SocketConnection_Write(client->conn, $("Hi.\n"));
}
def(void, Destroy) {
String strAddr = NetworkAddress_ToString(this->client->conn->addr);
String strPort = Integer_ToString(this->client->conn->addr.port);
String strFd = Integer_ToString(this->client->conn->fd);
Logger_Info(this->logger,
$("Client %:%, fd=% disconnected"),
strAddr.rd, strPort.rd, strFd.rd);
String_Destroy(&strAddr);
String_Destroy(&strPort);
String_Destroy(&strFd);
activeConn--;
}
def(Connection_Status, OnData) {
String s = String_New(1024);
s.len = SocketConnection_Read(this->client->conn, s.buf, String_GetSize(s));
RdString input = String_Trim(s.rd);
String strFd = Integer_ToString(this->client->conn->fd);
Logger_Info(this->logger,
$("Received data from fd=%: '%'"), strFd.rd, input);
String_Destroy(&strFd);
if (String_Equals(input, $("info"))) {
String strId = Integer_ToString(this->id);
String resp = String_Format($("You have the ID %.\n"), strId.rd);
SocketConnection_Write(this->client->conn, resp.rd);
String_Destroy(&resp);
String_Destroy(&strId);
} else if (String_Equals(input, $("active"))) {
String strActive = Integer_ToString(activeConn);
String resp = String_Format( $("% active connection(s).\n"),
strActive.rd);
SocketConnection_Write(this->client->conn, resp.rd);
String_Destroy(&resp);
String_Destroy(&strActive);
} else if (String_Equals(input, $("exit"))) {
SocketConnection_Write(this->client->conn, $("Bye.\n"));
String_Destroy(&s);
return Connection_Status_Close;
}
String_Destroy(&s);
return Connection_Status_Open;
}
def(Connection_Status, OnPush) {
return Connection_Status_Open;
}
Impl(Connection) = {
.size = sizeof(self),
.init = ref(Init),
.destroy = ref(Destroy),
.pull = ref(OnData),
.push = ref(OnPush)
};
ExportImpl(Connection);
#undef self
// -----------
// Application
// -----------
#define self Application
def(bool, Run) {
Server server = Server_New(CustomConnection_GetImpl(), &this->logger);;
Server_SetEdgeTriggered(&server, false);
Server_Listen(&server, 1337);
try {
Logger_Info(&this->logger, $("Server started."));
EventLoop_Run(EventLoop_GetInstance());
} catch (Signal, SigInt) {
Logger_Info(&this->logger, $("Server shutdown."));
} finally {
Server_Destroy(&server);
} tryEnd;
return true;
}