1: <?php
2: /**
3: * This file is part of GameQ.
4: *
5: * GameQ is free software; you can redistribute it and/or modify
6: * it under the terms of the GNU Lesser General Public License as published by
7: * the Free Software Foundation; either version 3 of the License, or
8: * (at your option) any later version.
9: *
10: * GameQ is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this program. If not, see <http://www.gnu.org/licenses/>.
17: */
18:
19: namespace GameQ\Query;
20:
21: /**
22: * Core for the query mechanisms
23: *
24: * @author Austin Bischoff <austin@codebeard.com>
25: */
26: abstract class Core
27: {
28: /**
29: * The socket used by this resource
30: *
31: * @var null|resource
32: */
33: public $socket = null;
34:
35: /**
36: * The transport type (udp, tcp, etc...)
37: * See http://php.net/manual/en/transports.php for the supported list
38: *
39: * @var string
40: */
41: protected $transport = null;
42:
43: /**
44: * Connection IP address
45: *
46: * @var string
47: */
48: protected $ip = null;
49:
50: /**
51: * Connection port
52: *
53: * @var int
54: */
55: protected $port = null;
56:
57: /**
58: * The time in seconds to wait before timing out while connecting to the socket
59: *
60: * @var int
61: */
62: protected $timeout = 3; // Seconds
63:
64: /**
65: * Socket is blocking?
66: *
67: * @var bool
68: */
69: protected $blocking = false;
70:
71: /**
72: * Called when the class is cloned
73: */
74: public function __clone()
75: {
76: // Reset the properties for this class when cloned
77: $this->reset();
78: }
79:
80: /**
81: * Set the connection information for the socket
82: *
83: * @param string $transport
84: * @param string $ip
85: * @param int $port
86: * @param int $timeout seconds
87: * @param bool $blocking
88: */
89: public function set($transport, $ip, $port, $timeout = 3, $blocking = false)
90: {
91: $this->transport = $transport;
92:
93: $this->ip = $ip;
94:
95: $this->port = $port;
96:
97: $this->timeout = $timeout;
98:
99: $this->blocking = $blocking;
100: }
101:
102: /**
103: * Reset this instance's properties
104: */
105: public function reset()
106: {
107: $this->transport = null;
108:
109: $this->ip = null;
110:
111: $this->port = null;
112:
113: $this->timeout = 3;
114:
115: $this->blocking = false;
116: }
117:
118: public function getTransport()
119: {
120: return $this->transport;
121: }
122:
123: public function getIp()
124: {
125: return $this->ip;
126: }
127:
128: public function getPort()
129: {
130: return $this->port;
131: }
132:
133: public function getTimeout()
134: {
135: return $this->timeout;
136: }
137:
138: public function getBlocking()
139: {
140: return $this->blocking;
141: }
142:
143: /**
144: * Create a new socket
145: *
146: * @return void
147: */
148: abstract protected function create();
149:
150: /**
151: * Get the socket
152: *
153: * @return mixed
154: */
155: abstract public function get();
156:
157: /**
158: * Write data to the socket
159: *
160: * @param string $data
161: *
162: * @return int The number of bytes written
163: */
164: abstract public function write($data);
165:
166: /**
167: * Close the socket
168: *
169: * @return void
170: */
171: abstract public function close();
172:
173: /**
174: * Read the responses from the socket(s)
175: *
176: * @SuppressWarnings(PHPMD.UnusedFormalParameter)
177: *
178: * @param array $sockets
179: * @param int $timeout
180: * @param int $stream_timeout
181: *
182: * @return array
183: */
184: abstract public function getResponses(array $sockets, $timeout, $stream_timeout);
185: }
186: