forked from LasVegasCoder/Socket-Programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrinceSocket.Class.php
155 lines (128 loc) · 3.14 KB
/
PrinceSocket.Class.php
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
147
148
149
150
151
152
153
154
155
<?php
defined('PRINCE_SOCKET')or die('no direct connect');
/*
Desc: Socket Programming Class that can connect to any TCP/IP, UDP host and port, or any socket application interface.
Author: Prince Ademola Adeyemi
Contact: YourVegasPrince@gmail.com
FB: facebook.com/YourVegasPrince@gmail
*/
if( !class_exists( 'PrinceSocket' ) )
{
private static var $_singleton = null;
private var $_hostname;
private var $_port;
private var $_link;
private var $_error_no;
private var $_error_str;
private var $_persist = false;
private var $_timeout = 30;
private var $_waittime = 5;
private static function __construct()
{
}
public static function Singleton()
{
if( self::$_singleton == null || $_singleton != instanceof PrinceSocket )
{
self::$_singleton = new PrinceSocket();
}
return self::$_singleton;
}
public function _Connect( $host, $port )
{
$this->_hostname = ( !empty( $host ) ? $host : '' );
$this->_port = ( !empty( $port ) ? $port : '' );
$pconnect = ( !empty( $this->_persist ) ? "pfsockopen" : "fsockopen" );
$this->_timeout = 60;
$this->_waittime = 5;
if( $this->_hostname && $this->_port )
{
$this->_link = new $pconnect( $this->_hostname, $this->_port, $this->_error_no, $this->_error_str, $this->_timeout );
stream_set_blocking( $this->_link, 0 );
return $this->_link;
}
else {
return $this->PrinceError( 'Error occurred' );
}
}
private function PrinceError( $errorMsg )
{
if( !empty( $errorMsg ) )
{
throw new Exception( $errorMsg );
}
else{
//
}
return false;
}
private function PrinceCMD( $cmd, $timeout = null )
{
if( $this->_link != false )
{
if( empty( $cmd ))
{
return null;
}
// Set custom wait time for your command,
$this->_waittime = ( $timeout !== null ) ? $timeout : $this->_waittime;
// Wait 5 seconds to wait before sending another command... avoiding flood;
sleep( $this->_waittime);
$cmd .= "\r\n";
$result = fwrite( $this->_link, $cmd, strlen( $cmd ) );
if( $result != '' )
{
return $result;
}
}
else {
$this->PrinceError('NO LINK ERROR');
}
}
public function getResponse( $time = null )
{
$this->_waittime = ( $time !== null ) ? $time : 1;
if( $this->_link )
{
sleep( $this->_waittime );
$response = fread( $this->_link, 2048 );
if( $response != '' )
{
return $response;
}
}
else{
$this->PrinceError( 'NO LINK' );
}
}
public function _readLine()
{
$line = '';
if( $this->_line )
{
$line = fgets( $this->_line, 1024 );
if( strlen( $line >= 2 && substr( $line, -2 ) == "\r\n" || substr( $line, -1 ) == "\n" ) )
{
$line = rtrim( $line );
}
if( !empty( $line ) )
{
return $line;
}
}
}
public function getMultilinedResponse()
{
$data = '';
while(($tmp = $this->_readLine()) != '.')
{
if(substr($tmp, 0, 2) == '..')
{
$tmp = substr($tmp, 1);
}
$data .= $tmp."\r\n";
}
return substr($data, 0, -2);
}
}// end PrinceSocket
?>