Example #1
0
 public function onMessageReceived(SocketServer $server, SocketClient $sender, $message)
 {
     if (!$sender->verified) {
         $cookies = $this->parseCookies(@$sender->headers['Cookie'] ?: "");
         $sender->verified = $this->auth($cookies);
         if (!$sender->verified) {
             $sender->send(array('type' => 'error', 'error' => 'Auth failed'));
             $sender->disconnect();
             return;
         }
     }
     echo "MESSAGE DATA: " . $message . "\n";
     $data = json_decode($message);
     if ($data) {
         if ($data->type == 'kill' && $sender->process) {
             $sender->process->stop();
         } elseif ($data->type == 'shell' && $data->cmd) {
             if ($sender->process) {
                 $sender->process->stop();
                 usleep(1000 * 10);
             }
             $process = new Process($data->cmd);
             $process->start(function ($type, $buffer) use($sender) {
                 if (Process::ERR !== $type) {
                     $sender->buffer = $sender->buffer ?: "";
                     $sender->buffer .= $buffer;
                 } else {
                 }
             });
             $sender->process = $process;
         }
     }
 }
Example #2
0
 public function sendIdipCmd($ip, $port, $data, $apiId = ServerApi::GPT_IDIP_GM)
 {
     $this->debug('start sendIdipCmd: ' . microtime(true));
     $socket = new SocketClient($ip, $port);
     $socket->setLogger($this->logger);
     $this->debug('start socket->idipCall: ' . microtime(true));
     $ret = $socket->idipCall($apiId, $data);
     $this->debug('return: ' . microtime(true));
     return $ret;
 }
Example #3
0
 /**
  * Check if the source is reachable
  *
  * @todo Faire un vrai isReachable (envoi de CEcho)
  *
  * @return boolean
  */
 function isReachableSource()
 {
     if (!$this->_socket_client) {
         $this->setSocketClient();
     }
     if ($this->_socket_client->connect() !== null) {
         $this->_reachable = 0;
         $this->_message = CAppUI::tr("CSourceDicom-unreachable-source", $this->host);
         return false;
     }
     return true;
 }
Example #4
0
 function action_deletemsg()
 {
     if (!isset($_COOKIE['username'])) {
         header("Location: /");
         die;
     }
     $this->model = new ModelMessage($_POST['msgid'], $_COOKIE['username'], null, 1, null, null);
     $res = $this->model->deleteMessage();
     if ($res) {
         echo 'success';
         $arr = array('action' => 'deletemsg', 'user' => $_COOKIE['username'], 'msgid' => $_POST['msgid']);
         $message = json_encode($arr);
         $sock = new SocketClient();
         $sock->connect('localhost', 8000, '/');
         $sock->sendData($message);
         $sock->disconnect();
     }
 }
 public function __construct($connection, SocketServerBroadcast $server)
 {
     parent::__construct($connection);
     $this->server = $server;
 }
 /**
  * Reads multipart data from sourceConnection and streams it to the
  * targetConnection.Returns the body of the request or the status code in
  * case there is no body.
  *
  * @param $method
  * @param $path
  * @param $streamEnd
  * @param array $requestHeaders
  * @return mixed|string
  * @throws \Exception
  * @throws \HTTPException
  */
 protected function sendStream($method, $path, $streamEnd, $requestHeaders = array())
 {
     $dataStream = $this->sourceConnection;
     // Read the json doc. Use _attachments field to find the total
     // Content-Length and create the request header with initial doc data.
     // At present CouchDB can't handle chunked data and needs
     // Content-Length header.
     $str = '';
     $jsonFlag = 0;
     $attachmentCount = 0;
     $totalAttachmentLength = 0;
     $streamLine = $this->getNextLineFromSourceConnection();
     while ($jsonFlag == 0 || $jsonFlag == 1 && trim($streamLine) == '') {
         $str .= $streamLine;
         if (strpos($streamLine, 'Content-Type: application/json') !== false) {
             $jsonFlag = 1;
         }
         $streamLine = $this->getNextLineFromSourceConnection();
     }
     $docBoundaryLength = strlen(explode('=', $requestHeaders['Content-Type'], 2)[1]) + 2;
     $json = json_decode($streamLine, true);
     foreach ($json['_attachments'] as $docName => $metaData) {
         // Quotes and a "/r/n"
         $totalAttachmentLength += strlen('Content-Disposition: attachment; filename=') + strlen($docName) + 4;
         $totalAttachmentLength += strlen('Content-Type: ') + strlen($metaData['content_type']) + 2;
         $totalAttachmentLength += strlen('Content-Length: ');
         if (isset($metaData['encoding'])) {
             $totalAttachmentLength += $metaData['encoded_length'] + strlen($metaData['encoded_length']) + 2;
             $totalAttachmentLength += strlen('Content-Encoding: ') + strlen($metaData['encoding']) + 2;
         } else {
             $totalAttachmentLength += $metaData['length'] + strlen($metaData['length']) + 2;
         }
         $totalAttachmentLength += 2;
         $attachmentCount++;
     }
     // Add Content-Length to the headers.
     $requestHeaders['Content-Length'] = strlen($str) + strlen($streamLine) + $totalAttachmentLength + $attachmentCount * (2 + $docBoundaryLength) + $docBoundaryLength + 2;
     if ($this->targetConnection == null) {
         $this->targetConnection = $this->targetClient->getConnection($method, $path, null, $requestHeaders);
     }
     // Write the initial body data.
     fwrite($this->targetConnection, $str);
     // Write the rest of the data including attachments line by line or in
     // chunks.
     while (!feof($dataStream) && ($streamEnd === null || strpos($streamLine, $streamEnd) === false)) {
         $totalSent = 0;
         $length = strlen($streamLine);
         while ($totalSent != $length) {
             $sent = fwrite($this->targetConnection, substr($streamLine, $totalSent));
             if ($sent === false) {
                 throw new \HTTPException('Stream write error.');
             } else {
                 $totalSent += $sent;
             }
         }
         // Use maxLength while reading the data as there may be no newlines
         // in the binary and compressed attachments, or the lines may be
         // very long.
         $streamLine = $this->getNextLineFromSourceConnection(100000);
     }
     // Read response headers
     $rawHeaders = '';
     $headers = array('connection' => $this->targetClient->getOptions()['keep-alive'] ? 'Keep-Alive' : 'Close');
     // Remove leading newlines, should not occur at all, actually.
     while (($line = fgets($this->targetConnection)) !== false && ($lineContent = rtrim($line)) === '') {
     }
     // Throw exception, if connection has been aborted by the server, and
     // leave handling to the user for now.
     if ($line === false) {
         // sendStream can't be called in recursion as the source stream can be
         // read only once.
         $error = error_get_last();
         throw HTTPException::connectionFailure($this->targetClient->getOptions()['ip'], $this->targetClient->getOptions()['port'], $error['message'], 0);
     }
     do {
         // Also store raw headers for later logging
         $rawHeaders .= $lineContent . "\n";
         // Extract header values
         if (preg_match('(^HTTP/(?P<version>\\d+\\.\\d+)\\s+(?P<status>\\d+))S', $lineContent, $match)) {
             $headers['version'] = $match['version'];
             $headers['status'] = (int) $match['status'];
         } else {
             list($key, $value) = explode(':', $lineContent, 2);
             $headers[strtolower($key)] = ltrim($value);
         }
     } while (($line = fgets($this->targetConnection)) !== false && ($lineContent = rtrim($line)) !== '');
     // Read response body
     $body = '';
     // HTTP 1.1 supports chunked transfer encoding, if the according
     // header is not set, just read the specified amount of bytes.
     $bytesToRead = (int) (isset($headers['content-length']) ? $headers['content-length'] : 0);
     // Read body only as specified by chunk sizes, everything else
     // are just footnotes, which are not relevant for us.
     while ($bytesToRead > 0) {
         $body .= $read = fgets($this->targetConnection, $bytesToRead + 1);
         $bytesToRead -= strlen($read);
     }
     // Reset the connection if the server asks for it.
     if ($headers['connection'] !== 'Keep-Alive') {
         fclose($this->targetConnection);
         $this->targetConnection = null;
     }
     // Handle some response state as special cases
     switch ($headers['status']) {
         case 301:
         case 302:
         case 303:
         case 307:
             // Temporary redirect.
             // sendStream can't be called in recursion as the source stream can be
             // read only once.
             throw HTTPException::fromResponse($path, new Response($headers['status'], $headers, $body));
     }
     return $body != '' ? json_decode($body, true) : array("status" => $headers['status']);
 }
Example #7
0
<?php

require_once 'SocketClient.php';
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$sock = new SocketClient();
$sock->connect('localhost', 8000, '/');
$sock->sendData('test');
$sock->disconnect();
Example #8
0
    public function recvPacket()
    {
        //echo "[@0:.]";
        list(, $packetSize) = unpack('v', $v = fread($this->f, 2));
        if (strlen($v) < 2) {
            throw new Exception("Error receiving a packet");
        }
        //echo "[@1:{$packetSize}]";
        list(, $packetType) = unpack('c', fread($this->f, 1));
        //echo "[@2:{$packetType}]";
        $packetData = $packetSize > 0 ? fread($this->f, $packetSize) : '';
        //echo "[@3:{$packetData}]";
        return new Packet($packetType, $packetData);
    }
}
$socketClient = new SocketClient();
$socketClient->connect('127.0.0.1', 9777);
$time = time();
for ($n = 0; $n < 100000; $n++) {
    //for ($n = 0; $n < 1000; $n++) {
    //for ($n = 0; $n < 100; $n++) {
    //for ($n = 0; $n < 20; $n++) {
    $socketClient->setUserBuffer(mt_rand(0, 100000), 0, $time + mt_rand(-50, 4), mt_rand(0, 500));
}
$socketClient->setUserBuffer(1000, 0, $time, 200);
$socketClient->setUserBuffer(1001, 0, $time, 300);
$socketClient->setUserBuffer(1000, 0, $time + 1, 300);
//$socketClient->setUserBufferFlush();
printf("Position(1000):%d\n", $pos_1000 = $socketClient->locateUserPosition(1000, 0));
printf("Position(1001):%d\n", $pos_1001 = $socketClient->locateUserPosition(1001, 0));
print_r($socketClient->listItems(0, $pos_1000, 3));