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;
}
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;
}
private $amserver;
private $socket;
private $socket_ssl;
+ private $listen_socket;
+ private $listen_socket_ssl;
}