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:

 

 

'//////////////////////////////////////////////////////////////////////////////

' 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

'   instead of WScript.Echo() with 2 behaviours for cscript.exe or wscript.exe

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

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

'

' Purpose: Simple TCP/IP Server broadcasting random data packet 2.5 times/second

'//////////////////////////////////////////////////////////////////////////////

 

const SCRIPTEZ_API = "ScriptEZ.API" ' Component name

 

Dim ScriptEZ

Dim ScriptEZConsole

Dim hSocketSvr

Dim StartTime

Dim TotalRecv

Dim hThreadPool

Dim hThreadPoolConsole

Dim onExit

Dim showRecvData

Dim enumerateClient

' start the script

'////////////////

'On Error Resume Next

Main()

 

' Main Subroutine

'////////////////

Sub Main()

  enumerateClient= 0

  showRecvData = 0

  onExit       = 0

  TotalRecv    = 0

  StartTime    = Now()

  ' instantiate component object with callback interface

  Set ScriptEZ = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZ_")

  Set ScriptEZConsole = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZConsole_")

  'ScriptEZ.SetCPUCoreAffinity "0"

 

  ' Register App for receiving AppEvent(s) with onAppEvent(EventID)

  ScriptEZ.AppRegister

  ScriptEZConsole.AppRegister

 

  ' Hide console window if 0

  ScriptEZ.ShowConsoleWindow 1

 

  ' create server socket object +  extra  encryption

  hSocketSvr = ScriptEZ.CreatePoolSocketServerObject(12346,vbNullString,100,8,0,0)

 

  If hSocketSvr > 0 Then

    ScriptEZ.EnableSocketObjectDataEncryption hSocketSvr,true,-1

 

    ' creating thread pool with 4 threads

    hThreadPool = ScriptEZ.CreateTaskQueueObject(4)

   

    hThreadPoolConsole = ScriptEZConsole.CreateTaskQueueObject(1)

    r = ScriptEZConsole.AddTask( hThreadPoolConsole,0)

 

    ' block until AppEventID = 3 is received

    Do Until onExit = 3

       WScript.Sleep 200

    Loop

  End If

 

  ScriptEZ.AppUnregister

  ScriptEZConsole.AppUnregister

  WScript.Sleep 4000

  Set ScriptEZ = Nothing

  Set ScriptEZConsole = Nothing

End Sub

 

' Callbacks for App Events notification and fetching help text

'/////////////////////////////////////////////////////////////

Sub ScriptEZ_onAppEvent(EventID)

 

  If EventID = 3 Then onExit = EventID

  

  If EventID = 0 Then showRecvData = EventID

  If EventID = 1 Then showRecvData = EventID

  If EventID = 4 Then enumerateClient = 1

  If EventID = 5 Then enumerateClient = 0

 

End Sub

 

Function ScriptEZ_AppEventHelp()

 

 ScriptEZ_AppEventHelp = _

  CStr("  **** Supported AppEvents ****" + vbLF + _

       "AppEventID = 0" + vbTab + "to hide received data (default)" + vbLF + _

       "AppEventID = 1" + vbTab + "to display received data" + vbLF + _

       "AppEventID = 3" + vbTab + "to exit" + vbLF + _

       "AppEventID = 4" + vbTab + "to enumerate connected clients" + vbLF + _

       "AppEventID = 5" + vbTab + "not to enumerate connected clients (default)" + vbLF _

       )

End Function

 

 

' Callbacks for socket object

' ( server side )

'///////////////////////////

Function ScriptEZ_onConnectionRequest(hSocketClientObjectProxy)

 

 ScriptEZ_onConnectionRequest = CLng(ScriptEZ.AddSocketClientObjectEventHandlers(hSocketClientObjectProxy))

 ' delegating to the thread pool....

 r = ScriptEZ.AddTask( hThreadPool,hSocketClientObjectProxy )

 r = ScriptEZ.EnableSocketObjectInOutBoundBandwidthPolling(hSocketClientObjectProxy)

 

End Function

 

' ( client/proxy side)

'/////////////////////

Function ScriptEZ_onConnectionState(hOwnerSocket,state_desc,error_number)

 

 ScriptEZ.Echo state_desc

 ScriptEZ_onConnectionState = CLng(0)

 

End Function

 

' ( client/proxy side)

'/////////////////////

Function ScriptEZ_onData(hOwnerSocket,raw_data,data_size)

 

 Dim bstrData

 TotalRecv = TotalRecv + data_size

 If showRecvData = 1 Then

   bstrData = ScriptEZ.ConvertStringToBSTR(raw_data)

   ScriptEZ.Echo CStr(Now)  + " size(" + CStr(data_size) + "): " + CStr(bstrData)

   ScriptEZ.FreeBSTR bstrData

 End If

 ScriptEZ.FreeString raw_data

 ScriptEZ_onData = CLng(0)

 

End Function

 

' Callbacks for thread pool object (TaskQueueObject)

'//////////////////////////////////////////////////

Function ScriptEZ_onRun( hSocketClientProxy )

 

  ' Send random data 2.5 times per second

  WScript.Sleep 400

  r = Rnd()

  if( r = 0 ) then r = 1

  TestData = "BoM" + String(r*1536,"x") + "EoM" + vbLF + Chr(0)

  r = ScriptEZ.SendSocketObjectData( hSocketClientProxy,TestData, Len(TestData) )

  If onExit = 3 Then

    ScriptEZ_onRun = CLng(0)

  Else

    If r <>  0 Then

      ScriptEZ_onRun = CLng(0)

    Else

      ScriptEZ_onRun = CLng(1)

    End If

  End If

 

End Function

 

' Notification on Task aborting

'//////////////////////////////

Sub ScriptEZ_onAbort( hSocketClientProxy )

End Sub

 

Function ScriptEZConsole_onRun( localStorage )

 

 Dim zClient

 ScriptEZ.SetConsoleTitle CStr(StartTime) + " (ScriptEZ.API) - TCP/IP Server@12346"

 WScript.Sleep 1000

 ScriptEZ.ClearConsole

 If enumerateClient = 0 Then ScriptEZ.Echo  vbLF + " Received: " + _

                                            CStr(Round(TotalRecv/1024.0,2)) + " KBytes"

 If enumerateClient = 1 Then

   ScriptEZ.Echo  vbLF + " Clients:" + _

                 CStr(ScriptEZ.GetActiveSocketClientObjectNumber(hSocketSvr))+" Received: " + _

                 CStr(Round(TotalRecv/1024.0,2))+" KBytes"

   Do While 1

    hSocketClientProxy = ScriptEZ.EnumActiveSocketClientObject(hSocketSvr)

    If hSocketClientProxy <= 0 Then

     Exit Do

    End If

    zClient = ScriptEZ.GetSocketConnectedClientName(hSocketClientProxy)

    ScriptEZ.Echo  _

          CStr(zClient) + _

          " Inbound:" + CStr(ScriptEZ.SocketObjectPendingInboundMessageCount(hSocketClientProxy)) + _

          " Outbound:" + CStr(ScriptEZ.SocketObjectPendingOutboundMessageCount(hSocketClientProxy))

    ScriptEZ.FreeBSTR zClient

   Loop

   If ScriptEZ.GetActiveSocketClientObjectNumber(hSocketSvr) = 0 Then TotalRecv = 0

 End If

 

 If onExit = 3 Then

   ScriptEZConsole_onRun = CLng(0)

 Else

   ScriptEZConsole_onRun = CLng(1)

 End If

 

End Function