Permalink
Browse files

Request #14344 Use Net_Server in SOAP_Server_TCP

git-svn-id: http://svn.php.net/repository/pear/packages/SOAP/trunk@322282 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 27d6144 commit 094448391303bce28c6d66851914eae44a7f5dff @CloCkWeRX CloCkWeRX committed Jan 14, 2012
Showing with 113 additions and 46 deletions.
  1. +36 −46 Server/TCP.php
  2. +77 −0 Server/TCP/Handler.php
View
@@ -21,6 +21,9 @@
require_once 'SOAP/Server.php';
+require_once 'SOAP/Server/TCP/Handler.php';
+
+
/**
* SOAP Server Class that implements a TCP SOAP Server.
* http://www.pocketsoap.com/specs/smtpbinding/
@@ -42,65 +45,52 @@ class SOAP_Server_TCP extends SOAP_Server {
var $headers = array();
var $localaddr;
var $port;
- var $listen;
- var $reuse;
+ var $type;
function SOAP_Server_TCP($localaddr = '127.0.0.1', $port = 10000,
- $listen = 5, $reuse = true)
+ $type = 'sequential')
{
parent::SOAP_Server();
$this->localaddr = $localaddr;
$this->port = $port;
- $this->listen = $listen;
- $this->reuse = $reuse;
+ $this->type = $type;
}
- function run()
- {
- if (($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0) {
- return $this->_raiseSoapFault('socket_create() failed. Reason: ' . socket_strerror($sock));
- }
- if ($this->reuse &&
- !@socket_setopt($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
- return $this->_raiseSoapFault('socket_setopt() failed. Reason: ' . socket_strerror(socket_last_error($sock)));
- }
- if (($ret = socket_bind($sock, $this->localaddr, $this->port)) < 0) {
- return $this->_raiseSoapFault('socket_bind() failed. Reason: ' . socket_strerror($ret));
- }
- if (($ret = socket_listen($sock, $this->listen)) < 0) {
- return $this->_raiseSoapFault('socket_listen() failed. Reason: ' . socket_strerror($ret));
- }
-
- while (true) {
- $data = null;
- if (($msgsock = socket_accept($sock)) < 0) {
- $this->_raiseSoapFault('socket_accept() failed. Reason: ' . socket_strerror($msgsock));
- break;
- }
- while ($buf = socket_read($msgsock, 8192)) {
- if (!$buf = trim($buf)) {
- continue;
- }
- $data .= $buf;
- }
-
- if ($data) {
- $response = $this->service($data);
- /* Write to the socket. */
- if (!socket_write($msgsock, $response, strlen($response))) {
- return $this->_raiseSoapFault('Error sending response data reason ' . socket_strerror());
- }
- }
-
- socket_close ($msgsock);
+ function run($idleTimeout = null)
+ {
+ $server = &Net_Server::create($this->type, $this->localaddr,
+ $this->port);
+ if (PEAR::isError($server)) {
+ echo $server->getMessage()."\n";
}
-
- socket_close ($sock);
+
+ $handler = &new SOAP_Server_TCP_Handler;
+ $handler->setSOAPServer($this);
+
+ // hand over the object that handles server events
+ $server->setCallbackObject($handler);
+ $server->readEndCharacter = '</SOAP-ENV:Envelope>';
+ $server->setIdleTimeout($idleTimeout);
+
+ // start the server
+ $server->start();
}
function service(&$data)
{
/* TODO: we need to handle attachments somehow. */
- return $this->parseRequest($data, $attachments);
+ $response = $this->parseRequest($data);
+ if ($this->fault) {
+ $response = $this->fault->message($this->response_encoding);
+ }
+ return $response;
+ }
+
+ function onStart()
+ {
+ }
+
+ function onIdle()
+ {
}
}
View
@@ -0,0 +1,77 @@
+<?php
+/**
+ * This file contains the code for the TCP SOAP server.
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE: This source file is subject to version 2.02 of the PHP license,
+ * that is bundled with this package in the file LICENSE, and is available at
+ * through the world-wide-web at http://www.php.net/license/2_02.txt. If you
+ * did not receive a copy of the PHP license and are unable to obtain it
+ * through the world-wide-web, please send a note to license@php.net so we can
+ * mail you a copy immediately.
+ *
+ * @category Web Services
+ * @package SOAP
+ * @author Tomasz Rup <tomasz.rup@gmail.com>
+ * @copyright 2003-2005 The PHP Group
+ * @license http://www.php.net/license/2_02.txt PHP License 2.02
+ * @link http://pear.php.net/package/SOAP
+ */
+
+/**
+ * server base class
+ */
+require_once 'Net/Server.php';
+
+/**
+ * base class for the handler
+ */
+require_once 'Net/Server/Handler.php';
+
+
+/**
+ * SOAP Server Class that implements a TCP SOAP Server.
+ * http://www.pocketsoap.com/specs/smtpbinding/
+ *
+ * This class overrides the default HTTP server, providing the ability to
+ * accept socket connections and execute SOAP calls.
+ *
+ * @access public
+ * @package SOAP
+ * @author Tomasz Rup <tomasz.rup@gmail.com>
+ */
+class SOAP_Server_TCP_Handler extends Net_Server_Handler {
+
+ var $_SOAP_Server;
+
+ function setSOAPServer(&$server)
+ {
+ $this->_SOAP_Server =& $server;
+ }
+
+ /**
+ * If the user sends data, send it back to him
+ *
+ * @access public
+ * @param integer $clientId
+ * @param string $data
+ */
+ function onReceiveData($clientId = 0, $data = '')
+ {
+ if (trim($data) <> '') {
+ $response = $this->_SOAP_Server->service($data);
+ $this->_server->sendData($clientId, $response);
+ }
+ }
+
+ function onStart()
+ {
+ $this->_SOAP_Server->onStart();
+ }
+
+ function onIdle()
+ {
+ $this->_SOAP_Server->onIdle();
+ }
+}

0 comments on commit 0944483

Please sign in to comment.