D
D
Drengel2015-04-14 11:07:06
PHP
Drengel, 2015-04-14 11:07:06

How to properly organize the reception of data from a GPS tracker?

Hello... We have created a gps monitoring system. We settled on the problem of receiving data from 100 navigators. The script is written in php. With some work, it crashes in a day or a few hours. The data flow is large. Help in which direction to dig so that Socket Listen does not hang up !!!
Code example:

<?php
ini_set('default_socket_timeout', 3000);

sleep(60);

set_time_limit(0);

$address = '0.0.0.0';
$port = 1234;

$Position_related_report_1 = array('GTTOW', 'GTDIS', 'GTIOB', 'GTSPD', 'GTSOS', 'GTRTL', 'GTDOG', 'GTIGL', 'GTHBM');
$Position_related_report_2 = array('GTFRI');
$Position_related_report_3 = array('GTERI');
$Position_related_report_4 = array('GTEPS', 'GTAIS');
$Position_related_report_5 = array('GTLBC');
$Position_related_report_6 = array('GTIDA');
$Position_related_report_7 = array('GTGEO');
$Position_related_report_8 = array('GTGES');
$Event_report_1 = array('GTMPN', 'GTMPF', 'GTBTC');
$Event_report_2 = array('GTSTT');
$Event_report_3 = array('GTANT');
$Event_report_4 = array('GTIGN');
$Event_report_5 = array('GTIGF');
$Event_report_6 = array('GTGSS');
$Device_information_report = array('GTINF');

$General_command_array = array('GTTOW', 'GTDIS', 'GTIOB', 'GTSPD', 'GTSOS', 'GTRTL', 'GTDOG', 'GTIGL', 'GTHBM',
    'GTFRI', 'GTERI', 'GTEPS', 'GTAIS', 'GTLBC', 'GTIDA', 'GTGEO', 'GTGES', 'GTMPN',
    'GTMPF', 'GTBTC', 'GTSTT', 'GTANT', 'GTIGN', 'GTIGF', 'GTGSS', 'GTINF');
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);//0 or SOL_TCP
echo "PHP Socket Server started at " . $address . " " . $port . "\n";
if ($sock < 0) {
    echo "Error: " . socket_strerror(socket_last_error()) . "\r\n";
    exit();
} else {
    echo "OK \r\n";
}

// Bind the socket to an address/port
$bind = socket_bind($sock, $address, $port);
if ($bind < 0) {
    echo "Error: " . socket_strerror(socket_last_error()) . "\r\n";
    exit();
} else {
    echo "OK \r\n";
}
//разрешаем использовать один порт для нескольких соединений
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);

//socket_connect($sock, $address, $port) or die("Could not connect to server\n");
// Start listening for connections
echo "Listening socket... ";
/*Цифра 5 здесь означает, что мы разрешим встать в очередь на подключение к этому адресу максимум пяти клиентам.
sudo sysctl -a | grep somaxconn
*/
$listen = socket_listen($sock, 100);//слушаем сокет
if ($listen < 0) {
    echo "Error: " . socket_strerror(socket_last_error()) . "\r\n";
    exit();
} else {
    echo "OK \r\n";
}


while (true) {
    echo "Waiting... ";
    /* Accept incoming requests and handle them as child processes */
    $client = socket_accept($sock);

    if ($client === false) {
        echo "Error: " . socket_strerror(socket_last_error()) . "\r\n";
        usleep(100);
    } else {
        echo "OK \r\n";
        echo "Client \"" . $client . "\" has connected\r\n";
        // Read the input from the client – 1024 bytes
        try {
            $str = socket_read($client, 1024, PHP_BINARY_READ);
            echo "socket_read: " . $str . "\n";
        } catch (Exception $e) {
      socket_close($client);
            continue;
        }

        if ($str == null) {
            echo "socket_read: NULL";
      socket_close($client);
      continue;
        }

        if ($str === false) {
            echo("Couldn't create socket: [" . socket_last_error() . "] " . socket_strerror(socket_last_error()));
        } else {

            $input = mb_convert_encoding($str, "UTF-8");

            echo "received: " . $input . "\n";

            $result = explode(',', $input);
            $command_array = explode(':', $result[0]);
            $command_type = $command_array[0];
            $command = $command_array[1];


            //Parser

            //Обработка данных
        }
    }
    // Close the client (child) socket
    socket_close($client);
}
// Close the master sockets
socket_close($sock);

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Grechushnikov, 2015-04-14
@maxyc_webber

maybe not quite in the topic, but suddenly socketo.me
will help you . Quite visited chats have been working on this engine for several months without stopping. there were no dependencies. I can assume that in the ratchet code, somewhere there may be a solution "for you antifreeze"

S
Sergey, 2015-04-14
@begemot_sun

Forget PHP for daemons as a nightmare, and especially for parsing the binary protocols of GPS trackers.

O
Oleg Abarmov, 2015-04-14
@XProx

I think it's worth checking everything
- the lifetime of the php script
- the size of the maximum memory allocated to it
- if the entry is in the database, then the TIME of an open connection to the database
PS: I spent 4 days in my time and didn’t find it, rewrote it on node js

D
Drengel, 2015-05-12
@Drengel

A month later. Rewrote to java. The script works perfectly. 100 trackers and a month without glitches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question