-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_tcp_server.zig
174 lines (128 loc) · 4.63 KB
/
example_tcp_server.zig
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const std = @import("std");
const pike = @import("pike.zig");
const os = std.os;
const mem = std.mem;
const net = std.net;
const log = std.log;
const heap = std.heap;
const atomic = std.atomic;
pub const ClientQueue = atomic.Queue(*Client);
pub const Client = struct {
socket: pike.Socket,
address: net.Address,
frame: @Frame(Client.run),
fn run(self: *Client, server: *Server, notifier: *const pike.Notifier) !void {
var node = ClientQueue.Node{ .data = self };
server.clients.put(&node);
defer if (server.clients.remove(&node)) {
suspend {
self.socket.deinit();
server.allocator.destroy(self);
}
};
try self.socket.registerTo(notifier);
log.info("New peer {} has connected.", .{self.address});
defer log.info("Peer {} has disconnected.", .{self.address});
var reader = self.socket.reader();
var writer = self.socket.writer();
try writer.writeAll("Hello from the server!\n");
var buf: [1024]u8 = undefined;
while (true) {
const num_bytes = try reader.read(&buf);
if (num_bytes == 0) return;
const message = mem.trim(u8, buf[0..num_bytes], " \t\r\n");
log.info("Peer {} said: {s}", .{ self.address, message });
}
}
};
pub const Server = struct {
socket: pike.Socket,
clients: ClientQueue,
allocator: *mem.Allocator,
frame: @Frame(Server.run),
pub fn init(allocator: *mem.Allocator) !Server {
var socket = try pike.Socket.init(os.AF.INET, os.SOCK.STREAM, os.IPPROTO.TCP, 0);
errdefer socket.deinit();
try socket.set(.reuse_address, true);
return Server{
.socket = socket,
.clients = ClientQueue.init(),
.frame = undefined,
.allocator = allocator,
};
}
pub fn deinit(self: *Server) void {
self.socket.deinit();
await self.frame;
while (self.clients.get()) |node| {
node.data.socket.writer().writeAll("Server is shutting down! Good bye...\n") catch {};
node.data.socket.deinit();
await node.data.frame catch {};
self.allocator.destroy(node.data);
}
}
pub fn start(self: *Server, notifier: *const pike.Notifier, address: net.Address) !void {
try self.socket.bind(address);
try self.socket.listen(128);
try self.socket.registerTo(notifier);
self.frame = async self.run(notifier);
log.info("Listening for peers on: {}", .{try self.socket.getBindAddress()});
}
fn run(self: *Server, notifier: *const pike.Notifier) callconv(.Async) void {
defer log.debug("TCP server has shut down.", .{});
while (true) {
var conn = self.socket.accept() catch |err| switch (err) {
error.SocketNotListening,
error.OperationCancelled,
=> return,
else => {
log.err("Server - socket.accept(): {s}", .{@errorName(err)});
continue;
},
};
const client = self.allocator.create(Client) catch |err| {
log.err("Server - allocator.create(Client): {s}", .{@errorName(err)});
conn.socket.deinit();
continue;
};
client.socket = conn.socket;
client.address = conn.address;
client.frame = async client.run(self, notifier);
}
}
};
pub fn run(notifier: *const pike.Notifier, stopped: *bool) !void {
// Setup allocator.
var gpa: heap.GeneralPurposeAllocator(.{}) = .{};
defer _ = gpa.deinit();
// Setup signal handler.
var event = try pike.Event.init();
defer event.deinit();
try event.registerTo(notifier);
var signal = try pike.Signal.init(.{ .interrupt = true });
defer signal.deinit();
defer {
stopped.* = true;
event.post() catch unreachable;
}
// Setup TCP server.
var server = try Server.init(&gpa.allocator);
defer server.deinit();
// Start the server, and await for an interrupt signal to gracefully shutdown
// the server.
try server.start(notifier, net.Address.initIp4(.{ 0, 0, 0, 0 }, 0));
try signal.wait();
}
pub fn main() !void {
try pike.init();
defer pike.deinit();
const notifier = try pike.Notifier.init();
defer notifier.deinit();
var stopped = false;
var frame = async run(¬ifier, &stopped);
while (!stopped) {
try notifier.poll(1_000_000);
}
try nosuspend await frame;
log.debug("Successfully shut down.", .{});
}