-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.php
executable file
·136 lines (94 loc) · 4.24 KB
/
start.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
<?php
/**
* ++++++++++++++++++++++++++
* # This file is a part of #
* # NoiLab. Licensed under #
* # MIT, use it under the #
* # license. #
* ++++++++++++++++++++++++++
*
* @author xtl<[email protected]>
* @license MIT
* @package xtlsoft/noilab
*
*/
namespace NoiLab;
require_once("vendor/autoload.php");
use \Workerman\Worker;
use \Workerman\WebServer;
use \Workerman\Connection\TcpConnection;
$App = new \NoiLab\Application();
$App->_config(__DIR__. "/config.json");
$web = new WebServer("http://" . $App->config->server->web);
$web->addRoot("*", __DIR__. "/web/");
$web->name = "WebServer";
$sock = new Worker("websocket://" . $App->config->server->socket);
$sock->name = "WebSocket";
$sock->count = 3;
$sock->onMessage = function($conn, $msg) use ($App){
if(substr($msg, 0, 4) == "_ID_"){
//$conn->send(substr($msg, 4));
$conn->id = substr($msg, 5);
$conn->stdin = false;
return;
}
if($msg == "_COMPLIE_"){
$out = $App->config->complie->temp . $conn->id . ".o";
$in = $App->config->complie->temp . $conn->id . ".cpp";
$cmd = $App->config->complie->path . " $in -o $out";
$descriptorspec = array(
0 => array('pty'),
1 => array('pty'),
2 => array('pty')
);
$env = array_merge(
array('COLUMNS'=>78, 'LINES'=> 18), $_SERVER
);
$proc = @proc_open($cmd, $descriptorspec, $pipes, null, $env);
stream_set_blocking($pipes[0], 0);
$conn->send("+ Compile.Start\r\n");
$stdout = new TcpConnection($pipes[1]);
$stdout->onMessage = function($c, $m) use ($conn){
$conn->send($m);
};
$stdin = new TcpConnection($pipes[2]);
$stdin->onMessage = function($c, $m) use ($conn){
$conn->send($m);
};
$stdout->onClose = function ($c) use ($conn, $proc, $stdin, $descriptorspec, $env, $out){
$stdin->close();
proc_terminate($proc);
$stat = proc_get_status($proc)['exitcode'];
if($stat == 0){
$conn->send("OK!\r\n+ Compile.End\r\n");
$proc = @proc_open($out, $descriptorspec, $pipes, null, $env);
$start = microtime();
stream_set_blocking($pipes[0], 0);
$conn->send("+ Running.Start\r\n\r\n");
$stdout = new TcpConnection($pipes[1]);
$stdout->onMessage = function($c, $m) use ($conn){
$conn->send($m);
};
$stdout->onClose = function($c) use ($conn, $proc, $stdin, $start){
$stdin->close();
proc_terminate($proc);
$stat = proc_get_status($proc)['exitcode'];
$time = microtime()- $start;
$conn->send("\r\n+ Running.End with return $stat in $time s.\r\n\r\n");
};
$conn->stdin = $pipes[0];
$stdin = new TcpConnection($pipes[2]);
$stdin->onMessage = function($c, $m) use ($conn){
$conn->send($m);
};
}else{
$conn->send("+ Compile.Error\r\n");
}
};
return;
}
if($conn->stdin){
@fwrite($conn->stdin, $msg);
}
};
Worker::runAll();