r365 - in incubator/classes: . Net

From: <skshAT@ATshadanakar.org>
Date: Fri, 20 May 2005 10:56:29 +0400 (MSD)

Author: sksh
Date: 2005-05-20 10:56:28 +0400 (Fri, 20 May 2005)
New Revision: 365

Added:
   incubator/classes/Net/
   incubator/classes/Net/Socket.php
Log:
+ PEARs Net/Socket.php port

Added: incubator/classes/Net/Socket.php
===================================================================
--- incubator/classes/Net/Socket.php 2005-05-19 14:54:05 UTC (rev 364)
+++ incubator/classes/Net/Socket.php 2005-05-20 06:56:28 UTC (rev 365)
@@ -0,0 +1,349 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Scheglov K. *
+ * skshAT@ATgsp.rdw.ru *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * Based on Net/Socket.php from PEAR Stig Bakken <ssbAT@ATphp.net>, *
+ * Chuck Hagenbuch <chuckAT@AThorde.org> *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ class SocketException extends BaseException {}
+
+ class Socket
+ {
+ const BLOCK_SIZE = 1024;
+
+ const NET_SOCKET_READ = 1;
+ const NET_SOCKET_WRITE = 2;
+ const NET_SOCKET_ERROR = 3;
+
+ private $blocking = true;
+ private $persistent = false;
+
+ private $socketFilePointer = null;
+ private $ipAddress = null;
+
+ private $port = 0;
+ private $lineLength = 2048;
+ private $timeout = 0;
+
+ private $errorNo = 0;
+ private $errorString = ''; // not null!
+
+ public function __construct()
+ {
+ return $this;
+ }
+
+ private function setIPAddress($ipAddress)
+ {
+ if (strspn($ipAddress, '.0123456789') == strlen($ipAddress) ||
+ strstr($ipAddress, '/') !== false) {
+ $this->ipAddress = $ipAddress;
+ } else
+ $this->ipAddress = gethostbyname($ipAddress);
+
+ return $this;
+ }
+
+ private function setPort($port)
+ {
+ $this->post = $port;
+
+ return $this->port;
+ }
+
+ private function setPersistent($persistent)
+ {
+ if ($persistent !== null) {
+ $this->persistent = $persistent;
+ }
+
+ return $this->persistent;
+ }
+
+ private function setTimeout($timeout)
+ {
+ if ($timeout !== null){
+ $this->timeout = $timeout;
+ } else {
+ $this->timeout = 0;
+ }
+
+ return $this->timeout;
+ }
+
+ public function connect($ipAddress, $port = 0, $persistent = null, $timeout = null, $options = null)
+ {
+ try {
+ $this->setIPAddress($ipAddress);
+ } catch (BaseException $e) {
+ throw new SocketException("Wrong IP address");
+ }
+
+ try {
+ $this->setPort($port % 65536);
+ } catch (BaseException $e) {
+ throw new SocketException("Wrong Port");
+ }
+
+ $this->setPersistent($persistent);
+ $this->setTimeout($timeout);
+
+ $openFunction = $this->persistent ? 'pfsockopen' : 'fsockopen';
+
+ try {
+ if ($options && function_exists('stream_context_create')) {
+ $context = stream_context_create($options);
+ $socketFilePointer = $openfunc($this->addr, $this->port, $this->errorNo, $this->errorString, $this->timeout, $context);
+ } else
+ $socketFilePointer = $openfunc($this->addr, $this->port, $this->errorNo, $this->errorString, $this->timeout);
+ $this->socketFilePointer = $socketFilePointer;
+ } catch (BaseException $e) {
+ throw new SocketException("Open socket failure");
+ }
+
+ socket_set_blocking($this->socketFilePointer, $this->blocking);
+
+ return true;
+ }
+
+ public function disconnect()
+ {
+ try {
+ fclose($this->socketFilePointer);
+ $this->socketFilePointer = null;
+ } catch (BaseException $e) {
+ throw new SocketException("Close socket failure");
+ }
+ return true;
+ }
+
+ public function isBlocking()
+ {
+ return $this->blocking;
+ }
+
+ public function setBlocking($blocking)
+ {
+ $this->blocking = $blocking;
+
+ return $this->blocking;
+ }
+
+ public function setSocketTimeout($seconds, $microseconds) // aka setTimeout
+ {
+ try {
+ socket_set_timeout($this->socketFilePointer, $seconds, $microseconds);
+ } catch (BaseException $e) {
+ throw new SocketException("Set socket timeout failure");
+ }
+
+ return $this;
+ }
+
+ public function getStatus()
+ {
+ return socket_get_status($this->socketFilePointer);
+ }
+
+ public function gets($size)
+ {
+ try {
+ $line = fgets($this->socketFilePointer, $size);
+ } catch (BaseException $e) {
+ throw new SocketException("Get a specified line of data failure");
+ }
+
+ return $line;
+ }
+
+ public function read($size)
+ {
+ try {
+ $data = fread($this->socketFilePointer, $size);
+ } catch (BaseException $e) {
+ throw new SocketException("Read a specified amount of data failure");
+ }
+
+ return $data;
+ }
+
+ public function write($data, $blockiSize = null)
+ {
+ //TODO: view this function one more time
+ try {
+ if (is_null($blockSize) && !OS_WINDOWS) {
+ return fwrite($this->socketFilePointer, $data);
+ } else {
+ if (is_null($blockSize))
+ $blockSize = self::BLOCK_SIZE;
+
+ $pos = 0;
+ $size = strlen($data);
+ while ($pos < $size) {
+ $written = fwrite($this->socketFilePointer, substr($data, $pos, $blockSize));
+ if ($written === false)
+ return false;
+
+ $pos += $written;
+ }
+
+ return $pos;
+ }
+ } catch (BaseException $e) {
+ throw new SocketException("Write a specified amount of data failure");
+ }
+ }
+
+ public function writeLine($data)
+ {
+ try {
+ fwrite($this->socketFilePointer, $data . "\r\n");
+ } catch (BaseException $e) {
+ throw new SocketException("Write line failure");
+ }
+
+ return $this;
+ }
+
+ public function isEOF()
+ {
+ return feof($this->socketFilePointer);
+ }
+
+ public function readByte()
+ {
+ try {
+ $string = fread($this->socketFilePointer, 1);
+ } catch (BaseException $e) {
+ throw new SocketException("Read Byte failure");
+ }
+
+ return ord($string);
+ }
+
+ public function readWord()
+ {
+ try {
+ $buffer = fread($this->socketFilePointer, 2);
+ } catch (BaseException $e) {
+ throw new SocketException("Read Word failure");
+ }
+
+ return (ord($buffer[0]) + (ord($buffer[1]) << 8));
+ }
+
+ public function readInt()
+ {
+ try {
+ $buffer = fread($this->socketFilePointer, 4);
+ } catch (BaseException $e) {
+ throw new SocketException("Read Int failure");
+ }
+
+ return (ord($buffer[0]) + (ord($buffer[1]) << 8) +
+ (ord($buffer[2]) << 16) + (ord($buffer[3]) << 24));
+ }
+
+ public function readString()
+ {
+ try {
+ $string = '';
+ while (($char = fread($this->socketFilePointer, 1)) != "\x00")
+ $string .= $char;
+ } catch (BaseException $e) {
+ throw new SocketException("Read String failure");
+ }
+
+ return $string;
+ }
+
+ public function readIPAddress()
+ {
+ try {
+ $buffer = fread($this->socketFilePointer, 4);
+ } catch (BaseException $e) {
+ throw new SocketException("Read IP address failure");
+ }
+
+ return sprintf("%s.%s.%s.%s", ord($buffer[0]), ord($buffer[1]),
+ ord($buffer[2]), ord($buffer[3]));
+ }
+
+ public function readLine()
+ {
+ try {
+ $line = '';
+ $timeout = time() + $this->timeout;
+
+ while (!feof($this->socketFilePointer) && (!$this->timeout || time() < $timeout)) {
+ $line .= fgets($this->socketFilePointer, $this->lineLength);
+
+ if (substr($line, -1) == "\n")
+ return rtrim($line, "\r\n");
+ }
+ } catch (BaseException $e) {
+ throw new SocketException("Read line failure");
+ }
+
+ return $line;
+ }
+
+ public function readAll()
+ {
+ try {
+ $data = '';
+ while (!feof($this->socketFilePointer))
+ $data .= fread($this->socketFilePointer, $this->lineLength);
+ } catch (BaseException $e) {
+ throw new SocketException("Read all failure");
+ }
+
+ return $data;
+ }
+
+ public function select($state, $secondsForTimeout, $microsecondsForTimeout = 0)
+ {
+ $read = null;
+ $write = null;
+ $except = null;
+
+ try {
+ if ($state & self::NET_SOCKET_READ)
+ $read[] = $this->socketFilePointer;
+
+ if ($state & self::NET_SOCKET_WRITE)
+ $write[] = $this->socketFilePointer;
+
+ if ($state & self::NET_SOCKET_ERROR)
+ $except[] = $this->socketFilePointer;
+
+ if (false === ($sr = stream_select($read, $write, $except, $secondsForTimeout, $microsecondsForTimeout)))
+ return false;
+
+ $result = 0;
+ if (count($read))
+ $result |= self::NET_SOCKET_READ;
+
+ if (count($write))
+ $result |= self::NET_SOCKET_WRITE;
+
+ if (count($except))
+ $result |= self::NET_SOCKET_ERROR;
+
+ } catch (BaseException $e) {
+ throw new SocketException("Select failure");
+ }
+
+ return $result;
+ }
+ }
+?>

Property changes on: incubator/classes/Net/Socket.php
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native
Received on Fri May 20 2005 - 10:56:29 MSD

This archive was generated by hypermail 2.2.0 : Sat Oct 27 2007 - 19:25:09 MSD