Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 146 lines (117 sloc) 3.482 kB
abcc31c rewrite
michael authored
1 <?php
2
3 require 'const.php';
4 require 'client.php';
148b39c started on channels and whois
michael authored
5 require 'modes.php';
abcc31c rewrite
michael authored
6 require 'channel.php';
7 require 'command.php';
8 require 'log.php';
9
10 require 'mod_mem.php';
11
12 // Sockets
13 $sockets = Array(
14 // Listening sockets
15 'listen' => Array(),
16 // Client sockets
17 'clients' => Array()
18 );
19
20 // Client classes
21 $clients = Array();
22 // Channels classes
23 $channels = Array();
24
25 $config = Array(
26 'listen' =>
27 Array(
28 Array(
29 'ip' => '127.0.0.1',
30 'port' => '6667'
31 ),
32 Array(
33 'ip' => '127.0.0.1',
34 'port' => '6668'
35 )
36 ),
37 'hostname' => 'localhost',
38 'network' => 'localhost'
39 );
40
41 if(!isset($config['listen']) || empty($config['listen'])) {
42 Log::write(Log::Error, 'You must define at least one listen block.');
43 exit(1);
44 }
45
46 // Listen on each block
47 foreach($config['listen'] as &$listen) {
48 Log::write(Log::Debug, "Trying to listen on {$listen['ip']}:{$listen['port']}");
49
50 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
51 if(!@socket_bind($socket, $listen['ip'], $listen['port'])) {
52 Log::write(Log::Error, "Couldn't bind to {$listen['ip']}:{$listen['port']}! " . socket_strerror(socket_last_error()));
53 exit(1);
54 }
55
56 if(!@socket_listen($socket)) {
57 Log::write(Log::Error, "Couldn't listen on {$listen['ip']}:{$listen['port']}! " . socket_strerror(socket_last_error()));
58 exit(1);
59 }
60
61 // Append to list of sockets to poll
62 $sockets['listen'][] = $socket;
63 }
64
65 // Handle a shutdown
66 function shutdown() {
67 global $sockets;
68 Log::write(Log::Info, 'Server going down...');
69
70 // Close ALL sockets
71 foreach($sockets['listen'] as &$socket) {
72 socket_close($socket);
73 }
74
75 foreach($sockets['clients'] as &$socket) {
76 // TODO: Maybe announce that the server is going down
77 socket_close($socket);
78 }
79
80 // Exit with no error
81 //exit(0);
82 }
83
84 // Add callback
85 register_shutdown_function('shutdown');
86
87 // Ignore abort
88 //ignore_user_abort(true);
89
90 $write = Array();
91 $except = Array();
92
93 while(true) {
94 // Read from the listening sockets and the clients
95 // We want to make a duplicate array in-case socket_select() decides to modify something (which it does)
96 $read = array_merge($sockets['listen'], $sockets['clients']);
97
98 if(@socket_select($read, $write, $except, 0) === false) {
99 // Something failed
100 Log::write(Log::Error, "Couldn't poll sockets! " . socket_strerror(socket_last_error()));
101 }
102
103 // Itterate through sockets with data
104 foreach($read as &$socket) {
105 if(in_array($socket, $sockets['listen'])) {
106 // This is a listen socket
107
108 // Accept the connection
109 $client = socket_accept($socket);
110 // Append to poll, create client and append client to the $clients array
111 $clients[] = new Client($sockets['clients'][] = $client);
112 //
113 // = new Client(&$socket);
114 } else {
115 $data = @socket_read($socket, 1024, PHP_NORMAL_READ);
116 if($data === false) {
117 // Connection closed
118
119 // TODO: Handle this.
120 } else {
121 // New data!
122
123 $data = trim($data, "\r\n");
124
125 if(empty($data))
126 continue; // Useless to us.
127
128 if(($client = Client::find($socket)) === false) {
129 // We received data from a client that was disconnected... What?
2f8a7bc ..
michael authored
130 // If coded right, this SHOULD NEVER HAPPEN
abcc31c rewrite
michael authored
131 // TODO
132 } else {
133 // Send client the command
134 $client->command($data);
135 }
136
137 }
138 }
139 }
140
141 // Comment out this line if you want to use a lot of CPU
2f8a7bc ..
michael authored
142 usleep(100);
abcc31c rewrite
michael authored
143 }
144 ?>
145
Something went wrong with that request. Please try again.