Skip to content

Commit 10e125f

Browse files
author
svyrydenko
committed
All existing functionality moved to Generators
1 parent 46c8809 commit 10e125f

File tree

5 files changed

+61
-42
lines changed

5 files changed

+61
-42
lines changed

README.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ require 'vendor/autoload.php';
3838
## Simple WebSocket server
3939

4040
```php
41+
use Generator;
42+
use Ollyxar\LaravelAuth\FileAuth;
4143
use Ollyxar\WebSockets\{
42-
Server as WServer,
43-
Ssl as Wsl,
44-
Worker as Handler,
45-
Frame as WFrame
44+
Frame,
45+
Worker,
46+
Dispatcher
4647
};
4748

4849
class MyHandler extends Handler
@@ -60,30 +61,32 @@ class MyHandler extends Handler
6061

6162
/**
6263
* @param $client
64+
* @return Generator
6365
*/
64-
protected function onConnect($client): void
66+
protected function onConnect($client): Generator
6567
{
66-
$this->sendToAll(WFrame::encode(json_encode([
68+
yield Dispatcher::make($this->sendToAll(WFrame::encode(json_encode([
6769
'type' => 'system',
6870
'message' => 'User {' . (int)$client . '} Connected.'
69-
])));
71+
]))));
7072
}
7173

7274
/**
7375
* @param $clientNumber
7476
*/
75-
protected function onClose($clientNumber): void
77+
protected function onClose($clientNumber): Generator
7678
{
77-
$this->sendToAll(WFrame::encode(json_encode([
79+
yield Dispatcher::make($this->sendToAll(WFrame::encode(json_encode([
7880
'type' => 'system',
7981
'message' => "User {$clientNumber} disconnected."
80-
])));
82+
]))));
8183
}
8284

8385
/**
8486
* @param string $message
87+
* @return Generator
8588
*/
86-
protected function onDirectMessage(string $message): void
89+
protected function onDirectMessage(string $message): Generator
8790
{
8891
$message = json_decode($message);
8992
$userName = $message->name;
@@ -95,7 +98,7 @@ class MyHandler extends Handler
9598
'message' => $userMessage
9699
]));
97100

98-
$this->sendToAll($response);
101+
yield Dispatcher::make($this->sendToAll($response));
99102
}
100103
}
101104

src/Dispatcher.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private function poll($timeout): void
5959
}
6060

6161
foreach ($read as $socket) {
62-
list(, $jobs) = $this->read[(int)$socket];
62+
$jobs = $this->read[(int)$socket][1];
6363
unset($this->read[(int)$socket]);
6464

6565
foreach ($jobs as $job) {
@@ -68,7 +68,7 @@ private function poll($timeout): void
6868
}
6969

7070
foreach ($write as $socket) {
71-
list(, $jobs) = $this->write[(int)$socket];
71+
$jobs = $this->write[(int)$socket][1];
7272
unset($this->write[(int)$socket]);
7373

7474
foreach ($jobs as $job) {
@@ -140,9 +140,14 @@ public function add(Generator $process): self
140140
return $this;
141141
}
142142

143-
public static function make(Generator $process) {
143+
/**
144+
* @param Generator $process
145+
* @return SysCall
146+
*/
147+
public static function make(Generator $process): SysCall
148+
{
144149
return new SysCall(
145-
function(Job $job, Dispatcher $dispatcher) use ($process) {
150+
function (Job $job, Dispatcher $dispatcher) use ($process) {
146151
$job->value($dispatcher->add($process));
147152
$dispatcher->enqueue($job);
148153
}
@@ -153,10 +158,10 @@ function(Job $job, Dispatcher $dispatcher) use ($process) {
153158
* @param $socket
154159
* @return SysCall
155160
*/
156-
public static function listenRead($socket)
161+
public static function listenRead($socket): SysCall
157162
{
158163
return new SysCall(
159-
function(Job $job, Dispatcher $dispatcher) use ($socket) {
164+
function (Job $job, Dispatcher $dispatcher) use ($socket) {
160165
$dispatcher->appendRead($socket, $job);
161166
}
162167
);
@@ -166,10 +171,10 @@ function(Job $job, Dispatcher $dispatcher) use ($socket) {
166171
* @param $socket
167172
* @return SysCall
168173
*/
169-
public static function listenWrite($socket)
174+
public static function listenWrite($socket): SysCall
170175
{
171176
return new SysCall(
172-
function(Job $job, Dispatcher $dispatcher) use ($socket) {
177+
function (Job $job, Dispatcher $dispatcher) use ($socket) {
173178
$dispatcher->appendWrite($socket, $job);
174179
}
175180
);

src/Master.php

+29-20
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
*/
99
final class Master
1010
{
11+
/**
12+
* @var array
13+
*/
1114
private $workers = [];
15+
16+
/**
17+
* Unix socket connector
18+
*/
1219
private $connector;
1320

1421
/**
@@ -25,45 +32,46 @@ public function __construct($workers, $connector)
2532

2633
/**
2734
* @param $client
28-
* @param $data
2935
* @return Generator
3036
*/
31-
protected function write($client, $data): Generator
37+
protected function read($client): Generator
3238
{
33-
yield Dispatcher::listenWrite($client);
34-
fwrite($client, $data);
35-
}
39+
yield Dispatcher::listenRead($client);
3640

37-
protected function listenWorker($socket): Generator
38-
{
39-
yield Dispatcher::listenRead($socket);
40-
yield Dispatcher::listenWrite($socket);
41-
42-
$data = Frame::decode($socket);
41+
$data = Frame::decode($client);
4342

4443
if (!$data['opcode']) {
4544
return yield;
4645
}
4746

48-
stream_select($read, $this->workers, $except, 0);
49-
5047
foreach ($this->workers as $worker) {
51-
if ($worker !== $socket) {
52-
dump('sending to worker # '.(int)$worker);
48+
if ($worker !== $client) {
49+
yield Dispatcher::listenWrite($worker);
5350
yield Dispatcher::make($this->write($worker, Frame::encode($data['payload'], $data['opcode'])));
5451
}
5552
}
53+
54+
yield Dispatcher::make($this->read($client));
55+
}
56+
57+
/**
58+
* @param $client
59+
* @param $data
60+
* @return Generator
61+
*/
62+
protected function write($client, $data): Generator
63+
{
64+
yield Dispatcher::listenWrite($client);
65+
fwrite($client, $data);
5666
}
5767

5868
/**
5969
* @return Generator
6070
*/
6171
protected function listenWorkers(): Generator
6272
{
63-
while (true) {
64-
foreach ($this->workers as $worker) {
65-
yield Dispatcher::make($this->listenWorker($worker));
66-
}
73+
foreach ($this->workers as $worker) {
74+
yield Dispatcher::make($this->read($worker));
6775
}
6876
}
6977

@@ -83,6 +91,7 @@ protected function listenConnector(): Generator
8391
}
8492

8593
foreach ($this->workers as $worker) {
94+
yield Dispatcher::listenWrite($worker);
8695
yield Dispatcher::make($this->write($worker, Frame::encode($data['payload'], $data['opcode'])));
8796
}
8897
}
@@ -98,7 +107,7 @@ public function dispatch(): void
98107
{
99108
(new Dispatcher())
100109
->add($this->listenConnector())
101-
//->add($this->listenWorkers())
110+
->add($this->listenWorkers())
102111
->dispatch();
103112
}
104113
}

src/Server.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Ollyxar\WebSockets;
22

3-
use \Exception;
3+
use Exception;
44

55
/**
66
* Class Server
@@ -146,6 +146,7 @@ public function setPassPhrase(string $passPhrase = 'abracadabra'): self
146146
* Launching server
147147
*
148148
* @return void
149+
* @throws Exception
149150
*/
150151
public function run(): void
151152
{

src/Worker.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ protected function read($client): Generator
9898
case Frame::CLOSE: {
9999
yield Dispatcher::make($this->onClose((int)$client));
100100
unset($this->clients[(int)$client]);
101+
fclose($client);
101102
break;
102103
}
103104
case Frame::PING: {
@@ -138,7 +139,7 @@ protected function accept($client): Generator
138139
fclose($client);
139140
} else {
140141
$this->clients[(int)$client] = $client;
141-
$this->onConnect($client);
142+
yield Dispatcher::make($this->onConnect($client));
142143

143144
yield Dispatcher::make($this->read($client));
144145
}

0 commit comments

Comments
 (0)