Named Pipe Client.
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 NamedPipe Client receiving random packets and sending back at
' every eight
received packets
'//////////////////////////////////////////////////////////////////////////////
const SCRIPTEZ_API = "ScriptEZ.API" '
Component name
Dim ScriptEZMain
Dim hNamedPipeClient
Dim ReplyCount
Dim StartTime
Dim ReadyToSend
Dim TotalRecv
Dim TotalSend
Dim LastRecvTime
Dim Err_num
Dim hThreadPool
Dim onExit
Dim InputString
Dim showRecvData
Dim Resend
Dim ScrollOutput
Dim PendingTasks
Dim bstrData
' start the script
'/////////////////
Main()
' Main
Subroutine
'////////////////
Sub
Dim HostAndPort
ScrollOutput = 1
Resend = 0
showRecvData
= 0
onExit = 0
ReplyCount = 0
ReadyToSend= 0
TotalRecv = 0
TotalSend = 0
StartTime = Now()
LastRecvTime = Now()
'
instantiate component object with callback interface
Set ScriptEZMain = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZMain_")
'ScriptEZMain.SetCPUCoreAffinity
"0"
' create client NamedPipe object + xtra encryption
hNamedPipeClient
= ScriptEZMain.CreateNamedPipeClientObject
'ScriptEZMain.EnableNamedPipeObjectDataEncryption hNamedPipeClient,true,-1
' connect to server
InputString = InputBox("Enter
your hostname and named pipe, such like localhost:12346", _
"Choose a hostname", _
"localhost:12346")
HostAndPort =
Err_num =
ScriptEZMain.ConnectNamedPipeServerObject(HostAndPort(0), HostAndPort(1),
hNamedPipeClient)
If Err_num <> 0 Then Exit
Sub
' Register App for receiving AppEvent(s) with
onAppEvent(EventID)
ScriptEZMain.AppRegister
' creating thread pool with 4 threads for polling info
hThreadPool
= ScriptEZMain.CreateTaskQueueObject(4)
r = ScriptEZMain.AddTask(
hThreadPool, hNamedPipeClient )
' block until NamedPipe break or exit event
Do Until Err_num <> 0 Or onExit = 3
WScript.Sleep 1000
Loop
' empty queued msg for sending
r = ScriptEZMain.SendNamedPipeObjectData(hNamedPipeClient,vbNullString,0)
' App unregister before ending
ScriptEZMain.AppUnregister
WScript.Sleep 4000
Set ScriptEZMain = Nothing
End Sub
Function DataHandler()
Dim data_size
data_size = Len(bstrData)
If showRecvData = 1 Then
ScriptEZMain.Echo CStr(Now) + " size(" + CStr(data_size) +
"): " + CStr(bstrData)
If Resend = 1 And
data_size > 0 Then
Err_num = ScriptEZMain.SendNamedPipeObjectData(hNamedPipeClient,bstrData,data_size)
TotalSend = TotalSend +
data_size
ReplyCount = ReplyCount + 1
Else
TotalSend = 0
End If
DataHandler = 0
End Function
' Callbacks for
App Events notification and fetching help text
'/////////////////////////////////////////////////////////////
Sub ScriptEZMain_onAppEvent(EventID)
ScriptEZMain.Echo " ->
EventID:" + CStr(EventID)
Select Case EventID
Case 3 onExit= 3
Case 0 showRecvData = 0
Case 1 showRecvData = 1
Case 2 Resend = 0
Case 4 Resend = 1
Case 5 ScrollOutput = 0
Case 6 ScrollOutput = 1
End Select
End Sub
Function ScriptEZMain_AppEventHelp()
ScriptEZMain_AppEventHelp = vbLF +_
CStr("Filename:
" + Chr(34) + CStr(WScript.ScriptFullName) + Chr(34) + vbLF +_
" **** Supported AppEvents ****" +
vbLF +_
"AppEventID = 0"
+ vbTab + "to hide received data (default)" + vbLF +_
"AppEventID = 2"
+ vbTab + "to not resend back data (default)" + vbLF +_
"AppEventID = 6"
+ vbTab + "to enable scrolling (default)" + vbLF +_
"AppEventID = 1"
+ vbTab + "to display received data" + vbLF +_
"AppEventID = 4"
+ vbTab + "to start resending back data" + vbLF + _
"AppEventID = 5"
+ vbTab + "to disable scrolling" + vbLF +_
"AppEventID = 3"
+ vbTab + "to exit" + vbLF _
)
End Function
' Callbacks for
NamedPipe object
' ( client/proxy
side)
'///////////////////////////
Function ScriptEZMain_onConnectionStateNamedPipe(hOwnerNamedPipe,state_desc,error_number)
ScriptEZMain.Echo state_desc
Err_num = error_number
ScriptEZMain_onConnectionStateNamedPipe = CLng(0)
End Function
' ( client/proxy
side)
'//////////////////////////
Function ScriptEZMain_onDataNamedPipe(hOwnerNamedPipe,raw_data,data_size)
' converting string to BSTR
bstrData = ScriptEZMain.ConvertStringToBSTR(raw_data)
LastRecvTime = Now()
TotalRecv = TotalRecv +
data_size
If showRecvData = 0 Then
ScriptEZMain.Echo " ThreadID:" + CStr(ScriptEZMain.GetCurrentThreadId)
+ vbTab + _
" Data size..:" + CStr(data_size)
ReadyToSend = ReadyToSend + 1
If ReadyToSend = 8 Then 'sending
back every 8 received packets
' Handle this packet of data
r = DataHandler()
ReadyToSend = 0
End If
ScriptEZMain.FreeBSTR bstrData
ScriptEZMain.FreeString raw_data
ScriptEZMain_onDataNamedPipe = CLng(0)
End Function
' Notification on
Task aborting
'//////////////////////////////
Sub ScriptEZMain_onAbort( lpString )
End Sub
' Callback for thread pool
object (TaskQueueObject)
'//////////////////////////////////////////////////
Function ScriptEZMain_onRun( localStorage )
If Err_num <> 0 Or onExit = 3 Then
ScriptEZMain_onRun = CLng(0) ' stop this task
Exit Function
Else
ScriptEZMain_onRun = CLng(1) ' run again this task
End If
WScript.Sleep 1000
Dim hNamedPipe
Dim Total
hNamedPipe
= localStorage
ScriptEZMain.SetConsoleTitle CStr(StartTime) + " (ScriptEZ.API) - NamedPipe Client
sentback(" + _
CStr(ReplyCount) +")/" + InputString
ScriptEZMain.PeekNamedPipeClientObjectStatsOnReceivedData hNamedPipe
PendingTasks = ScriptEZMain.GetPendingTaskCount(hThreadPool)
If ScrollOutput = 0 Then
ScriptEZMain.ClearConsole
Total =
ScriptEZMain.NamedPipeSplittedPackets + _
ScriptEZMain.NamedPipeOverlappedPackets + _
ScriptEZMain.NamedPipeSimplePackets + _
ScriptEZMain.NamedPipeWrongPackets
If Total > 0 Then
ScriptEZMain.Echo _
" ." + CStr(Now) + vbLF+ _
"
Pending..........:" + CStr(PendingTasks) + vbLF + _
vbLF
+ " ." + CStr(LastRecvTime) + vbLF + _
"
Splitted.........:" + CStr(ScriptEZMain.NamedPipeSplittedPackets) +
_
" ("+CStr(Round((ScriptEZMain.NamedPipeSplittedPackets/Total)*100.0,2))+"%)"+vbLF+_
"
Overlapped.......:" + CStr(ScriptEZMain.NamedPipeOverlappedPackets)
+ _
" ("+CStr(Round((ScriptEZMain.NamedPipeOverlappedPackets/Total)*100.0,2))+"%)"+vbLF+_
"
Simple...........:" + CStr(ScriptEZMain.NamedPipeSimplePackets) + _
" ("+CStr(Round((ScriptEZMain.NamedPipeSimplePackets/Total)*100.0,2))+"%)"+vbLF+_
" Wrong............:"
+ CStr(ScriptEZMain.NamedPipeWrongPackets) + vbLF +_
"
PooledThreadId...:" + CStr(ScriptEZMain.GetCurrentPooledThreadId) +
vbLF
End If
End Function