-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_socket.php
More file actions
146 lines (136 loc) · 2.87 KB
/
class_socket.php
File metadata and controls
146 lines (136 loc) · 2.87 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
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
*
* @author tobi.schaefer@gmail.com
* @package Gameserver Query
* @copyright (c) 2014 Tobias Schäfer
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
class socket
{
private $socket = null;
/**
* Connect to a socket
*
* @param string $ip
* IP to connect
* @param string $port
* Port to connect
* @param string $network_protocol
* tcp or udp? default:udp
* @return bool true if connection success
* @access public
*/
public function connect($ip, $port, $network_protocol = 'udp')
{
$ip = gethostbyname($ip);
$port = (int) $port;
isset($this->query_counter['socket']) ? $this->query_counter['socket']++ : ($this->query_counter['socket'] = 1);
if($network_protocol == 'tcp')
{
$context = stream_context_create();
if($socket = stream_socket_client("tcp://{$ip}:{$port}", $errno, $error, (1 / 10), STREAM_CLIENT_CONNECT, $context))
{
$this->socket = $socket;
return true;
}
else
{
die('Failed to open socket');
}
}
else
{
if($socket = fsockopen('udp://' . $ip, $port, $errno, $error, 3))
{
socket_set_blocking($socket, false);
$this->socket = $socket;
return true;
}
else
{
die('Failed to open socket');
}
}
}
/**
* Close a socket
*
* @param resource $socket
* the socket to close
* @access public
*/
public function close()
{
if(is_resource($this->socket))
{
fclose($this->socket);
}
}
/**
* Send data to a socket
*
* @param resource $socket
* socket to send to
* @param string $data
* data to send
* @access public
*/
public function write($data)
{
if(is_resource($this->socket))
{
return fwrite($this->socket, $data, strlen($data));
}
}
/**
* Read data from a socket
*
* @param resource $socket
* socket to read from
* @param int $timeout
* timeout im ms
* @param string $size
* size of the data to read in byte
* @return string Data recived from socket
* @access public
*/
public function read($timeout = 500, $size = 8192)
{
if(is_resource($this->socket))
{
$loops = 0;
$starttime = microtime(true);
$read = array($this->socket);
$null = null;
/*$a = stream_get_meta_data($this->socket);
print_r($a);
$result = stream_socket_recvfrom($this->socket, 1024);
//die($result);
*/
while(($t = $timeout * 1000 - (microtime(true) - $starttime) * 10000) > 0)
{
$s = stream_select($read, $null, $null, 0, $t);
if(($s === false || $s <= 0) || ++$loops > 200)
{
break;
}
if($size > 8192)
{
$buffer = '';
while(! feof($this->socket))
{
$buffer .= fgets($this->socket);
}
$result = trim($buffer);
}
else
{
$result = stream_socket_recvfrom($this->socket, $size);
}
return $result;
}
}
}
}