PageRenderTime 30ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Server/Network/Socket.php

https://github.com/xxOrpheus/EtherRS
PHP | 172 lines | 95 code | 15 blank | 62 comment | 26 complexity | 45c68f963fcc54f6be4f32af7866ed04 MD5 | raw file
  1<?php
  2namespace Server\Network;
  3
  4class Socket extends \Server\Server {
  5	protected $currentStream, $activeStreams = array();
  6	protected $currentSocket, $activeSockets = array();
  7
  8	public function __construct(Stream $inStream = null, Stream $outStream = null, $conn = null, $name = null) {
  9		if($inStream != null && $outStream != null) {
 10			$this->addStream($inStream, $outStream, $name, true);
 11		}
 12
 13		if(is_resource($conn)) {
 14			$this->addSocket($conn, $name, true);
 15		}
 16		$this->log("Socket server initialized");
 17	}
 18
 19
 20	/**
 21	 *
 22	 * Read data from a socket
 23	 *
 24	 * @param string $name  The name of the socket to read from
 25	 * @param int    $bytes How many bytes to read
 26	 *
 27	 */
 28	public function read($bytes, $name = null) {
 29		if($name === null) {
 30			$this->currentStream[0]->clear();
 31			$data = @socket_read($this->currentSocket, $bytes, PHP_BINARY_READ);
 32		} else {
 33			$this->activeStreams[$name][0]->clear();
 34			$data = @socket_read($this->activeSockets[$name], $bytes, PHP_BINARY_READ);
 35		}
 36		if(!$data) {
 37			$this->log($this->lastError($name === null ? $this->currentSocket : $name));
 38		}
 39		$data = unpack('C*', $data);
 40		return $data;
 41	}
 42
 43	/**
 44	 *
 45	 * Write data to a socket
 46	 *
 47	 * @param string $name The name of the socket to write to
 48	 * @param mixed  $data The data to write
 49	 *
 50	 */
 51	public function write($data, $name = null) {
 52		if($name === null) {
 53			$this->currentStream[1]->clear();
 54			socket_write($this->currentSocket, $data);
 55		} else {
 56			if(!is_resource($this->activeSockets[$name])) {
 57				throw new Exception(__METHOD__ . ': Not a valid resource');
 58			}
 59			socket_write($this->activeSockets[$name], $data);
 60		}
 61	}
 62
 63	/**
 64	 *
 65	 * Write data from a stream to a socket
 66	 *
 67	 * @param string $name The name of the stream to use
 68	 *
 69	 */
 70	public function writeStream($name = null) {
 71		if($name === null) {
 72			$stream = $this->currentStream[1]->getStream();
 73			$this->write($stream);
 74		} else {
 75			if(!isset($this->activeStreams[$name])) {
 76				throw new Exception(__METHOD__ . ': Not a valid stream');
 77			}
 78			$stream = $this->activeStreams[$name][1]->getStream();
 79			$this->write($stream);
 80		}
 81	}
 82
 83	/**
 84	 *
 85	 * Add a socket to the current object
 86	 *
 87	 * @param Socket $socket    The socket to add
 88	 * @param string $name      The name to use
 89	 * @param bool   $setActive Should we set this socket to the current socket?
 90	 *
 91	 */
 92	public function addSocket($socket, $name = null, $setActive = false) {
 93		if(!is_resource($socket)) {
 94			throw new Exception(__METHOD__ . ': $socket is not a valid resource');
 95		}
 96
 97		if($name !== null) {
 98			$this->activeSockets[$name] = $socket;
 99		} else {
100			$this->activeSockets[] = $socket;
101		}
102
103		if($setActive === true) {
104			$this->currentSocket = $socket;
105		}
106	}
107
108	/**
109	 *
110	 * Add a stream to the current object
111	 *
112	 * @param Stream $stream    The stream to add
113	 * @param string $name      The name to use
114	 * @param bool   $setActive Should we use this stream as the current one?
115	 *
116	 */
117	public function addStream(Stream $inStream, Stream $outStream, $name = null, $setActive = false) {
118		if($name !== null) {
119			$this->activeStreams[$name] = array($inStream, $outStream);
120		} else {
121			$this->activeStreams[] = array($inStream, $outStream);
122		}
123
124		if($setActive === true) {
125			$this->currentStream = array($inStream, $outStream);
126		}
127	}
128
129	/**
130	 *
131	 * Select a stream to use
132	 *
133	 * @param string $name The name of the stream
134	 *
135	 */
136	public function selectStream($name) {
137		if(!isset($this->activeStreams[$name]))
138			return false;
139		$this->currentStream = $this->activeStreams[$name];
140	}
141
142	/**
143	 *
144	 * Select a socket to use
145	 *
146	 * @param string $name The name of the socket
147	 *
148	 */
149	public function selectSocket($name) {
150		if(!isset($this->activeSockets[$name]))
151			return false;
152		$this->currentSocket = $this->activeSockets[$name];
153	}
154
155	/**
156	 *
157	 * Get the last error of the socket
158	 *
159	 * @param string $name The name of the socket
160	 *
161	 */
162	public function lastError($name = null) {
163		if($name === null) {
164			return socket_last_error($this->currentSocket);
165		} else {
166			if(!isset($this->activeSockets[$name]))
167				return false;
168			return socket_last_error($this->activeSockets[$name]);
169		}
170	}
171}
172?>