/
backend.php
69 lines (66 loc) · 2.14 KB
/
backend.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
<?php
error_reporting(E_ALL);
require_once('function.php');
require_once('queue.php');
require_once('actionclass.php');
$socketQueue = new actionQueue();
$socketQueue->initQueue();
$mainSocket = SocketOpen();
//socket_set_nonblock($mainSocket);
socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1);
while(TRUE)
{
if($socketQueue->len > 5)//Maximum count
continue; //Do not accept create new socket
//if($resSocket = socket_accept($mainSocket) && $resSocket != FALSE)
$resSocket = socket_accept($mainSocket);
//echo "Listening\n";
if($resSocket != FALSE)
{
$socketQueue->push($resSocket);
//socket_set_nonblock($resSocket);
}
if($socketQueue->isempty() == FALSE)
{
$currentSocket = $socketQueue->pop();
$rawMsg = SockRead($currentSocket);
echo "Raw Msg is $rawMsg\n";
if(Auth($rawMsg) == true)
{
$actionObj = ParseMsg($rawMsg);
//var_dump($actionObj);
$simpleResultObj = $actionObj->Compile();
if($simpleResultObj->resultno != 0)
{
echo "Compile Error\n";
SockWrite($currentSocket, "ERR");
SockWrite($currentSocket, $simpleResultObj->resultStr);
}
else
{
echo "Compile OK\n";
SockWrite($currentSocket, "OK");
$simpleResultObj = $actionObj->Run();
//Send Msg back to client
if($simpleResultObj->resultno == 0)
{
SockWrite($currentSocket, "OK");
SockWrite($currentSocket, $simpleResultObj->resultStr);
}
else
{
SockWrite($currentSocket, "ERR");
SockWrite($currentSocket, $simpleResultObj->resultStr);
}
}
}
else
{
$getaddr = "";
$getport = "";
socket_getsockname($currentSocket, $getaddr, $getport);
echo "Invalid request from $getaddr:$getport";
SockWrite($currentSocket, "FATAL");
}
}
}