gnope php gtk gui gnope.org  

Shutting Down Socket

 
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners
View previous topic :: View next topic  
Author Message
WorldDrknss



Joined: 06 Jul 2007
Posts: 8

PostPosted: Tue Jan 15, 2008 6:40 pm    Post subject: Shutting Down Socket Reply with quote

I have two functions:

start_btn and stop_btn,
the start_btn starts the socket and uses Gtk::io_add_watch.
The real question is, how can I shutdown the socket from the start_btn function using another function stop_btn.

This is what I got so far:
Code:
function start_btn(){
 global $finalip;
 global $finalpt;
 global $start_btn;
 global $stop_btn;
 $socket_address = $finalip;
 $socket_port = $finalpt;
 $socket = stream_socket_server("tcp://$socket_address:$socket_port", $errno, $errstr);
 Gtk::io_add_watch($socket,Gtk::IO_IN,'socket_receive');
 function socket_receive($stream, $condition){
   global $buffer;
   global $console;
   $iter = $buffer->get_end_iter();
   $sendsocket = stream_socket_accept($stream);
   $lock = "\$Lock ".generate_lock()." Pk=".generate_pk()."|";
   fwrite($sendsocket, $lock, strlen($lock));
   $hubname = "\$HubName WD Redirect Array|";
   fwrite($sendsocket, $hubname, strlen($hubname));
   $buff = fread($sendsocket, 2048);
   if(preg_match('/(\$ValidateNick\s([^\|]*)\|)/', $buff, $pnick)) {
    $vnick = $pnick[2];
    $hello = "\$Hello ".$vnick."|";
    fwrite($sendsocket, $hello, strlen($hello));
   }
   $nicklist = "\$NickList WD\$\$|";
   fwrite($sendsocket, $nicklist, strlen($nicklist));
   $oplist = "\$OpList WD\$\$|";
   fwrite($sendsocket, $oplist, strlen($oplist));
   $getaddress = getredirect();
   $redirect = "\$ForceMove ".$getaddress."|";
   fwrite($sendsocket, $redirect, strlen($redirect));
   $to = "\$To: $vnick From: WDR \$<WDR> You are being redirected to ".$getaddress." because: WDR|";
   fwrite($sendsocket, $to, strlen($to));
   $log = date("[h:i:s]")." -> $vnick is being redirect to: $getaddress";
   $buffer->insert($iter, $log."\n");
   $console->scroll_to_mark($buffer->get_insert(), 0);
   Gtk::io_add_watch($stream,Gtk::IO_IN,'socket_receive');
 }
 $start_btn->set_sensitive(false);
 $stop_btn->set_sensitive(true);
}


but for the life of me I can not figure out how to shutdown the socket using another function.
Back to top
kksou



Joined: 06 Sep 2006
Posts: 25

PostPosted: Wed Jan 16, 2008 10:59 am    Post subject: Reply with quote

Gtk::io_add_watch is very similar to Gtk::timeout_add.

Take a look at:
http://gtk.php.net/manual/en/gtk.method.timeout_add.php

In one of the comments, it says:
//at the end, return "true" if the timeout shall
// be executed again. If you don't return anything
// or return false, the timeout is stopped.

So to stop the Gtk::io_add_watch, you return a false at the end of the callback function.

Have modified your code slightly and attached it below.

Note that I've added one more global variable $io_stop. When the user clicks on the stop button, it will set this to 1, which will stop the callback function of the Gtk::io_add_watch.

Regards,
/kksou

Code:
function start_btn(){
   global $finalip;
   global $finalpt;
   global $start_btn;
   global $stop_btn;
      
   $socket_address = $finalip;
   $socket_port = $finalpt;
   $socket = stream_socket_server("tcp://$socket_address:$socket_port", $errno, $errstr);
   
   global $io_stop;
   $io_stop = 0;
   Gtk::io_add_watch($socket,Gtk::IO_IN,'socket_receive');
   
   $start_btn->set_sensitive(false);
   $stop_btn->set_sensitive(true);
}

function stop_btn(){
   global $io_stop;
   $io_stop = 1;
}

function socket_receive($stream, $condition){
   global $buffer;
   global $console;
   $iter = $buffer->get_end_iter();
   $sendsocket = stream_socket_accept($stream);
   $lock = "\$Lock ".generate_lock()." Pk=".generate_pk()."|";
   fwrite($sendsocket, $lock, strlen($lock));
   $hubname = "\$HubName WD Redirect Array|";
   fwrite($sendsocket, $hubname, strlen($hubname));
   $buff = fread($sendsocket, 2048);
   if(preg_match('/(\$ValidateNick\s([^\|]*)\|)/', $buff, $pnick)) {
      $vnick = $pnick[2];
      $hello = "\$Hello ".$vnick."|";
      fwrite($sendsocket, $hello, strlen($hello));
   }
   $nicklist = "\$NickList WD\$\$|";
   fwrite($sendsocket, $nicklist, strlen($nicklist));
   $oplist = "\$OpList WD\$\$|";
   fwrite($sendsocket, $oplist, strlen($oplist));
   $getaddress = getredirect();
   $redirect = "\$ForceMove ".$getaddress."|";
   fwrite($sendsocket, $redirect, strlen($redirect));
   $to = "\$To: $vnick From: WDR \$<WDR> You are being redirected to ".$getaddress." because: WDR|";
   fwrite($sendsocket, $to, strlen($to));
   $log = date("[h:i:s]")." -> $vnick is being redirect to: $getaddress";
   $buffer->insert($iter, $log."\n");
   $console->scroll_to_mark($buffer->get_insert(), 0);

   // you can remove the next line
   //Gtk::io_add_watch($stream,Gtk::IO_IN,'socket_receive');

   //at the end, return "true" if the io_add_watch shall
   //be executed again. If you don't return anything
   // or return false, the io_add_watch is stopped.
   global $io_stop;
   if ($io_stop) {
      return false;
   } else {
      return true;
   }
}
Back to top
WorldDrknss



Joined: 06 Jul 2007
Posts: 8

PostPosted: Wed Jan 16, 2008 5:22 pm    Post subject: Reply with quote

Thank you, ill try this tonight and get back with you. Also I have another question. Does Gtk::io_add_watch handle multiple clients simultaneously. Since hundred of users can pass through this at a single given time.
Back to top
WorldDrknss



Joined: 06 Jul 2007
Posts: 8

PostPosted: Thu Jan 17, 2008 2:38 am    Post subject: Reply with quote

I have been working on this. This is what I got so far, but I have some questions.

Code:
<?php
define('GTK_ARRAY_RUNNING', 1);
define('GTK_ARRAY_SHUTDOWN', 2);
class ArrayProtocol {
   protected $gtkios;
   protected $socket;
   protected $hostname;
   protected $port;
   protected $errno;
   protected $errstr;
   protected $sendsock;
   protected $timeout;
   protected $lock;
   protected $key;
   protected $status;
   
   function __construct() {
      $this->gtkios = array();
      $this->timeout_num = 1000;
      $this->timeout = Gtk::timeout_add($this->timeout_num, array($this, 'timeout'));
      $this->status = GTK_ARRAY_SHUTDOWN;
   }
   
   function connect($hostname, $port){
      $this->hostname = $hostname;
      $this->port = $port;
      $this->socket = stream_socket_server("tcp://".$this->hostname.":".$this->port, $this->errno, $this->errstr);
      if(!$this->socket) trigger_error("GtkIO::connect : connection error : " . "{$this->errstr} ({$this->errno})");
      else $this->gtkio_setup();
      return $this->socket;
   }
   
   function gtkio_setup() {
       $this->gtkios[Gtk::IO_IN] = Gtk::io_add_watch($this->socket, Gtk::IO_IN, array($this, 'io_in'));
       $this->gtkios[Gtk::IO_OUT] = Gtk::io_add_watch($this->socket, Gtk::IO_OUT, array($this, 'io_out'));
       $this->gtkios[Gtk::IO_HUP] = Gtk::io_add_watch($this->socket, Gtk::IO_HUP, array($this, 'io_hup'));
       $this->gtkios[Gtk::IO_PRI] = Gtk::io_add_watch($this->socket, Gtk::IO_PRI, array($this, 'io_pri'));
       $this->gtkios[Gtk::IO_ERR] = Gtk::io_add_watch($this->socket, Gtk::IO_ERR, array($this, 'io_err'));
       $this->gtkios[Gtk::IO_NVAL] = Gtk::io_add_watch($this->socket, Gtk::IO_NVAL, array($this, 'ion_val'));
   }
   
   function write($data) { return fwrite($sendsock, $data, strlen($data)); }
   function read($size = 2048) { return fread($sendsock, $size); }
   function eof() { return feof($this->socket); }
   
   function io_in($stream, $condition) {
      $this->sendsock = stream_socket_accept($stream);
      $generated_lock = $this->generate_lock();
      $generated_pk = $this->generate_pk();
      
      $lock = "\$Lock $generated_lock Pk=$generated_pk|";
      $this->write($lock);
      
      global $hub_name; //<- Pulls from PHP-GTK Input Box
      $sendhub = "\$HubName $hubname|";
      $this->write($sendhub);
      
      $buff = $this->read();
      if(preg_match('/(\$ValidateNick\s([^\|]*)\|)/', $buff, $pnick)) {
          $vnick = $pnick[2];
          $hello = "\$Hello ".$vnick."|";
         $this->write($hello);
      }
   }
   
   function io_out($stream, $condition) { return true; }
   function io_hup($stream, $condition) { return true; }
   function io_pri($stream, $condition) { return true; }
   function io_err($stream, $condition) { return true; }
   
   function timeout() {
      static $count = 0;
      $count++;
      if($count > 3) { $this->disconnect(); return; }
      if($this->status = GTK_ARRAY_RUNNING && $this->socket != null)
         $this->timeout = Gtk::timeout_add($this->timeout_num, array($this, 'timeout'));
   }
   
   function generate_lock(){
      $hlock = "";
      for($i = 0; $i < rand(80, 134); $i++) { $lock[] = chr(94.0 * rand() + 33); } foreach($lock as $lk) { $hlock .= $lk; }
      $this->lock = $hlock;
      return $this->lock;
   }
   
   function generate_pk(){
      $hpk = "";
      for($i = 0; $i < 16; $i++) { $pk[] = chr(94.0 * rand() + 33); } foreach($pk as $k) { $hpk .= $k; }
      $this->pk = hpk;
      return $this->pk;
   }
   
   function disconnect(){ fclose($this->socket); }
}

$rarray = new ArrayProtocol();
?>


Will this
Code:
global $hub_name; //<- Pulls from PHP-GTK Input Box
to be able to pull the text from a PHP-GTK Input Box. Another question can I post back to the console view within the class using:
Code:
$log = date("[h:i:s]")." -> $vnick is being redirect to: $getaddress";
   $buffer->insert($iter, $log."\n");
   $console->scroll_to_mark($buffer->get_insert(), 0);
as in my first example code. Last but not least, this can handle multiple clients at once?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Templates enhanced by DigiWiki. Hosting by Tradebit.