@@ -25,7 +25,13 @@ class Socket
2525 * socket
2626 * @var string
2727 */
28- protected string $ content ;
28+ protected string $ content = '' ;
29+
30+ protected $ clientSocket ;
31+
32+ protected $ serverSocket ;
33+
34+ protected $ createdSocket ;
2935
3036 /**
3137 * Socket constructor method
@@ -50,52 +56,113 @@ public function create()
5056 }
5157
5258 /**
53- * Initiates a connection on a socket
59+ * Binds a name to a socket
60+ * @return bool
61+ */
62+ public function bind ()
63+ {
64+ return socket_bind ($ this ->socket , $ this ->host , $ this ->port );
65+ }
66+
67+ /**
68+ * Listens for a connection on a socket
5469 * @return bool
5570 */
71+ public function listen ()
72+ {
73+ return socket_listen ($ this ->socket , 1 );
74+ }
75+
76+ /**
77+ * Accept a connection on a socket
78+ * @return resource
79+ */
80+ public function accept ()
81+ {
82+ return socket_accept ($ this ->socket );
83+ }
84+
85+ /**
86+ * Bind, listen and return accepted socket
87+ * connection
88+ * @return resource
89+ */
90+ public function acceptSocketConnection ()
91+ {
92+ if (!$ this ->bind ()) {
93+ return false ;
94+ }
95+
96+ if (!$ this ->listen ()) {
97+ return false ;
98+ }
99+
100+ $ acceptedSocket = $ this ->accept ();
101+
102+ if (!$ acceptedSocket ) {
103+ return false ;
104+ }
105+
106+ return $ acceptedSocket ;
107+ }
108+
109+ /**
110+ * Return resource of type Socket
111+ * which was created right now. Can get
112+ * access by connect method
113+ * @return resource
114+ */
115+ public function getConnectedSocket ()
116+ {
117+ return $ this ->socket ;
118+ }
119+
120+ /**
121+ * Initiates a connection on a socket
122+ * @return \WebSocket\Socket
123+ */
56124 public function connect ()
57125 {
58- return socket_connect (
126+ socket_connect (
59127 $ this ->socket , $ this ->host , $ this ->port
60128 );
129+
130+ return $ this ->socket ;
61131 }
62132
63133 /**
64- * Write to a socket specified message
134+ * Write to a socket specified messages
65135 * @param string $message
66136 * @return int
67137 */
68- public function write (string $ message )
138+ public function write ($ socket , string $ message )
69139 {
70140 return socket_write (
71- $ this -> socket , $ message , strlen ($ message )
141+ $ socket , $ message , strlen ($ message )
72142 );
73143 }
74144
75145 /**
76146 * Reads a maximum of length bytes from a socket
147+ * @param resource $socket
77148 * @return string
78149 */
79- public function read ()
150+ public function read ($ socket )
80151 {
81- $ write = null ;
82- $ exception = null ;
83-
84- while (socket_select ([$ this ->socket ], $ write , $ exception , 0 )) {
85- socket_recv (
86- $ this ->socket , $ this ->content , strlen ($ this ->content ), 0
87- );
152+ while (socket_recv ($ socket , $ buffer , 2048 , 0 )) {
153+ $ this ->content .= $ buffer ;
88154 }
89155
90156 return $ this ->content ;
91157 }
92158
93159 /**
94- * Socket detructor method which
95- * close current listening socket
160+ * Close specified socket connection
161+ * @param resource $socket
162+ * @return void
96163 */
97- public function __destruct ( )
164+ public function close ( $ socket )
98165 {
99- socket_close ($ this -> socket );
166+ return socket_close ($ socket );
100167 }
101168}
0 commit comments