コード例 #1
0
ファイル: Listener.php プロジェクト: lan143/battleship
 private function onRead($buffer, $id)
 {
     while ($data = @event_buffer_read($buffer, 2048)) {
         //Logger::getInstance()->outDebug("Received data from client #".$id.": ".$data);
         $this->onData($data, $id);
     }
 }
コード例 #2
0
ファイル: ServerRequest.php プロジェクト: hinathan/edges-php
 public function read($eventbuffer)
 {
     $this->log("read()");
     if ($this->fired('end')) {
         $this->log("read but already fired 'end' event");
         return;
     }
     $data = event_buffer_read($eventbuffer, self::READ_CHUNK_SIZE);
     if ($this->_have_header) {
         $this->_content_bytes += strlen($data);
         $this->rawData($data);
     } else {
         $chunks = explode("\r\n", $data);
         while (!$this->_have_header && $chunks) {
             $chunk = array_shift($chunks);
             $this->readHeaderLine($chunk);
         }
         if ($this->_have_header) {
             $this->server->handleRequest($this->client_id);
         }
         if ($chunks) {
             $reconstitutedData = implode("\r\n", $data);
             $this->_content_bytes += strlen($reconstitutedData);
             $this->rawData($reconstitutedData);
         }
     }
 }
コード例 #3
0
 function ev_read($buffer, $id)
 {
     static $ct = 0;
     $ct_last = $ct;
     $ct_data = '';
     while ($read = event_buffer_read($buffer, 1024)) {
         $ct += strlen($read);
         $ct_data .= $read;
     }
     $datamsg = '收到数据来自ID:' . $id . ' 内容:' . $ct_data;
     $datamsg = iconv('utf-8', 'gbk', $datamsg);
     echo $datamsg . "\n";
     if ($ct_data == '00') {
         //收到来自打印机的心跳数据 给打印机下发数据
         $clientimei = self::$imei[$id];
         $data811 = '%30测试数据';
         $data811 .= '%%%00你的IMEI如下:';
         $data811 .= '%%' . $clientimei;
         $data811 .= '1234:###' . iconv('utf-8', 'gbk', $data811);
         $fblen = $this->jsdata($data811);
         $write = '1123456789' . $fblen . '81101   1' . $data811 . '#';
         event_buffer_write($buffer, $write);
     } else {
         $type = substr($ct_data, 20, 3);
         //各类方法
         if ($type == '810') {
             //拿810 取IMEI
             self::$imei[$id] = substr($ct_data, 23, 15);
         }
     }
 }
コード例 #4
0
ファイル: libevent.php プロジェクト: heartshare/VagrantForPhp
function foo2($buf, $arg)
{
    static $i;
    $i++;
    if ($i == 10) {
        event_base_loopexit($arg);
    }
    var_dump($buf);
    var_dump(event_buffer_read($buf, 10));
}
コード例 #5
0
ファイル: Server.php プロジェクト: danielmunro/beehive
 protected function read($buffer, Client $client)
 {
     $message = trim(event_buffer_read($buffer, self::MAX_READ_LENGTH));
     if (!$client->getHandshake()) {
         if ($client->handshake($message) && $this->connect_callback) {
             call_user_func_array($this->connect_callback, [$client]);
         }
     }
     call_user_func_array($this->read_callback, [$client, $client->decodeIncoming($message)]);
 }
コード例 #6
0
ファイル: Buffer.php プロジェクト: expressif/stream
 /**
  * Internal function for reading an event
  */
 public function _read()
 {
     $buffer = '';
     while ($read = event_buffer_read($this->event, 1024)) {
         $buffer .= $read;
     }
     if (!empty($buffer)) {
         $this->emit('data', array($buffer));
     }
 }
コード例 #7
0
 /**
  * Wczytanie zawartosci z podanego bufora badz bufora zdarzenia
  * @param resource $pBuffer                bufor z ktorego wczytujemy dane (opcjonalny)
  * @return long                            liczba wczytanych bajtow
  */
 public function read($pBuffer = null)
 {
     if (null === $pBuffer) {
         $pBuffer = $this->eventBuffer;
     }
     $length = 0;
     while ($buf = event_buffer_read($pBuffer, 1024)) {
         $this->readBuffer .= $buf;
         $length += strlen($buf);
     }
     return $length;
 }
コード例 #8
0
 public function ev_read($buffer, $id)
 {
     static $ct = 0;
     $ct_last = $ct;
     $ct_data = '';
     // event_buffer_read — Read data from a buffered event
     while ($read = event_buffer_read($buffer, 1024)) {
         $ct += strlen($read);
         $ct_data .= $read;
     }
     $ct_size = ($ct - $ct_last) * 8;
     echo "[{$id}]" . __METHOD__ . " > " . $ct_data . "\n";
     event_buffer_write($buffer, "Received {$ct_size} byte data.\r\n");
 }
コード例 #9
0
 function ev_read($buffer, $id)
 {
     static $ct = 0;
     $ct_last = $ct;
     $ct_data = '';
     while ($read = event_buffer_read($buffer, 1024)) {
         $ct += strlen($read);
         $ct_data .= $read;
     }
     $ct_size = ($ct - $ct_last) * 8;
     if (APP_DEBUG === true) {
         Log::write("[{$id}] " . __METHOD__ . " > " . $ct_data, Log::INFO);
     }
     //		event_buffer_write($buffer, "Received $ct_size byte data./r/n");
 }
コード例 #10
0
ファイル: serv.php プロジェクト: baohanddd/libevent
function ev_read($buffer, $id)
{
    $db = $GLOBALS['db'];
    $headers = $part = "";
    while ($part = event_buffer_read($buffer, 256)) {
        $headers .= $part;
    }
    $str = sprintf("Hello %s\n", $id);
    // echo sprintf("connections: %d\n", count($GLOBALS['connections']));
    $db->echos->resps->insert(array('echo' => $str));
    $db->echos->resps->count();
    // echo sprintf("total: %d\n", $db->echos->resps->count());
    fwrite($GLOBALS['connections'][$id], $str);
    fclose($GLOBALS['connections'][$id]);
    unset($GLOBALS['connections'][$id], $GLOBALS['buffers'][$id]);
}
コード例 #11
0
ファイル: AsyncBase.php プロジェクト: ansendu/ansio
 public function onEvBufReadEvent($evBuf, $arg)
 {
     $connId = $arg[0];
     Debug::netEvent(get_class($this) . '::' . __METHOD__ . '(' . $connId . ') invoked. ');
     $this->updateLastContact($connId);
     $data = '';
     while (true) {
         $read = event_buffer_read($evBuf, $this->readPacketSize);
         if ($read === '' || $read === NULL || $read === false) {
             break;
         }
         $data .= $read;
     }
     if ($data) {
         Debug::netEvent(get_class($this) . '::' . __METHOD__ . '(' . $connId . ') stdin(' . Utils::convertSize(strlen($data)) . ') start... ');
         Debug::stdin('Server --> Get Client Data: ' . Debug::exportBytes($data));
         $this->connSocketSessionPool[$connId]->stdin($data);
         Debug::netEvent(get_class($this) . '::' . __METHOD__ . '(' . $connId . ') stdin(' . Utils::convertSize(strlen($data)) . ') end. ');
     }
 }
コード例 #12
0
 function ev_read($buffer, $id)
 {
     if (!self::$users[$id]['handshaked']) {
         //首次连接进行握手
         $read = event_buffer_read($buffer, 1024 * 5);
         //握手最多只读5k
         $tmp = str_replace("\r", '', $read);
         if (strpos($tmp, "\n\n") === false) {
             return;
         }
         $this->doHandshake($id, $read);
         return;
     }
     $read = event_buffer_read($buffer, 500);
     if (($message = $this->deframe($read, self::$users[$id], $id)) == FALSE) {
         return;
     }
     if (self::$users[$id]['hasSentClose']) {
         $this->ev_error(self::$buffers[$id], "Client disconnected. Sent close: " . $id, $id);
     }
     $message_data = trim($message);
     $message_data = json_decode($message_data, true);
     if (!$message_data) {
         return;
     }
     switch ($message_data['type']) {
         case 'login':
             self::send($id, '{"type":"welcome","id":"' . $id . '"}');
             break;
         case 'update':
             // 转播给所有用户
             self::send_to_all(json_encode(array('type' => 'update', 'id' => $id, 'angle' => $message_data["angle"] + 0, 'momentum' => $message_data["momentum"] + 0, 'x' => $message_data["x"] + 0, 'y' => $message_data["y"] + 0, 'life' => 1, 'name' => isset($message_data['name']) ? $message_data['name'] : 'xkd-' . $id, 'authorized' => false)));
             break;
         case 'message':
             // 向大家说
             $new_message = array('type' => 'message', 'id' => $id, 'message' => $message_data['message']);
             return self::send_to_all(json_encode($new_message));
         default:
             break;
     }
 }
コード例 #13
0
 /**
  * a buffer event callback when socket status is prepared for reading.
  * @param resorce $BufferEvent
  * @param resorce $ClientSocket
  */
 public function evcb_doReceive($BufferEvent, $Socket)
 {
     //        echo $this->getSocketName().":StartRead\t".microtime(true)."\n";
     $data = event_buffer_read($BufferEvent, 4096);
     if ($data !== false && $data != '') {
         if (!$this->CheckExpire()) {
             return;
         }
         if ($this->isInit()) {
             return;
         }
         $Headers = $this->extraceHttpHeader($data);
         if ($Headers === false) {
             return;
         }
         if ($this->ProcessRequest($Headers)) {
             $this->Init();
             if (!$this->Server->ClientInited($this, $Headers)) {
                 //                  echo "Session Verify Failed!\n";
                 $this->Shutdown(false, true);
             }
         }
     }
 }
コード例 #14
0
 public function read($connId, $n)
 {
     if (!isset($this->buf[$connId])) {
         return FALSE;
     }
     if (isset($this->readEvents[$connId])) {
         if (Daemon::$useSockets) {
             $read = socket_read(Daemon::$worker->pool[$connId], $n);
             if ($read === FALSE) {
                 $no = socket_last_error(Daemon::$worker->pool[$connId]);
                 if ($no !== 11) {
                     Daemon::log(get_class($this) . '::' . __METHOD__ . ': connId = ' . $connId . '. Socket error. (' . $no . '): ' . socket_strerror($no));
                     $this->onFailureEvent($connId, array());
                 }
             }
         } else {
             $read = fread(Daemon::$worker->pool[$connId], $n);
         }
     } else {
         $read = event_buffer_read($this->buf[$connId], $n);
     }
     if ($read === '' || $read === NULL || $read === FALSE) {
         if (Daemon::$settings['logreads']) {
             Daemon::log('read(' . $connId . ',' . $n . ') interrupted.');
         }
         unset(Daemon::$worker->readPoolState[$connId]);
         return FALSE;
     }
     if (Daemon::$settings['logreads']) {
         Daemon::log('read(' . $connId . ',' . $n . ',[' . gettype($read) . '-' . ($read === FALSE ? 'false' : strlen($read)) . ':' . Daemon::exportBytes($read) . ']).');
     }
     return $read;
 }
コード例 #15
0
ファイル: Server.php プロジェクト: nsautomaton/thalassa
 private function readcb($buffer, $conn)
 {
     $data = event_buffer_read($buffer, 1024 * 10);
     $this->event->call('onData', array($conn, $data));
 }
コード例 #16
0
 protected function _read($connectionId)
 {
     $data = event_buffer_read($this->buffers[$connectionId], self::SOCKET_BUFFER_SIZE);
     if (!strlen($data)) {
         return false;
     }
     @($this->_read[$connectionId] .= $data);
     //добавляем полученные данные в буфер чтения
     return strlen($this->_read[$connectionId]) < self::MAX_SOCKET_BUFFER_SIZE;
 }
コード例 #17
0
 protected function _read($connectionId)
 {
     $data = event_buffer_read($this->buffers[$connectionId], self::SOCKET_BUFFER_SIZE);
     if (!strlen($data)) {
         return;
     }
     @($this->_read[$connectionId] .= $data);
     //add the data into the read buffer
     return strlen($this->_read[$connectionId]) < self::MAX_SOCKET_BUFFER_SIZE;
 }
コード例 #18
0
ファイル: Buffer.php プロジェクト: ansendu/phpLibEventWrapper
 /**
  * Reads data from the stream
  *
  * @param int $size Number of bytes to read
  * @return string
  */
 public function read($size)
 {
     return event_buffer_read($this->resource, $size);
 }
コード例 #19
0
ファイル: Connection.php プロジェクト: ruslanchek/phpdaemon
 /**
  * Read data from the connection's buffer
  * @param integer Max. number of bytes to read
  * @return string Readed data
  */
 public function read($n)
 {
     if (!isset($this->buffer)) {
         return false;
     }
     if (isset($this->readEvent)) {
         if (Daemon::$useSockets) {
             $read = socket_read($this->fd, $n);
             if ($read === false) {
                 $no = socket_last_error($this->fd);
                 if ($no !== 11) {
                     // Resource temporarily unavailable
                     Daemon::log(get_class($this) . '::' . __METHOD__ . ': id = ' . $this->id . '. Socket error. (' . $no . '): ' . socket_strerror($no));
                     $this->onFailureEvent($this->id);
                 }
             }
         } else {
             $read = fread($this->fd, $n);
         }
     } else {
         $read = event_buffer_read($this->buffer, $n);
     }
     if ($read === '' || $read === null || $read === false) {
         $this->reading = false;
         return false;
     }
     return $read;
 }
コード例 #20
0
ファイル: prefork.php プロジェクト: Aishwariya/prefork
 public function read_request($event, $length)
 {
     $request_message = event_buffer_read($this->offer_event, $length);
     event_buffer_disable($this->offer_event, EV_READ | EV_WRITE);
     event_buffer_free($this->offer_event);
     unset($this->offer_event);
     socket_shutdown($this->offer_socket);
     socket_close($this->offer_socket);
     unset($this->offer_socket);
     $this->become_intern($request_message);
 }
コード例 #21
0
 public function read($n = NULL)
 {
     if ($n === NULL) {
         $n = $this->readPacketSize;
     }
     if ($this->noEvents) {
         if (!$this->readFD) {
             return '';
         }
         $data = fread($this->readFD, $n);
         if ($data === FALSE) {
             return '';
         }
         return $data;
     }
     if ($this->readBuf === FALSE) {
         return '';
     }
     $r = event_buffer_read($this->readBuf, $n);
     if ($r === NULL) {
         $r = '';
     }
     if ($r === FALSE) {
         throw new Exception('read buffer failed.');
     }
     return $r;
 }
コード例 #22
0
 /**
  * @param $conn_id
  * @param $count
  * @return string
  */
 public function readFromBuffer($conn_id, $count)
 {
     $readed = event_buffer_read($this->connection_buffers[$conn_id], $count);
     self::log('Connection', $conn_id, 'read ' . strlen($readed) . ' chars from buffer');
     return $readed;
 }
コード例 #23
0
ファイル: Libevent.php プロジェクト: nangong92t/go_src
 /**
  * 数据可读时触发的函数
  * @param fd $buffer
  * @param mixed $args
  */
 public function bufferCallBack($buffer, $args)
 {
     /* $data = '';
        while ($tmp = event_buffer_read($buffer, 10240)) 
        {
            $data .= $tmp; 
        } */
     $data = event_buffer_read($buffer, 10240);
     return call_user_func_array($args['func'], array($args['fd'], strlen($data), $data, $args['args']));
 }
コード例 #24
0
 function ev_read($buffer, $id)
 {
     $data = '';
     while ($read = event_buffer_read($buffer, 1024)) {
         $data .= $read;
     }
     if (APP_DEBUG === true) {
         Log::write("eSeal[{$id}] " . __METHOD__ . " > " . $data, Log::INFO);
     }
     if ($data == 'QUIT') {
         $this->_closeConnections();
     } else {
         if ($data == 'PING') {
             //ping的时候服务器返回服务器的时间
             event_buffer_write($buffer, date('Y-m-d H:i:s'));
         } else {
             $this->_processSeal($data);
         }
     }
 }
コード例 #25
0
function ev_read($buffer, $id)
{
    global $clients;
    global $redis;
    while ($read = event_buffer_read($buffer, 1024)) {
        $clients[$id]['read_buffer'] .= $read;
    }
    // Determine if the buffer is ready
    // The are two states when we determine if the buffer is ready.
    // State 1 is the command state, when we wait for a command from
    // the client
    // State 2 is the DATA state, when the client sends the data
    // for the email.
    if ($clients[$id]['state'] === 1) {
        // command state, strings terminate with \r\n
        if (strlen($clients[$id]['read_buffer']) > 1 && strpos($clients[$id]['read_buffer'], "\r\n", strlen($clients[$id]['read_buffer']) - 2) !== false) {
            $clients[$id]['read_buffer_ready'] = true;
        }
    } elseif ($clients[$id]['state'] === 2) {
        // DATA reading state
        // not ready unless you get a \r\n.\r\n at the end
        $len = strlen($clients[$id]['read_buffer']);
        if ($len > GSMTP_MAX_SIZE || $len > 4 && strpos($clients[$id]['read_buffer'], "\r\n.\r\n", $len - 5) !== false) {
            $clients[$id]['read_buffer_ready'] = true;
            // finished
            $clients[$id]['read_buffer'] = substr($clients[$id]['read_buffer'], 0, $len - 5);
        }
    } elseif ($clients[$id]['state'] === 3) {
        $clients[$id]['read_buffer_ready'] = true;
        // finished
    }
    if (!$redis) {
        //error_log("no redis ev read: ".var_dump($redis));
    }
    process_smtp($id);
    if (strlen($clients[$id]['response']) > 0) {
        event_buffer_write($buffer, $clients[$id]['response']);
        add_response($id, null);
    }
}