PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Model/Network/socket.php

https://github.com/philbrookes/Game
PHP | 99 lines | 62 code | 6 blank | 31 comment | 12 complexity | c6f6766aed61b5a765770521114ef6d7 MD5 | raw file
  1. <?php
  2. namespace Core\Model\Network;
  3. /**
  4. * This class will act as either a listener socket, a client socket or a server socket.
  5. * EXAMPLE 1: Make a listener:
  6. * $listenSocket = new socket($MyExternalIP, $MyOpenPort);
  7. * $listenSocket->open();
  8. *
  9. * EXAMPLE 2: Client / server Socket:
  10. * $serverSock = new Socket();
  11. * $serverSock->Accept($listenSock->getSock());
  12. *
  13. * @author Philip Brookes
  14. * email: phil@locals.ie
  15. * twitter: @philthi
  16. *
  17. * I love talking to people
  18. * if you wish to comment on any of the code below
  19. * do not hesitate to contact me.
  20. *
  21. */
  22. class socket {
  23. private $ip;
  24. private $port;
  25. private $sockHandle;
  26. public function getSock(){
  27. return $this->sockHandle;
  28. }
  29. public function setIP($newVal){
  30. $this->ip = $newVal;
  31. }
  32. public function getIP(){
  33. return $this->ip;
  34. }
  35. public function setPort($newVal){
  36. $this->port = $newVal;
  37. }
  38. public function getPort(){return $this->port;}
  39. /**
  40. * IP and Port are required if this socket will be a listener socket
  41. */
  42. public function __construct($ip=null, $port=null){
  43. $this->sockHandle = socket_create(AF_INET, SOCK_STREAM, 0);
  44. //Make it a non-blocking socket
  45. socket_set_nonblock($this->sockHandle);
  46. $this->setPort($port);
  47. $this->setIP($ip);
  48. }
  49. public function open($ip=null, $port=null){
  50. if($ip) $this->setIP($ip);
  51. if($port) $this->setPort($port);
  52. //Binds to specified socket and port and listens for connection requests
  53. if(socket_bind($this->sockHandle, $this->getIP(), $this->getPort())){
  54. socket_listen($this->sockHandle);
  55. return true;
  56. }
  57. return false;
  58. }
  59. //accepts the pending connection request from a socketHandle (Note that this is not an instance of this class, but the return of getSock())
  60. public function accept($socket=null){
  61. if(is_null($socket)) return false;
  62. $newconn = @socket_accept($socket);
  63. if($newconn == false) return false;
  64. else $this->sockHandle = $newconn;
  65. //Make it a non-blocking connection
  66. socket_set_nonblock($this->sockHandle);
  67. return true;
  68. }
  69. //reads data from the socket, will return blank if nothing is there to read
  70. public function getData(){
  71. $data = @socket_read($this->sockHandle, 1024);
  72. return $data;
  73. }
  74. //Returns true if the remote end is still connected, false if the remote end has hung up.
  75. public function isAlive(){
  76. $res = @socket_recv($this->sockHandle, $data, 1024, MSG_PEEK);
  77. $result = socket_last_error($this->sockHandle);
  78. //if bytes received is zero rather than blank, client has hung up
  79. if($result == 32 || $result == 104 || $res === 0){
  80. return false;
  81. }else{
  82. return true;
  83. }
  84. }
  85. //send data to the remote socket
  86. public function write($data){
  87. socket_write($this->sockHandle, $data);
  88. }
  89. //disconnect the socket
  90. public function close(){
  91. socket_close($this->sockHandle);
  92. }
  93. }