This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncTCPServer.php
137 lines (110 loc) · 3.73 KB
/
AsyncTCPServer.php
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
<?php namespace Async\TCP;
function eventReadCallback($bufferEvent, $connection) {
$cb = $connection->dataCallback;
if(!$cb)
return;
$dataArray = array();
while($data = event_buffer_read($bufferEvent, 256)) {
$dataArray[] = $data;
}
$cb(implode(NULL, $dataArray));
};
function eventWriteCallback($bufferEvent, $connection) {
$connection->writePending = FALSE;
if($connection->closePending) {
$connection->close();
}
};
function eventErrorCallback($bufferEvent, $events, $connection) {
$connection->close();
$cb = $connection->disconnectCallback;
if($cb) {
$cb();
}
};
class Connection {
public $socket;
public $eventBuffer;
public $tcpServer;
public $dataCallback;
public $disconnectCallback;
public $writePending = FALSE;
public $closePending = FALSE;
function __construct($socket, $server) {
$this->socket = $socket;
$this->tcpServer = $server;
stream_set_blocking($socket, 0);
$this->eventBuffer = event_buffer_new(
$socket, // File descriptor to watch
'\Async\TCP\eventReadCallback', // Read event callback
'\Async\TCP\eventWriteCallback', // Write event callback
'\Async\TCP\eventErrorCallback', // Error callback
$this // Custom data to provide to callback
);
event_buffer_base_set($this->eventBuffer, $server->eventBase);
// event_buffer_timeout_set($this->eventBuffer, 30, 30);
event_buffer_watermark_set($this->eventBuffer, EV_READ | EV_WRITE, 0, 0xffffff);
event_buffer_priority_set($this->eventBuffer, 10);
event_buffer_enable($this->eventBuffer, EV_READ | EV_WRITE | EV_PERSIST);
}
function write($bytes) {
$this->writePending = TRUE;
event_buffer_write($this->eventBuffer, $bytes);
}
function close() {
if(!$this->writePending)
$this->_close();
else {
echo "WAITING FOR WRITE\n";
$this->closePending = TRUE;
}
}
function _close() {
event_buffer_disable($this->eventBuffer, EV_READ | EV_WRITE);
event_buffer_free($this->eventBuffer);
fclose($this->socket);
unset($this->eventBuffer, $this->socket);
echo "TCP CONNECTION CLOSED\n";
}
function onData($function) {
$this->dataCallback = $function;
}
function onDisconnect($function) {
$this->disconnectCallback = $function;
}
}
class Server {
public $connectCallback;
public $socket;
public $address;
public $eventBase;
public $evAccept;
function __construct($address) {
$this->address = $address;
$this->eventBase = \event_base_new();
$this->evAccept = function($fd, $events, $server) {
$socket = stream_socket_accept($fd);
$connection = new Connection($socket, $server);
$cb = $server->connectCallback;
if($cb) {
$cb($connection);
}
};
}
function onConnect($function) {
$this->connectCallback = $function;
}
function run() {
$this->socket = stream_socket_server($this->address);
$event = event_new();
event_set(
$event, // The libevent Event object
$this->socket, // The file descriptor to watch
EV_READ|EV_PERSIST, // Watch for READ events and watch the event forever, not just once
$this->evAccept, // Call this function when the event happens
$this); // Pass this as the 3rd argument to the callback
event_base_set($event, $this->eventBase);
event_add($event);
event_base_loop($this->eventBase);
}
}