Evil_Master Posted September 22, 2013 Share Posted September 22, 2013 Hello, I am new to PHP Coding, I have a PHP shell that listens for messages and sends them back to all connected clients, it is used for sending messages on a gameserver to other gameservers. it is a modified version of CrossServer-AdminChat. What I want the PHP shell to do is to listen for variables sent after the 'a' flag is sent and count up all returns, and after it verifies that all of its clients have sent the variables, it will add them up, and send back a line to (you guessed it) all servers. unfortunately, I am not very familiar with PHP. Here is the shell: <?php //-------------------------------------------------------------------------------------------------- // PHP CrossServer-AdminChat (MasterServer) //-------------------------------------------------------------------------------------------------- /* * CrossServer-AdminChat v2.0 * Copyright (C) 2006 by KoST * http://www.amxmodx.org/forums/viewtopic.php?t=30427 * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details: * http://www.gnu.org/licenses/gpl.txt * */ //-------------------------------------------------------------------------------------------------- // specify the ip, port and password for the master-server: //-------------------------------------------------------------------------------------------------- define(SERVER_PASSWORD,"PASSWORD"); define(SERVER_IP,"I.P.ADDR.ESS"); define(SERVER_PORT,18107); //-------------------------------------------------------------------------------------------------- set_time_limit(0); ob_implicit_flush(); define(STATE_CONNECT,1); define(STATE_CHAT,2); define(STATE_TERMINATE,3); define(MAX_PACKET_LEN,500); define(MAX_OUTBUFFER_SIZE,20000); define(NOOP_TIMEOUT,30); if ($_SERVER['DOCUMENT_ROOT']) die("do not call this using a webbrowser !"); $o=new Server; if ($o->StartServer()){ while ($o){ $o->MainLoop(); // called all 10 secs (if server idle) } } class Server{ var $sockets=array(); var $outbuffers=array(); var $inbuffers=array(); var $info=array(); var $outbuffers_empty=true; var $num_clients=0; var $last_check=0; function Server(){ //return $this->StartServer(); } function StartServer(){ $server=socket_create(AF_INET,SOCK_STREAM,0); if ($server<0) {$this->LA('ERROR: socket_create()');return false;} socket_set_nonblock($server); socket_set_option($server, SOL_SOCKET,SO_REUSEADDR, 1); if (!socket_bind($server, SERVER_IP, SERVER_PORT)) {$this->LA('ERROR: socket_bind() (port in use?)');return false;} if (!socket_listen($server, 5)) {$this->LA('ERROR: socket_listen()');return false;} $this->sockets[0]=$server; $this->LA('Server ready'); return true; } function MainLoop(){ if ((time()-$this->last_check)>NOOP_TIMEOUT) {$this->CheckTimeout();$this->last_check=time();} if ($this->outbuffers_empty){ $changed=$this->sockets; $num=socket_select($changed,$write=NULL,$except=NULL,1,0); // wait }else{ $this->SendOutBuffers(); $changed=$this->sockets; $num=socket_select($changed,$write=NULL,$except=NULL,0,10000); // wait } foreach ($changed as $socket){ if ($socket==$this->sockets[0]){ $this->IncomingConnection(); }else{ if ($socket){ if (!socket_recv($socket,$buffer,1,MSG_PEEK)){ $this->ClientDisconnect($socket); }else{ if (strlen($this->outbuffers[$socket])<MAX_OUTBUFFER_SIZE){ $bytes=socket_recv($socket,$buffer,MAX_PACKET_LEN,0); } if ($bytes){ $this->IncomingData($socket,$buffer); }else{ $this->ClientDisconnect($socket); } } } } } } function LA($txt){ echo date('d.m.y H:i:s').' - '.$txt."\n"; } function IncomingConnection(){ $socket=socket_accept($this->sockets[0]); socket_set_nonblock($socket); if ($socket<=0){ $this->LA('incoming connection dropped'); }else{ $this->num_clients++; socket_getpeername ($socket,$addr,$port); $this->info[$socket]['IP']=$addr; $this->LA('incoming connection accepted ['.$addr.']'); $this->sockets[]=$socket; $this->info[$socket]['STATE']=STATE_CONNECT; $this->NewOP($socket); } } function ClientDisconnect($socket){ $this->num_clients--; $this->LA($socket.' client drop'); //$this->LA($this->num_clients.' clients connected'); socket_close($socket); $suck=$this->getIndex($socket); unset($this->outbuffers[$socket]); unset($this->inbuffers[$socket]); unset($this->info[$socket]); unset($this->sockets[$suck]); } function getIndex($socket){ return array_search($socket,$this->sockets); } function IncomingData($socket,$data){ //echo "."; $this->NewOP($socket); $buffer=&$this->inbuffers[$socket]; $buffer.=$data; $pos=strpos($buffer,"\n"); if (!$pos && strlen($buffer)>MAX_PACKET_LEN){ $this->LA('MAX_PACKET_LEN exceeded, dropping client..'); $this->ClientDisconnect($socket); } while ($pos!==false){ $txt=substr($buffer,0,$pos); $type=substr($txt,0,1); $buffer=substr($buffer,$pos+1); if (strlen($txt)>(MAX_PACKET_LEN-1)) { $this->LA('MAX_PACKET_LEN exceeded, dropping client..'); $this->ClientDisconnect($socket); return 0; } $len=strlen($txt); switch($this->info[$socket]['STATE']){ case STATE_CONNECT: if (strcmp($txt,"2".SERVER_PASSWORD)==0){ $this->LA($socket.' login OK'); //$this->LA($this->num_clients.' clients connected'); $this->info[$socket]['STATE']=STATE_CHAT; }else{ $this->LA($socket.' login FAILED'); $this->SendTo($socket,"login_failed\n"); $this->info[$socket]['STATE']=STATE_TERMINATE; } break; case STATE_CHAT: switch($type){ case 'c': if ($len>1) $this->SendToAll($socket,$txt."\n",true); break; case 'a': if ($len==1) $this->SendToAll($socket,$txt."\n",true); break; case 'r': if ($len>1) $this->SendToAll($socket,$txt."\n",true); break; case 's': if ($len>2) { $player_id=ord(substr($txt,1,1)); $this->SendToAll($socket,"c".substr($txt,2)."\n",false); $this->SendTo($socket,$txt."\n"); } break; case 'k': if ($len>1) $this->SendToAll($socket,$txt."\n",true); break; } break; } $pos=strpos($buffer,"\n"); } } function SendToAll($nsocket,$data,$to_self=false){ foreach ($this->sockets as $socket){ if ($socket!=$nsocket || $to_self){ if ($this->info[$socket]['STATE']==STATE_CHAT){ $this->SendTo($socket,$data); } } } } function SendTo($socket,$data){ $buffer=&$this->outbuffers[$socket]['buffer']; $buffer.=$data; $this->outbuffers_empty=false; } function SendOutBuffers(){ $all_zero=true; foreach ($this->sockets as $socket){ if ($socket!=$this->sockets[0]){ $buffer=&$this->outbuffers[$socket]['buffer']; if (strlen($buffer)>0){ if (!$written=socket_write($socket,$buffer)){ $this->LA("write error !!!"); $this->ClientDisconnect($socket); } if ($written>0) $this->NewOP($socket); $buffer=substr($buffer,$written); if (!$buffer){ if ($this->info[$socket]['STATE']==STATE_TERMINATE){ $this->ClientDisconnect($socket); } }else{ $all_zero=false; } } } } if ($all_zero) $this->outbuffers_empty=true; } function NewOP($socket){ $this->info[$socket]['LAST_OPERATION']=time(); } function CheckTimeout(){ $t=time(); foreach ($this->sockets as $socket){ if ($socket!=$this->sockets[0]){ if ($this->info[$socket]['LAST_OPERATION']==0) { $this->LA('TimeOut, dropping client..'); $this->ClientDisconnect($socket); }else{ $this->info[$socket]['LAST_OPERATION']=0; } } } } }; ?> Now, to add more details, when the shell recieves 'a' as the incoming data, I want it to send back a flag to all servers (can be anything really, the servers listen for it and will send back the variables) and when it recieves all of the data, it will send back a line 'counted $x players' but I do not even know where to begin with this Any help with this would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/282351-php-shell-listen-for-values-from-x-clients-send-info-back-after-totaled/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.