-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketUtility.php
More file actions
98 lines (91 loc) · 2.29 KB
/
SocketUtility.php
File metadata and controls
98 lines (91 loc) · 2.29 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
<?php
// Sends a buffer through a socket, ensuring the entire buffer is sent
function socket_send_buffer( &$sock, $buffer, $length )
{
while($length > 0)
{
// Attempt to send data
$sent = @socket_write( $sock, $buffer, $length );
if( $sent === false )
{
return false; // Sending failed
}
if( $sent < $length )
{
// If not all data was sent, update buffer and remaining length
$buffer = substr( $buffer, $sent );
$length -= $sent;
}
else
{
return true; // All data sent successfully
}
}
return false; // Fallback failure
}
// Receives a specific length of data from a socket
function socket_recv_buffer( &$sock, $length )
{
$all_buffer = "";
while( $length > 0 )
{
$buffer = "";
// Receive exact number of bytes requested using MSG_WAITALL
$count = @socket_recv( $sock, $buffer, $length, MSG_WAITALL );
if( $count == 0 )
{
return false; // Connection closed or error
}
$all_buffer .= $buffer;
$length -= $count;
}
return $all_buffer; // Return full received data
}
// Reads an unsigned 8-bit integer from the binary stream
function unpack_uint8( &$stream )
{
$val = unpack( "C", $stream );
$val = $val[1];
$stream = substr( $stream, 1 ); // Advance stream
return $val;
}
// Reads a signed 8-bit integer from the binary stream
function unpack_int8( &$stream )
{
$val = unpack( "c", $stream );
$val = $val[1];
$stream = substr( $stream, 1 );
return $val;
}
// Reads an unsigned 16-bit little-endian integer
function unpack_uint16( &$stream )
{
$val = unpack( "v", $stream );
$val = $val[1];
$stream = substr( $stream, 2 );
return $val;
}
// Reads an unsigned 32-bit little-endian integer
function unpack_uint32( &$stream )
{
$val = unpack( "V", $stream );
$val = $val[1];
$stream = substr( $stream, 4 );
return $val;
}
// Reads a 32-bit float value
function unpack_float( &$stream )
{
$val = unpack( "f", $stream );
$val = $val[1];
$stream = substr( $stream, 4 );
return $val;
}
// Reads a fixed-length ASCII string
function unpack_ASCII( &$stream, $len )
{
$val = implode("", unpack( "a" . $len, $stream ));
$stream = substr( $stream, $len );
return $val;
}
?>