Named Pipe 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:
' . Named Pipe 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 Named Pipe Server broadcasting random data packet 2.5 times/second
'//////////////////////////////////////////////////////////////////////////////
const SCRIPTEZ_API = "ScriptEZ.API" ' Component name
Dim ScriptEZ
Dim ScriptEZConsole
Dim hNamedPipeSvr
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 NamedPipe object
hNamedPipeSvr =
ScriptEZ.CreatePoolNamedPipeServerObject(12346,100,8)
If hNamedPipeSvr > 0 Then
' 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 NamedPipe object
' ( server side )
'///////////////////////////
Function ScriptEZ_onConnectionRequestNamedPipe(hNamedPipeClientObjectProxy)
ScriptEZ_onConnectionRequestNamedPipe =
CLng(ScriptEZ.AddNamedPipeClientObjectEventHandlers(hNamedPipeClientObjectProxy))
' delegating to
the thread pool....
r = ScriptEZ.AddTask(
hThreadPool,hNamedPipeClientObjectProxy )
End Function
' ( client/proxy side)
'/////////////////////
Function ScriptEZ_onConnectionStateNamedPipe(hOwnerNamedPipe,state_desc,error_number)
ScriptEZ.Echo state_desc
ScriptEZ_onConnectionStateNamedPipe
= CLng(0)
End Function
' ( client/proxy side)
'/////////////////////
Function ScriptEZ_onDataNamedPipe(hOwnerNamedPipe,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_onDataNamedPipe =
CLng(0)
End Function
' Callbacks for thread pool object (TaskQueueObject)
'//////////////////////////////////////////////////
Function ScriptEZ_onRun( hNamedPipeClientProxy )
' 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.SendNamedPipeObjectData( hNamedPipeClientProxy,TestData, Len(TestData)
)
If onExit = 3 Then
ScriptEZ_onRun = CLng(0)
Else
ScriptEZ_onRun = CLng(1)
End If
End Function
' Notification on Task aborting
'//////////////////////////////
Sub ScriptEZ_onAbort( hNamedPipeClientProxy )
End Sub
Function ScriptEZConsole_onRun( localStorage )
Dim zClient
ScriptEZ.SetConsoleTitle
CStr(StartTime) + " (ScriptEZ.API) - NamedPipe 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.GetActiveNamedPipeClientObjectNumber(hNamedPipeSvr))+"
Received: " + _
CStr(Round(TotalRecv/1024.0,2))+" KBytes"
Do While 1
hNamedPipeClientProxy =
ScriptEZ.EnumActiveNamedPipeClientObject(hNamedPipeSvr)
If hNamedPipeClientProxy
<= 0 Then
Exit Do
End If
Loop
If
ScriptEZ.GetActiveNamedPipeClientObjectNumber(hNamedPipeSvr) = 0 Then TotalRecv
= 0
End If
If onExit = 3 Then
ScriptEZConsole_onRun = CLng(0)
Else
ScriptEZConsole_onRun = CLng(1)
End If
End Function