public function readFrom(Socket $socket) { $data = $socket->read(); if (!$socket instanceof self) { $data = $this->decode($data); } return $data; }
/** * Returns false, if there is no more document on the socket. * @return string|boolean */ private function getDocument() { $docSize = $this->socket->read(self::FITNESSE_INTEGER); if ($docSize == 0) { return false; } $document = $this->socket->read($docSize); return $document; }
/** * 發送EMAIL的組件 * @param Array $opt 發送的數據 * @author wave */ public function mail_send($opt = array()) { set_time_limit(0); Socket::$address = $opt['address']; Socket::$port = $opt['port']; $_data = array(0 => "EHLO " . $opt['cc'] . "\r\n", 1 => "AUTH LOGIN\r\n", 2 => base64_encode($opt['form']) . "\r\n", 3 => base64_encode($opt['pass']) . "\r\n", 4 => "MAIL FROM: <" . $opt['form'] . ">\r\n", 5 => "RCPT TO: <" . $opt['to'] . ">\r\n", 6 => "Content-Type: text/html; charset=\"utf-8\"\r\n", 7 => "DATA\r\n", 8 => "Form: " . $opt['cc'] . "<" . $opt['form'] . ">\r\nTo: " . $opt['to'] . "\r\nSubject: " . $opt['title'] . "\r\n\r\n" . $opt['body'] . "\r\n", 9 => "\r\n.\r\n", 10 => "QUIT\r\n"); $jilu = array(); //記錄發送數組 foreach ($_data as $k => $v) { Socket::$data = $v; Socket::send(); $jilu['ok'][$k] = Socket::read(); if (!in_array($k, array(7, 8))) { } if ($k - count($_data) == 0) { if (substr(Socket::read(), 0, 3) != "250") { $jilu['err'] = Socket::read(); } } } Socket::colse(); return isset($jilu['err']) ? $jilu['err'] : 'ok'; }
/** * Because the sockets are in non-blocking mode to avoid stalling the whole * server when one client is slow, we read data from the socket and buffer it. * When there is data in the buffer we try to read and handle that. This method * should be called constantly so the data can be read and handled. * This method will not return any data. The appropiate events/methods will be called * in the observerving objects when something happened. */ public function cycle() { /* We can't do anything with a closed socket...*/ if (!$this->m_bConnected) { return false; } /* When we send a closing handshake, we will wait 5 seconds for the client to do the * same. When we don't recieve a closing frame in time, we close the underlying TCP * connection. This is allowed by the protocol ;-) */ if ($this->m_nCloseStartedAt != 0 && time() - $this->m_nCloseStartedAt > 5) { $this->close($this->m_nCloseReason, $this->m_sCloseReason); } /* Keep reading the handshake untill it is read and we've send a reply. */ if (!($this->m_bReadHandshake && $this->m_bSendHandshake)) { /* this method will handle the opening handshake */ $this->accept(); return; } /* The TCP connection was closed, no point in continueing */ if (@feof($this->m_rSocket)) { /* Close the connection with a 1006 statuts code (Not closed cleanly). */ $this->close(1006); return false; } /* Use the parent's read function to read data from the socket (if there is any) */ $sData = parent::read(2048); if ($sData === false) { return; } /* Unpack the byte string as bytes */ $aData = unpack('C*', $sData); /* Append the newly read data to the end of the read buffer */ $this->m_aReadBuffer = array_merge($this->m_aReadBuffer, $aData); /* If this property is -1, we aren't busy reading any message so we can * assume that this frame is the first frame of a new message */ if ($this->m_nCurrentMessageType == -1) { $bFirstFrame = true; } else { $bFirstFrame = false; } /* We need at least two bytes to start reading and parsing a frame */ if ($this->m_pCurrentFrame == null && $this->getReadBufferSize() >= 2) { /* If there is enough data, create a new WebSocketFrame */ $this->m_pCurrentFrame = new WebSocketFrame(); } /* if there is a WebSocketFrame */ if ($this->m_pCurrentFrame != null) { /* Start reading, if this method returns something other then true, the frame is * to large and cannot be read */ if (($Result = $this->m_pCurrentFrame->read($this)) !== true) { $this->disconnect(1009, 'Frame too large.'); } /* When the frame is fully read, start processing it. * Frames can be read in multiple steps, just keep calling the read method untill * the result of the isComplete method is true. */ if ($this->m_pCurrentFrame->isComplete()) { /* All frames send by a client MUST be masked or the connection MUST be closed * protocol section 4.3 */ if (!$this->m_pCurrentFrame->isMasked()) { $this->disconnect(1002, 'Protocol error: Message should be masked.'); return false; } /* Message can be broken up into smaller parts which we call frames. These frame can be * send seperatly. Only control frames may be injected between frames beloning to the * same message. */ if (!$bFirstFrame && $this->m_pCurrentFrame->getType() != WebSocketFrame::TYPE_CONT && !$this->m_pCurrentFrame->isControlFrame()) { $this->disconnect(1002, 'Protocol error: Mixing messages.'); return false; } /* If we recieve a control frame in between frames beloning to a seperate message, we * have to raise the associated events before continueing reading the rest of the message */ if (!$bFirstFrame && $this->m_pCurrentFrame->isControlFrame()) { $this->onCompleteMessage($this->m_pCurrentFrame->getType(), array()); $this->m_pCurrentFrame = null; return true; } /* Add the payload data in the newly read frame to the frame-read-buffer */ $this->m_aFragmentBuffer = array_merge($this->m_aFragmentBuffer, $this->m_pCurrentFrame->getData()); /* If this frame is the first of a new message, store the message type. * The opcode in the following frames for this message will have a different * opcode. */ if ($bFirstFrame) { $this->m_nCurrentMessageType = $this->m_pCurrentFrame->getType(); } /* If the newly read frame is the final frame of a full message we can process it further */ if ($this->m_pCurrentFrame->isFinal()) { /* This call will inform the observers */ $this->onCompleteMessage($this->m_nCurrentMessageType, $this->m_aFragmentBuffer); /* Clear the frame-read buffer for the next message */ $this->m_aFragmentBuffer = array(); /* We want a new message! */ $this->m_nCurrentMessageType = -1; } /* This frame is processed, prepare for the next one. */ $this->m_pCurrentFrame = null; } } }
/** * Overrideable Read Functionality. * * @param Socket $client * * @return string */ protected function read(Socket $client) { return $client->read($this->maxRead, $this->readType); }
/** * Parse the response and return the array of offsets * * @param Socket $socket Socket handle * * @return array */ public static function deserializeOffsetArray(Socket $socket) { $unpack = unpack('N', $socket->read(4)); $nOffsets = array_shift($unpack); if ($nOffsets < 0) { throw new Exception\OutOfRange($nOffsets . ' is not a valid number of offsets'); } $offsets = array(); for ($i = 0; $i < $nOffsets; ++$i) { $offsets[] = self::unpackLong64bigendian($socket->read(8)); } return $offsets; }
/** * Read a packet from a socket. * * @param Socket $socket * @return Packet * @throws Exception */ static function read($socket) { $byte = $socket->read(1); $header = unpack('C', $byte); $packetType = $header[1] >> 4; if (!array_key_exists($packetType, self::$PACKET_TYPES)) { throw new \Exception('Invalid packet type received'); } //Calculate remaining length $multiplier = 1; $length = 0; while (true) { $digit = current(unpack('C', $socket->read(1))); $length += ($digit & 127) * $multiplier; $multiplier *= 128; if (($digit & 128) == 0) { break; } } //Read body $data = $socket->read($length); $packet = new self::$PACKET_TYPES[$packetType](); $packet->parse($header, $data); return $packet; }
/** * Reads data returned from the ntp server * * @return void */ protected function read() { return $this->socket->read(); }