Pomoc za php sockets

Pomoc za php sockets

offline
  • Pridružio: 15 Jul 2009
  • Poruke: 150

Napisano: 24 Nov 2015 19:49

Poz, dali neko ima resenje za ovo:

U c jeziku postoji polling funkcija za sockets kao na primer ovo:
struct timeval startime; unsigned int GetTickCount() {    struct timeval tv;    gettimeofday(&tv, NULL);    // this will rollover ~ every 49.7 days    return (unsigned int)((tv.tv_sec - startime.tv_sec) * 1000 + tv.tv_usec / 1000); } int recv_nonb(int sock, uint8_t *buf, int len, int timeout) {    int ret;    int index = 0;    uint32_t last = GetTickCount()+timeout;    while (1) {       uint32_t now = GetTickCount();       if (now>last) return -2; // timeout       struct pollfd pfd;       pfd.fd = sock;       pfd.events = POLLIN | POLLPRI;       ret = poll(&pfd, 1, last-now);       if (ret > 0)       {          if (pfd.revents & (POLLIN|POLLPRI))          {             ret = recv(sock, buf+index, len-index, MSG_NOSIGNAL|MSG_DONTWAIT);             if (ret > 0)             {                index += ret;                if (index == len) return index;             }             else if (ret == 0) return 0; // disconected             else if ((ret == -1) && (errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINTR)) return -1; // error          }          if (pfd.revents & (POLLHUP|POLLNVAL)) return 0; // disconnected       }       else if (ret == 0) return -2; // timeout       else return -1; // error    } }

U c jeziku ova funkcija tacno zna kada server posalje header (4 bytes) i body (xxx bytes), znaci header je poseban paket, bodi je poseban paket, ali u php ne mogu da dobijem nikako header nego je sve spojeno zajedno header+body u jednom paketu (i to u retkim slucajevima), obicno dobijam nekoliko paketa od servera i tu bude do 63 headera+63 bodija spojeno u samo jedan paket, pa molim nekog ko ima iskustva sa ovim da pokusa da mi pomogne da prevedem ovu c funkciju u php ako je ikako moguce.

Ja sam pokusao nesto i radi ali kao sto sam rekao daleko daleko je drugcije u php-u jer ne mogu da nadjem ovaj poling nigde na netu za php. Dali uopste postoji nesto slicno kao sto je u c? Moja funkcija u php:

... $timeout = 2; $socket = null; if (($fp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) == false) {    throw new Exception( sprintf( "Unable to create a socket: %s", socket_strerror( socket_last_error() ) ) ); } if (!socket_connect($fp, $HOST, $PORT)) {    throw new Exception( sprintf( "Unable to connect to server %s: %s", MAIL_SERVER, socket_strerror( socket_last_error() ) ) ); } function recv_nonb($socket, &$buf, $len) {    global $timeout;    $last = time() + $timeout;    $n = 0;    $rcvd = 0;    $buf = "";    while (true)    {       $now = time();       if ($now > $last)       {          echo "receiving = timeout!<br />";          break;       }       $read = array($socket);       @socket_select($read, $write = NULL, $except = NULL, 0, "1000");       $n = @socket_recv($socket, $tmp, 1, MSG_DONTWAIT);       if ($n === false || $n == 0)       {          break;       }       else       {          $buf .= $tmp;          $rcvd++;       }    }    return $rcvd;    } ...

U php-u dobijam na primer ovaj paket (ovo je trenutno sada u hexu da bi se bolje videlo), umesto da prvo dobijem header kao jedan paket (komanda od 4 bytes)) + body kao sledeci paket i tako sve ukupno 63 paketa puta 2 (63 headera + 63 bodija), ja u php-u dobijam samo jedan paket od 63 headera+63 bodija sve spojeno: 0007002C000005927F7F81000100000100000000000000000200006A0000000000006C0000000001A8B5F8F3444C1E0F00070033000005737F7F86000100000100000000000000000300410600000000004108000000000050010000000001A8B5F8F3444C1E0F000700B80000058D7F7F98000100000100000000000000001600410600000000004108000000000050010000000000006A0000000000000000000000003311000000000033150000000000521100000000005221000000000052250000000000006C0000000000006D0000000000006800000000003317000000000033410000000000331B0000000000331D0000000000A8210000000000A8250000000000A82900000000000063000000000000650000000001A8B5F8F3444C1E0F0007002C000005697F7F8F0005000001000000000000000002050800000000000508100000000001A8B5F8F3444C1E0F00070025000005A37F7F900005000001000000000000000001030B000000000001A8B5F8F3444C1E0F00070025000005707F7F910005000001000000000000000001050F000000000001A8B5F8F3444C1E0F00070025000005867F7F9300050000010000000000000000010417000000000001A8B5F8F3444C1E0F00070025000005A07F7F9400050000010000000000000000010259000000000001A8B5F8F3444C1E0F00070025000005837F7F950005000001000000000000000001030A000000000001A8B5F8F3444C1E0F000700F7000005747F7F99000500000100000000000000001F023800000000000408100000000004150000000000FFFFFF0000000000000000000000FFF4300000000004082000000000041900000000000306000000000004060000000000FFFD000000000005160000000000020A000000000004230000000000FFF8F00000000004380000000000032500000000000417000000000004270000000000024F0000000000022F000000000002081000000000022E0000000000042200000000000517000000000004221000000000041710000000000259000000000002591000000000040620000000000428000000000001A8B5F8F3444C1E0F000700D40000056F7F7F9A000500000100000000000000001A03283000000000020A0000000000020A1000000000020A2000000000020A3000000000020A400000000003292000000000021110000000000411400000000004150000000000042400000000000328200000000003294000000000032840000000000208000000000002081000000000020820000000000428000000000004281000000000022F0000000000024F0000000000FFF720000000000259000000000002591000000000043010000000000405000000000001A8B5F8F3444C1E0F00070025000005937F7F8E00064800010000000000000000010000000000000001A8B5F8F3444C1E0F00070025000005977F7F9600062900010000000000000000010000000000000001A8B5F8F3444C1E0F0007008E000005807F7F9B00060200010000000000000000100000000000000000000100000000000002000000000000030000000000000400000000000005000000000000060000000000000700000000000008000000000000090000000000000A0000000000000B0000000000000C0000000000000D0000000000000E0000000000000F0000000001A8B5F8F3444C1E0F000700250000057C7F7F9C00060300010000000000000000010000000000000001A8B5F8F3444C1E0F000700870000058A7F7F9D000604000100000000000000000F000000000000000000010000000000000200000000000003000000000000040000000000000500000000000006000000000000070000000000002F00000000000E0000000000032000000000000320100000000006070D0000000006E41A00000000FFF4000000000001A8B5F8F3444C1E0F00070025000005957F7F9E00060800010000000000000000010000000000000001A8B5F8F3444C1E0F0007002C000005847F7F9F0006220001000000000000000002000000000000000000010000000001A8B5F8F3444C1E0F0007003A0000059A7F7FA000062900010000000000000000040000000000000000000100000000000002000000000000030000000001A8B5F8F3444C1E0F00070033000005947F7FA1000648000100000000000000000300000000000000000002000000000000030000000001A8B5F8F3444C1E0F00070033000005677F7FA20006520001000000000000000003000000000000000000150000000000FFFF0000000001A8B5F8F3444C1E0F0007003A0000058B7F7FA300065400010000000000000000040000000000000000001500000000000001000000000000050000000001A8B5F8F3444C1E0F000700250000059D7F7F8200098C00010000000000000000010000000000000001A8B5F8F3444C1E0F000700250000057B7F7F8C00096300010000000000000000010000000000000001A8B5F8F3444C1E0F000700250000056D7F7F8D0009CD00010000000000000000010000000000000001A8B5F8F3444C1E0F0007003A000005897F7FA400090D0001000000000000000004FFFFFF0000000000000000000000000001000000000000020000000001A8B5F8F3444C1E0F00070025000005907F7FA500090F00010000000000000000010000000000000001A8B5F8F3444C1E0F00070025000005687F7FA600092B00010000000000000000010000000000000001A8B5F8F3444C1E0F000700250000056B7F7FA700093E00010000000000000000010000000000000001A8B5F8F3444C1E0F00070025000005657F7FA800098E00010000000000000000010000000000000001A8B5F8F3444C1E0F00070025000005A47F7FA90009BE00010000000000000000010000000000000001A8B5F8F3444C1E0F000700250000058E7F7F92004ABF00010000000000000000010000000000000001A8B5F8F3444C1E0F000700250000059F7F7F83000B0200010000000000000000010000000000000001A8B5F8F3444C1E0F0007002C0000056C7F7F8A000B000001000000000000000002000000000000000000460000000001A8B5F8F3444C1E0F00070048000005A17F7FAA000B02000100000000000000000600000000000000000058000000000000A0000000000000A4000000000000A80000000000008C0000000001A8B5F8F3444C1E0F00070025000005787F7FAB000B0F00010000000000000000010000000000000001A8B5F8F3444C1E0F00070025000005877F7FAC000BAA00010000000000000000010000000000000001A8B5F8F3444C1E0F000700250000057D7F7F85000D0000010000000000000000010000C00000000001A8B5F8F3444C1E0F0007003A0000058F7F7F87000D02000100000000000000000400008C000000000000A0000000000000A4000000000000A80000000001A8B5F8F3444C1E0F0007003A000005797F7FAD000D0300010000000000000000040000040000000000000800000000000024000000000000280000000001A8B5F8F3444C1E0F00070041000005A27F7FAE000D060001000000000000000005000000000000000000C0000000000000C4000000000000C8000000000000CC0000000001A8B5F8F3444C1E0F00070025000005997F7FAF000D0700010000000000000000010000000000000001A8B5F8F3444C1E0F000700410000059C7F7FB0000D08000100000000000000000500000000000000000014000000000000180000000000001C000000000000200000000001A8B5F8F3444C1E0F0007003A000005757F7FB1000D0F0001000000000000000004000004000000000000080000000000000C000000000000100000000001A8B5F8F3444C1E0F000700410000056E7F7FB2000D2200010000000000000000050000000000000000000400000000000008000000000000C0000000000000100000000001A8B5F8F3444C1E0F000700330000056A7F7FB3000D95000100000000000000000300000000000000000004000000000000100000000001A8B5F8F3444C1E0F0007003A000005767F7FB4000D96000100000000000000000400000400000000000008000000000000100000000000000C0000000001A8B5F8F3444C1E0F00070025000005817F7F8B00170200010000000000000000010000000000000001A8B5F8F3444C1E0F0007003A0000058C7F7FB500172200010000000000000000040000000000000000000100000000000002000000000000030000000001A8B5F8F3444C1E0F00070033000005827F7F80001810000100000000000000000300000000000000004001000000000041010000000001A8B5F8F3444C1E0F000700330000057E7F7F84001880000100000000000000000300201100000000002111000000000021130000000001A8B5F8F3444C1E0F00070033000005727F7F88001802000100000000000000000300000000000000002011000000000021110000000001A8B5F8F3444C1E0F0007003A000005917F7F8900184300010000000000000000040000000000000000341100000000003511000000000080110000000001A8B5F8F3444C1E0F00070025000005717F7F9700180300010000000000000000010000000000000001A8B5F8F3444C1E0F00070033000005857F7FB60018030001000000000000000003008C1100000000008D11000000000000000000000001A8B5F8F3444C1E0F00070033000005967F7FB7001805000100000000000000000300000000000000008C1100000000008D110000000001A8B5F8F3444C1E0F000700250000059E7F7FB800181200010000000000000000010000000000000001A8B5F8F3444C1E0F000700640000059B7F7FB9001830000100000000000000000A0000000000000000AA800000000000AA810000000000AA830000000000AA850000000000AA870000000000AA890000000000341100000000003511000000000080110000000001A8B5F8F3444C1E0F000700250000057F7F7FBA00183400010000000000000000010000000000000001A8B5F8F3444C1E0F00070033000005887F7FBB001835000100000000000000000300000000000000000401000000000011030000000001A8B5F8F3444C1E0F00070033000005987F7FBC00183D000100000000000000000300000000000000005401000000000054110000000001A8B5F8F3444C1E0F0007003A000005777F7FBD001841000100000000000000000400000000000000005A0100000000005B0100000000005B030000000001A8B5F8F3444C1E0F0007002C0000057A7F7FBE0018430001000000000000000002000000000000000034110000000001A8B5F8F3444C1E0F
Header paket: (uzecu za primer deo iz paketa koji sam dobio preko php funkcije, ovo: 0007002C) je u prevodu komanda 0x0007 (sedmica iz enuma ili CC_MSG_CARD_ADD, prvih 2 bytes) i drugih 2 bytes 0x002c predstavlja velicinu bodija, to je jedan paket

Body paket: odmah za header paketom treba da ide paket (body) i on je velicine 0x002c bytes, onda opet ide komanda pa body, komanda pa body...i tako u krug dok ne istera do kraja CARD_ADD

Ovo u php ja ne mogu da dobijem niti imam ideju kako da odradim da bude isto kao sto je u C, dobijam sve spojeno u jedan paket. Dali je ovo nemoguce uraditi u php-u?

Sve komande koje trenutno treba da obradim su:
#define CC_MAXMSGSIZE   2048 typedef enum {    CC_MSG_CLI_INFO,         // client -> server    CC_MSG_ECM_REQUEST,      // client -> server    CC_MSG_EMM_REQUEST,      // client -> server    CC_MSG_CARD_DEL = 4,      // server -> client    CC_MSG_BAD_ECM,    CC_MSG_KEEPALIVE,      // client -> server    CC_MSG_CARD_ADD,         // server -> client    CC_MSG_SRV_INFO,         // server -> client    CC_MSG_CMD_0B = 0x0b,   // server -> client ???????    CC_MSG_ECM_NOK1 = 0xfe,   // server -> client ecm queue full, card not found    CC_MSG_ECM_NOK2 = 0xff,   // server -> client    CC_MSG_NO_HEADER = 0xffff } cc_msg_cmd;
Ja znam da to moze da se uradi sa preg_match ili nekom slicnom metodom, ali to jednostavno nije resenje jer to nije to i jer ce kasnije to biti problem procesuiranju drugih komandi, treba mi nesto identicno kao sto je u c jeziku u navedenoj funkciji! Dali ima resenja za ovo ili da odustajem?

Dopuna: 25 Nov 2015 18:11

Reseno vrlo jednostavno sa receiving prvo 4 bytes pa onda receiving ostatak:

$MSG_CLI_DATA         = "0000"; $MSG_CW_ECM           = "0001"; $MSG_EMM_ACK          = "0002"; $MSG_CARD_REMOVED     = "0004"; $MSG_CMD_05           = "0005"; $MSG_KEEPALIVE        = "0006"; $MSG_NEW_CARD         = "0007"; $MSG_SRV_DATA         = "0008"; $MSG_CMD_0A           = "000a"; $MSG_CMD_0B           = "000b"; $MSG_CMD_0C           = "000c"; // CCCam 2.2.x fake client checks $MSG_CMD_0D           = "000d"; $MSG_CMD_0E           = "000e"; $MSG_NEW_CARD_SIDINFO = "000f"; $MSG_SLEEPSEND        = "0080"; //Sleepsend support $MSG_CACHE_PUSH       = "0081"; //CacheEx Cache-Push In/Out $MSG_CACHE_FILTER     = "0082"; //CacheEx Cache-Filter Request $MSG_CW_NOK1          = "00fe"; //Node no more available $MSG_CW_NOK2          = "00ff"; //No decoding $MSG_NO_HEADER        = "ffff"; function recv_nonb($socket, &$buf, $len, $timeout) {    $last = time() + $timeout;    $n = 0;    $rcvd = 0;    $buf = "";    $read = array($socket);    while (true)    {       $now = time();       if ($now > $last)       {          echo "receiving = timeout!<br />";          return -1;       }       if ($n === 0) @socket_select($read, $write = NULL, $except = NULL, 0, "1000");       $n = @socket_recv($socket, $tmp, 1, MSG_DONTWAIT);       if ($n > 0)       {          $rcvd += $n;          $buf .= $tmp;          if ($rcvd == $len)          {             break;          }       }       else       {          $rcvd = 0;          break;       }    }    return $rcvd;    } ...blablabla          if ($logedin) {             cc_sendinfo_srv();             $last = time() + $timeout;             while (time() < $last)             {                while (($received = recv_nonb($fp, $data, 4, $timeout)))                {                   switch ($received)                   {                      case -1:                         echo "timeout receiving data!\n";                         break;                      case 0:                         break;                      case 4:                         $command = decrypt("server", $data, strlen($data));                         $len = hexdec(substr($command, 4, 4));                         $command = substr($command, 0, 4);                         switch ($command)                         {                            case $MSG_CLI_DATA:                               break;                            case $MSG_SRV_DATA:                               $recv_len = recv_nonb($fp, $data, $len, $timeout);                               if ($recv_len != $len) invalid_arg("server info corupted data!");                               $data = HexToBin(decrypt("server", $data, strlen($data)));                               get_server_info($data);                               break;                            case $MSG_NEW_CARD:                               $recv_len = recv_nonb($fp, $data, $len, $timeout);                               if ($recv_len != $len) invalid_arg("add card corupted data!");                               $data = decrypt("server", $data, strlen($data));                               echo $data . "<br /><br />";                               break;                            default:                               echo "unknoown command!<br />";                               break;                         }                         break;                      default:                         break;                   }                }             }          } blablabla...

Eto ako nekome zatreba socket receive funkcija, ova sigurno radi posao.



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
Ko je trenutno na forumu
 

Ukupno su 1091 korisnika na forumu :: 38 registrovanih, 8 sakrivenih i 1045 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: A.R.Chafee.Jr., Arahne, Arsenije, Ben Roj, BlekMen, bojank, bokisha253, Centauro, Denaya, Djokislav, Dogma21, doloress, Dr.Strangelove, drimer, Georgius, herrDule, kokodakalo, kuntalo, LUDI, mikrimaus, Milan A. Nikolic, MILO-VAN, Milometer, Mirage 2000N, moldway, nebidrag, nemkea71, nenad81, nikoladim, Pohovani_00, Povratak1912, RED4G-304, royst33, Sirius, theNedjeljko, uruk, User98, vrag81