-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
128 lines (113 loc) · 3.46 KB
/
server.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
<?php
// Configure Log4php
require('log4php/Logger.php');
Logger::configure('logging.xml');
// Logger for current script
$log = Logger::getLogger("app");
require __DIR__ . '/lib/Utils.php';
require __DIR__ . '/lib/Application.php';
// Parsing commandline options
$options = getopt("s:");
Application::set('app_dir', __DIR__);
// Load basic settings
Application::load_yml(__DIR__ . "/config/settings.yml");
// Load custom settings
if (array_key_exists("s", $options)) Application::load_yml($options['s']);
// Configure routes
Application::load_yml(__DIR__ . "/config/routes.yml");
// Configure objects
Application::set_objects(array(
"memcache" => function(){
$settings = Application::get('memcache');
Logger::getLogger("app")->debug("Instate memcache ".posix_getpid());
$mc = new Memcache();
$mc->addServer($settings['host'], $settings['port']);
return $mc;
},
"mysql" => function() {
$settings = Application::get('mysql');
Logger::getLogger("app")->debug("Instate mysql".posix_getpid());
$db = new mysqli(
$settings['host'],
$settings['username'],
$settings['password'],
$settings['database'],
$settings['port']
);
$db->set_charset($settings['charset']);
return $db;
},
));
$settings = Application::get('server');
$serv = new swoole_server($settings['host'], $settings['port']);
$serv->set($settings['settings']);
function onStart($serv)
{
$log = Logger::getLogger('app');
$log->info("Server launched,"
." master #{$serv->master_pid},"
." manager #{$serv->manager_pid}");
$serv->addtimer(1000);
$serv->addtimer(2000);
$serv->addtimer(3000);
}
function onReceive($serv, $fd, $from_id, $input)
{
$log = Logger::getLogger('app');
try {
// Verify
MessageHelper::verify($input);
// Decrypt
$input_decrypt = MessageHelper::decrypt($input);
Logger::getLogger('traffic')->info(' UP ' . $input_decrypt);
// Unpack
$input_unpacked = MessageHelper::unpack($input_decrypt);
$cmd = intval($input_unpacked['cmd']);
// Route
$routes = Application::get('routes');
if (array_key_exists($cmd, $routes))
{
$func = $routes[$cmd];
$func($serv, $fd, $from_id, $input_unpacked);
}
} catch (Exception $e) {
$log->error($e);
MessageHelper::send($serv, MessageHelper::MSG_ILLEGAL);
}
$serv->close($fd);
}
function onTimer($serv, $interval)
{
$log = Logger::getLogger('app');
switch ($interval) {
case 1000:
break;
case 2000:
break;
case 3000:
break;
default:
break;
}
}
function onWorkerStart($serv, $worker_id){
$log = Logger::getLogger('app');
$log->info("Worker launched. #".posix_getpid());
Utils::require_all('helpers');
Utils::require_all('controllers');
Utils::require_all('models');
}
function onConnect ($serv, $fd){
// Logger::getLogger("server")->debug("Client:Connect.");
}
$serv->on('start', 'onStart');
$serv->on('receive', 'onReceive');
$serv->on('timer', 'onTimer');
$serv->on('WorkerStart', 'onWorkerStart');
$serv->on('connect', 'onConnect');
$serv->on('close', function ($serv, $fd) {
// Logger::getLogger("server")->debug("Client: Close.");
});
$log->info('Launching server at '.$settings['host'].':'.$settings['port']);
$serv->start();
?>