forked from RetroAchievements/RAWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.php
More file actions
135 lines (114 loc) · 3.45 KB
/
echo.php
File metadata and controls
135 lines (114 loc) · 3.45 KB
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
<pre><?php
//header( 'Content-type: text/html; charset=utf-8' );
set_time_limit( 60 * 5 ); // ( 60 * 60 * 24 );
ob_implicit_flush( TRUE );
define('SOCK_MSG_DELIM',"_*_");
$testServer = stream_socket_server( 'tcp://127.0.0.1:45456', $errno, $errstr );
stream_set_blocking( $testServer, 0 );
function echoAndFlush( $str )
{
echo $str . "\n";
ob_flush();
}
function FormatSocketMessage( $type, $myUID, $content )
{
return "$type,$myUID,$content" . SOCK_MSG_DELIM;
}
function SocketWrite( $connection, $type, $msg = '' )
{
$socketName = stream_socket_get_name( $connection, TRUE );
$msgData = "$type,0,$msg";
echoAndFlush( "[ SND to $socketName ]:$msgData" );
fputs( $connection, $msgData . SOCK_MSG_DELIM );
}
function SplitPacket( $packetData, &$msgType, &$msgUID, &$msgData )
{
//echoAndFlush( $packetData );
$splitData = explode( ',', $packetData );
$msgType = $splitData[0]; // Type
$msgUID = $splitData[1]; // UID
$msgData = $splitData[2]; // Data (concat tbd?)
}
$connections = Array();
$chatQueue = new SplQueue();
while( true )
{
$selfName = stream_socket_get_name( $testServer, FALSE );
$numConnections = count( $connections );
echoAndFlush( "[ Ready ]: ($numConnections) at $selfName" );
// Read any new connections!
if( $conn = @stream_socket_accept( $testServer, 0.0 ) )
{
stream_set_blocking( $conn, 0 );
$socketName = stream_socket_get_name( $conn, TRUE );
if( !isset( $connections[ $socketName ] ) )
{
$connections[ $socketName ][ 'Connection' ] = $conn;
$connections[ $socketName ][ 'LastPing' ] = microtime( true );
$connections[ $socketName ][ 'UserID' ] = 0;
$chatQueue->push( "[SERVER]: $socketName has joined" );
}
}
// Receive messages from all attached clients
foreach( $connections as $nextConnName => &$nextConn )
{
$conn = $nextConn[ 'Connection' ];
$message = fread( $conn, 1024 );
$now = microtime( true );
if( strlen( $message ) > 1 )
{
echoAndFlush( "[ RCV from $nextConnName ]:$message" );
$msgsToParse = explode( SOCK_MSG_DELIM, $message );
foreach( $msgsToParse as $nextMsg )
{
if( strlen( $nextMsg ) < 2 )
continue;
SplitPacket( $nextMsg, $msgType, $msgUID, $msgData );
if( strncmp( $msgType, "PING", 4 ) == 0 )
{
$nextConn[ 'LastPing' ] = $now;
SocketWrite( $conn, "PONG" );
}
else if( strncmp( $msgType, "HELO", 4 ) == 0 )
{
// UID Decl
$nextConn[ 'UserID' ] = $msgData;
echoAndFlush( "[ Storing UID ]:$nextConnName is $msgData" );
}
else if( strncmp( $msgType, "MSG", 3 ) == 0 )
{
$newChatMsg = $nextConn[ 'UserID' ] . ": $msgData";
echoAndFlush( "[ Adding Chat Msg ]:$newChatMsg" );
$chatQueue->push( $newChatMsg );
}
else
{
// Unrecognised msg type?
echoAndFlush( "[ Warning ]:Unrecognised msg type '$msgType'" );
}
}
}
if( ( $now - $nextConn[ 'LastPing' ] ) > 8.0 )
{
// Drop: too long has passed since last heartbeat
fclose( $conn );
echoAndFlush( "[ Dropping Connection ]:$nextConnName" );
unset( $connections[ $nextConnName ] ); // safe?
continue;
}
}
// Push out any required messages to all listeners
while( !$chatQueue->IsEmpty() )
{
// Push this message to all listeners
foreach( $connections as $nextConnName => &$nextConn )
{
$conn = $nextConn[ 'Connection' ];
SocketWrite( $conn, "MSG", $chatQueue->top() );
}
$chatQueue->pop();
}
$timeInterval = 0.033; // 0.033s
usleep( 1000000 * $timeInterval );
}
?></pre>