TCP/IP Server.

 

When ScriptEZ.API based script file is running, an identification area is appended

to the system menu of the console windows as following:

 

 

<?php

 

// PHP 5.3.x for Windows can be downloaded here: http://windows.php.net

//////////////////////////////////////////////////////////////////////////////

// Author: SY

// Demo ScriptEZ.API based application which uses facilities from RAD API:

// . TCP/IP usage

// . Threadpool usage

// . Echo() method usage which displays always messages into a console window

// . ScriptEZ.API Remoting Control Interface usage by NPTelnet.exe program

//   (Connect to ScriptEZ.API process with <processid (PID)> as connection name)

//

// Purpose: TCP/IP Server broadcasting random data packet 5 times/second

//////////////////////////////////////////////////////////////////////////////

 

// Global variables

$ScriptEZ      = null;

$ScriptEZCon   = null;

$hSocketSvr    = 0;

$hThreadPool   = 0;

$onExit        = 0;

$display_info  = 0;

$SentData      = 0;

$ReceivedData  = 0;

 

// (1) Events notification (callbacks) from ScriptEZ component object

class ScriptEZ_EventHandlers

{

    // Remoting Control Interface

    function AppEventHelp()

    {

      $AppEventHelp = "  **** Supported AppEvents ****" . "\n" .

                       "AppEventID = 3" . "\t" . "to exit" . "\n";

   

      return  $AppEventHelp;

    }

    function onAppEvent($EventID)

    {

      global $onExit;

      if($EventID == 3)

      {

        $onExit = 3;

      }

      return 0;

    }

    ////////////////////

 

    // Callbacks for socket object ( server side )

    function onConnectionRequest( $hSocketClientObjectProxy )

    {

      global $ScriptEZ,$hThreadPool;

   

      $r = $ScriptEZ->AddSocketClientObjectEventHandlers($hSocketClientObjectProxy);   

      // delegating to the thread pool....

      $ScriptEZ->SetSocketObjectLocalStorage($hSocketClientObjectProxy,0);

      $ScriptEZ->AddTask( $hThreadPool,$hSocketClientObjectProxy );

      return $r;

    }

   

    // Socket object notification (client/proxy side)

    function onConnectionState($hOwnerSocket, $state_desc, $error_number)

    {

      global $ScriptEZ;

       

      $ScriptEZ->Echo("\nConnectio state: $state_desc");

      return 0;

    }

    function onData($hOwnerSocket, $raw_data, $data_size)

    {

      global $ReceivedData;

      $ReceivedData = $ReceivedData + $data_size;

      return -1; // let the component free this data block

    }

    ////////////////////

   

    // Task notification for a thread pool

    function onRun( $hSocketClientProxy )

    {

      global $ScriptEZ,

             $SentData,

             $onExit;

     

      // Send random data 5 times per second

      $ScriptEZ->Sleep(100);

      if($onExit == 3)

      {

        $r = 0;

      }

      else

      {

        $dispatch_info = $ScriptEZ->GetSocketObjectLocalStorage($hSocketClientProxy);

        $dispatch_info++;

        $ScriptEZ->SetSocketObjectLocalStorage($hSocketClientProxy,$dispatch_info);

        if($dispatch_info == 2)

        {

            $r = rand(1,8192); // randomize from 1 to 8192 bytes

            $alea_string = str_pad("",$r,"x");

            $TestData = "BoM" . $alea_string . "EoM\t\0"; // terminate string with ‘\0’

            $r = strlen($TestData);

            $SentData = $SentData + $r;

            $ScriptEZ->SendSocketObjectData( $hSocketClientProxy, $TestData, $r );

            if( $r != 0) return 0;

            $ScriptEZ->SetSocketObjectLocalStorage($hSocketClientProxy,0);

        }

        $r = 1;

      }

      return $r;

    }

    ////////////////////

}

 

// (2) Events notification (callbacks) from ScriptEZCon component object

class ScriptEZ_ConnsoleRefresh

{

  // Task notification for a thread pool

  function onRun( $localStorage )

  {

      global $ScriptEZ,

             $ScriptEZCon,

             $hSocketSvr,

             $display_info,

             $SentData,

             $ReceivedData,

             $onExit;

            

      $ScriptEZCon->Sleep(100);

      $display_info++;

      if($display_info == 10)

      {

        $ScriptEZCon->ClearConsole();

        $own_memory = $ScriptEZ->ProbeMemoryStatus($ScriptEZ->GetCurrentProcessId);

        $ScriptEZCon->Echo("\nBroadcasting data..: 5 times/second" .

                           "\nConnected client(s): " . $ScriptEZ->GetActiveSocketClientObjectNumber($hSocketSvr) .

                           "\nSent Data..........: " . round($SentData/1024,2) . " KBytes" .

                           "\nReceived Data......: " . round($ReceivedData/1024,2) . " KBytes" .

                           "\nIn use memory......: " . $ScriptEZ->ProbeMemoryStatus(0) . "%%" .

                           "\nOwn used memory....: " . number_format($own_memory,0,'.',' ') . " KBytes" .

                           "\nThreads at work....: " . $ScriptEZ->ProbeMemoryStatus(-4));

        $display_info = 0;

      }

     

      if($onExit == 3)

      {

        $r = 0;

      }

      else

      {

        $r = 1;

      }

      return $r;

  }

}

// Main function entry

function Main()

{

    global  $ScriptEZ,

            $ScriptEZCon,

            $hSocketSvr,

            $hThreadPool,

            $onExit,

            $host_and_port;

   

    // create component objects and event sinks class

    $ScriptEZ    = new COM("ScriptEZ.API");

    $ScriptEZCon = new COM("ScriptEZ.API");

    $sink        = new ScriptEZ_EventHandlers();

    $sink2       = new ScriptEZ_ConnsoleRefresh();

   

    // install event sinks class

    com_event_sink($ScriptEZ, $sink, "_IAPIEvents");

    com_event_sink($ScriptEZCon, $sink2, "_IAPIEvents");

   

    // set up Remoting Control Interface

    $ScriptEZ->AppRegister();

   

 

    // creating 2 thread pools with 3 and 1 threads

    $hThreadPool = $ScriptEZ->CreateTaskQueueObject(3);

    $hThreadPool2= $ScriptEZCon->CreateTaskQueueObject(1);

   

    // create server socket object +  extra  encryption

    $hSocketSvr = $ScriptEZ->CreatePoolSocketServerObject(12346,0,100,8,0,0);

    if( $hSocketSvr > 0)

    {

       $ScriptEZ->CenterWindow(-1);

       $ScriptEZ->EnableSocketObjectDataEncryption( $hSocketSvr,true,-1 );

       $ScriptEZ->SetConsoleTitle( strftime("%d/%m/%y %X") . " - Demo PHP Server@12346");

       $ScriptEZCon->AddTask( $hThreadPool2,0); // this task will refresh info onto console window

    }

 

    // endless loop

    while(!$onExit)

    {

      com_message_pump(50);// dispatch COM events

    }

 

    // App unregister before ending and release objects

    $ScriptEZ->AppUnregister();

    $ScriptEZ    = null;

    $ScriptEZCon = null;

    $sink        = null;

    $sink2       = null;

}

 

// start now program here !

Main();

 

?>