TCP.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: TCP.php,v 1.1 2003/04/08 06:21:53 shane Exp $
  20. //
  21. require_once 'SOAP/Server.php';
  22. /**
  23. * SOAP_Server_TCP
  24. * SOAP Server Class
  25. *
  26. * implements TCP SOAP Server
  27. * http://www.pocketsoap.com/specs/smtpbinding/
  28. *
  29. * class overrides the default HTTP server, providing the ability
  30. * to accept socket connections and execute soap calls.
  31. *
  32. * TODO:
  33. * use Net_Socket
  34. * implement some security scheme
  35. * implement support for attachments
  36. *
  37. * @access public
  38. * @version $Id: TCP.php,v 1.1 2003/04/08 06:21:53 shane Exp $
  39. * @package SOAP::Server
  40. * @author Shane Caraveo <shane@php.net>
  41. */
  42. class SOAP_Server_TCP extends SOAP_Server {
  43. var $headers = array();
  44. var $localaddr;
  45. var $port;
  46. var $listen;
  47. var $reuse;
  48. function SOAP_Server_TCP($localaddr="127.0.0.1", $port=10000, $listen=5, $reuse=TRUE)
  49. {
  50. parent::SOAP_Server();
  51. $this->localaddr = $localaddr;
  52. $this->port = $port;
  53. $this->listen = $listen;
  54. $this->reuse = $reuse;
  55. }
  56. function run()
  57. {
  58. if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
  59. return $this->_raiseSoapFault("socket_create() failed: reason: " . socket_strerror ($sock));
  60. }
  61. if ($this->reuse &&
  62. !@socket_setopt($sock,SOL_SOCKET,SO_REUSEADDR,1)) {
  63. return $this->_raiseSoapFault("socket_setopt() failed: reason: ".socket_strerror(socket_last_error($sock)));
  64. }
  65. if (($ret = socket_bind ($sock, $this->localaddr, $this->port)) < 0) {
  66. return $this->_raiseSoapFault("socket_bind() failed: reason: " . socket_strerror ($ret));
  67. }
  68. # print "LISTENING on {$this->localaddr}:{$this->port}\n";
  69. if (($ret = socket_listen ($sock, $this->listen)) < 0) {
  70. return $this->_raiseSoapFault("socket_listen() failed: reason: " . socket_strerror ($ret));
  71. }
  72. do {
  73. $data = NULL;
  74. if (($msgsock = socket_accept($sock)) < 0) {
  75. $this->_raiseSoapFault("socket_accept() failed: reason: " . socket_strerror ($msgsock));
  76. break;
  77. }
  78. # print "Accepted connection\n";
  79. while ($buf = socket_read ($msgsock, 8192)) {
  80. if (!$buf = trim($buf)) {
  81. continue;
  82. }
  83. $data .= $buf;
  84. }
  85. if ($data) {
  86. $response = $this->service($data);
  87. # write to the socket
  88. if (!socket_write($msgsock, $response, strlen($response))) {
  89. return $this->_raiseSoapFault('Error sending response data reason '.socket_strerror());
  90. }
  91. }
  92. socket_close ($msgsock);
  93. } while (true);
  94. socket_close ($sock);
  95. }
  96. function service(&$data)
  97. {
  98. # XXX we need to handle attachments somehow
  99. return $this->parseRequest($data,$attachments);
  100. }
  101. }
  102. ?>