@@ -39,80 +39,92 @@ protected function createConnection($resource)
39
39
}
40
40
41
41
/**
42
- * Main server method. Listens for connections, handles connectes/disconnectes, e.g.
42
+ * Main server loop
43
+ *
44
+ * This method does not return.
43
45
*/
44
46
public function run ()
45
47
{
46
- while (true )
48
+ while (true ) {
49
+ $ this ->processSockets ();
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Selects all active sockets for read events, and processes those whose
55
+ * state changes. Listens for connections, handles connects/disconnects, e.g.
56
+ *
57
+ * @return void
58
+ */
59
+ protected function processSockets ()
60
+ {
61
+ $ changed_sockets = $ this ->allsockets ;
62
+ @stream_select ($ changed_sockets , $ write = null , $ except = null , 0 , 5000 );
63
+ foreach ($ changed_sockets as $ socket )
47
64
{
48
- $ changed_sockets = $ this ->allsockets ;
49
- @stream_select ($ changed_sockets , $ write = null , $ except = null , 0 , 5000 );
50
- foreach ($ changed_sockets as $ socket )
65
+ if ($ socket == $ this ->master )
51
66
{
52
- if ($ socket == $ this ->master )
67
+ if (( $ resource = stream_socket_accept ( $ this ->master )) === false )
53
68
{
54
- if (($ resource = stream_socket_accept ($ this ->master )) === false )
55
- {
56
- $ this ->log ('Socket error: ' . socket_strerror (socket_last_error ($ resource )));
57
- continue ;
58
- }
59
- else
60
- {
61
- $ client = $ this ->createConnection ($ resource );
62
- $ this ->clients [(int )$ resource ] = $ client ;
63
- $ this ->allsockets [] = $ resource ;
64
-
65
- if (count ($ this ->clients ) > $ this ->_maxClients )
66
- {
67
- $ client ->onDisconnect ();
68
- if ($ this ->getApplication ('status ' ) !== false )
69
- {
70
- $ this ->getApplication ('status ' )->statusMsg ('Attention: Client Limit Reached! ' , 'warning ' );
71
- }
72
- continue ;
73
- }
74
-
75
- $ this ->_addIpToStorage ($ client ->getClientIp ());
76
- if ($ this ->_checkMaxConnectionsPerIp ($ client ->getClientIp ()) === false )
77
- {
78
- $ client ->onDisconnect ();
79
- if ($ this ->getApplication ('status ' ) !== false )
80
- {
81
- $ this ->getApplication ('status ' )->statusMsg ('Connection/Ip limit for ip ' . $ client ->getClientIp () . ' was reached! ' , 'warning ' );
82
- }
83
- continue ;
84
- }
85
- }
69
+ $ this ->log ('Socket error: ' . socket_strerror (socket_last_error ($ resource )));
70
+ continue ;
86
71
}
87
72
else
88
73
{
89
- $ client = $ this ->clients [(int )$ socket ];
90
- if (!is_object ($ client ))
91
- {
92
- unset($ this ->clients [(int )$ socket ]);
93
- continue ;
94
- }
95
- $ data = $ this ->readBuffer ($ socket );
96
- $ bytes = strlen ($ data );
74
+ $ client = $ this ->createConnection ($ resource );
75
+ $ this ->clients [(int )$ resource ] = $ client ;
76
+ $ this ->allsockets [] = $ resource ;
97
77
98
- if ($ bytes === 0 )
78
+ if (count ( $ this -> clients ) > $ this -> _maxClients )
99
79
{
100
80
$ client ->onDisconnect ();
81
+ if ($ this ->getApplication ('status ' ) !== false )
82
+ {
83
+ $ this ->getApplication ('status ' )->statusMsg ('Attention: Client Limit Reached! ' , 'warning ' );
84
+ }
101
85
continue ;
102
86
}
103
- elseif ($ data === false )
104
- {
105
- $ this ->removeClientOnError ($ client );
106
- continue ;
107
- }
108
- elseif ($ client ->waitingForData === false && $ this ->_checkRequestLimit ($ client ->getClientId ()) === false )
87
+
88
+ $ this ->_addIpToStorage ($ client ->getClientIp ());
89
+ if ($ this ->_checkMaxConnectionsPerIp ($ client ->getClientIp ()) === false )
109
90
{
110
91
$ client ->onDisconnect ();
92
+ if ($ this ->getApplication ('status ' ) !== false )
93
+ {
94
+ $ this ->getApplication ('status ' )->statusMsg ('Connection/Ip limit for ip ' . $ client ->getClientIp () . ' was reached! ' , 'warning ' );
95
+ }
96
+ continue ;
111
97
}
112
- else
113
- {
114
- $ client ->onData ($ data );
115
- }
98
+ }
99
+ }
100
+ else
101
+ {
102
+ $ client = $ this ->clients [(int )$ socket ];
103
+ if (!is_object ($ client ))
104
+ {
105
+ unset($ this ->clients [(int )$ socket ]);
106
+ continue ;
107
+ }
108
+ $ data = $ this ->readBuffer ($ socket );
109
+ $ bytes = strlen ($ data );
110
+
111
+ if ($ bytes === 0 )
112
+ {
113
+ $ client ->onDisconnect ();
114
+ continue ;
115
+ }
116
+ elseif ($ data === false )
117
+ {
118
+ $ this ->removeClientOnError ($ client );
119
+ continue ;
120
+ }
121
+ elseif ($ client ->waitingForData === false && $ this ->_checkRequestLimit ($ client ->getClientId ()) === false )
122
+ {
123
+ $ client ->onDisconnect ();
124
+ }
125
+ else
126
+ {
127
+ $ client ->onData ($ data );
116
128
}
117
129
}
118
130
}
0 commit comments