-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
wsserver.php
executable file
·140 lines (118 loc) · 3.97 KB
/
wsserver.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
138
139
140
<?php
/**
* Barcode Buddy for Grocy
*
* PHP version 7
*
* LICENSE: This source file is subject to version 3.0 of the GNU General
* Public License v3.0 that is attached to this project.
*
* @author Marc Ole Bulling
* @copyright 2019 Marc Ole Bulling
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU GPL v3.0
* @since File available since Release 1.0
*/
/**
* This file starts the websocket server and needs to be called from the command line
* to work
*
* @author Marc Ole Bulling
* @copyright 2019 Marc Ole Bulling
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU GPL v3.0
* @since File available since Release 1.0
*/
require_once __DIR__ . '/incl/configProcessing.inc.php';
require_once __DIR__ . '/incl/websocketconnection.inc.php';
require_once __DIR__ . '/incl/internalChecking.inc.php';
if (checkExtensionsInstalled()["result"] != RESULT_ALL_INSTALLED) {
die("Not all required extensions are installed. Please run setup.php for more information.");
}
$address = '127.0.0.1';
$port = $CONFIG->PORT_WEBSOCKET_SERVER;
echo "Starting socket server on $address:$port\n";
set_time_limit(0);
ob_implicit_flush();
// current mode is stored
$currentBBMode = "Consume";
//Only these modes are allowed as input
$allowedModes = array("Consume", "Consume (spoiled)", "Purchase", "Open", "Inventory", "Quantity", "Add to shoppinglist");
//null var
$null = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
showErrorAndDie("socket_create()");
}
if (socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1) === false) {
showErrorAndDie("socket_set_option()");
}
if (socket_bind($socket, $address, $port) === false) {
showErrorAndDie("socket_bind()");
}
if (socket_listen($socket) === false) {
showErrorAndDie("socket_listen()");
}
$clients = array($socket);
while (true) {
//manage multiple connections
$changed = $clients;
//returns the socket resources in $changed array
socket_select($changed, $null, $null, $null, 0);
//check for new clients
if (in_array($socket, $changed)) {
$socket_new = socket_accept($socket); //accept new socket
$clients[] = $socket_new; //add socket to client array
$found_socket = array_search($socket, $changed);
unset($changed[$found_socket]);
}
//loop through all connected clients
foreach ($changed as $changed_socket) {
$buf = @socket_read($changed_socket, 1024);
if ($buf === false || $buf === "") { // check disconnected client
// remove client for $clients array
$found_socket = array_search($changed_socket, $clients);
unset($clients[$found_socket]);
} else {
// A message was received
$command = $buf[0];
$data = substr($buf, 1);
switch ($command) {
// Get mode
case '0':
sendMode();
break;
// Set mode
case '1':
if (in_array($data, $allowedModes)) {
$currentBBMode = $data;
}
sendMode();
break;
// Echo
case '2':
sendMessage('{"action":"echo","data":"' . $data . '"}');
break;
// Invalid command
default:
echo "Unknown command " . $buf;
}
}
}
}
function sendMode(): void {
global $currentBBMode;
sendMessage('{"action":"getmode","data":"4' . $currentBBMode . '"}');
}
function sendMessage(string $msg): void {
global $clients;
foreach ($clients as $changed_socket) {
@socket_write($changed_socket, $msg, strlen($msg));
}
}
/**
* @return never
*/
function showErrorAndDie(string $functionName): void {
echo $functionName . " failed. Reason: " . socket_strerror(socket_last_error()) . "\n";
sleep(5);
die();
}