X-Git-Url: http://git.pjr.cc/?p=pengine.git;a=blobdiff_plain;f=lib%2FinterComms.php;fp=lib%2FinterComms.php;h=f53a4a0176e58f402a027c99c04a6e2bb617abc3;hp=52d874da815986645de76cc3ee29f664a3594d3b;hb=527c1ed71988910d53837e504d5b1ba0b16888aa;hpb=c0b76ea046b221cafdc183a2a79d31ff9400ed19 diff --git a/lib/interComms.php b/lib/interComms.php index 52d874d..f53a4a0 100644 --- a/lib/interComms.php +++ b/lib/interComms.php @@ -2,16 +2,19 @@ define("NETCOM_PORT", 14001); define("NETCOM_PORT_SSL", 14002); + +// comms on this thing are very serial, the server are only capable of processing +// one thing at a time... for now class netCom { function __construct($am_i_a_server = false, $server_addr = "127.0.0.1") { // i have to set it to something, right? - $semKey = ftok(__FILE__, "pengine"); - $encrypt = false; + $this->semKey = ftok(__FILE__, "p"); + $this->encrypt = false; - $amserver = $am_i_a_server; - $server = $server_addr; + $this->amserver = $am_i_a_server; + $this->server = $server_addr; } @@ -19,23 +22,70 @@ class netCom { function go() { if($this->amserver) { + $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + socket_bind($this->listen_socket, "127.0.0.1", NETCOM_PORT); + socket_listen($this->listen_socket); + + } else { + $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + $res = socket_connect($this->socket, "127.0.0.1", NETCOM_PORT); + if(!$res) { + echo "fail on connect\n"; + socket_close($this->socket); + return false; + } } } - function serverLoop() + function waitForConnection() { - + socket_listen($this->listen_socket); + $this->socket = socket_accept($this->listen_socket); } - - function sendMessage() - { + + function sendMessage($message_array) + { + $datacomp = base64_encode(serialize($message_array)); + $tosend = "PEN:$datacomp:INE"; + + socket_send($this->socket, $tosend, strlen($tosend), 0); + // get up to one meg of data - this is bad... i can feel this function + // hurting alot + // TODO FIX THIS - its garbage code... im not really sure how to handle this really + // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need + // timeouts now. } function receiveMessage() { + $recvd = ""; + $continue = true; + while($continue) { + $size = socket_recv($this->socket, $recvd_a, 1024, 0); + $recvd .= $recvd_a; + if(preg_match("/.*\:INE$/", $recvd)) { + // we have a full string... break out + $continue = false; + break; + } + } + + + // first check we got something that makes sense + if(preg_match("/^PEN:.*:INE/", $recvd) < 1) { + socket_close($this->socket); + echo "Returned data is not in right format\n"; + // we have a problem jim + return false; + } + + $xps = explode(":", $recvd); + $component = unserialize(base64_decode($xps[1])); + + return $component; } @@ -46,6 +96,8 @@ class netCom { private $amserver; private $socket; private $socket_ssl; + private $listen_socket; + private $listen_socket_ssl; }