Code:
; -------------------------------------------------
;-----------CLIENTSCRIPT----------------------
; -------------------------------------------------
; CONFIGURATION SECTION:
; Specify address and port of the server.
Network_Address = 127.0.0.1
Network_Port = 8765
windowTitle=Warcraft
; ----------------------------
; END OF CONFIGURATION SECTION
; ----------------------------
KEY_0=0
KEY_1=1
KEY_2=2
KEY_3=3
KEY_4=4
KEY_5=5
KEY_6=6
KEY_7=7
KEY_8=8
KEY_9=9
KEY_A=10
KEY_B=11
KEY_C=12
KEY_D=13
KEY_E=14
KEY_F=15
KEY_G=16
KEY_H=17
KEY_I=18
KEY_J=19
KEY_K=20
KEY_L=21
KEY_M=22
KEY_N=23
KEY_O=24
KEY_P=25
KEY_Q=26
KEY_R=27
KEY_S=28
KEY_T=29
KEY_U=30
KEY_V=31
KEY_W=32
KEY_X=33
KEY_Y=34
KEY_Z=35
SetTitleMatchMode 2 ; match anywhere in the title
WinGet, targetWindowIds, list, %windowTitle%
Gui, Add, Button, gConnection_Init, Connect
Gui, Add, Edit, w150 vServerIp
;Gui, Add, Button, gClear, Clear
Gui, Add, Text,, Key reciever client (transfers keystrokes from the server to the client(s)):
Gui, Show
LinesReceived:=0
return
Connection_Init:
OnExit, ExitSub ; For connection cleanup purposes.
; Connect to any type of server:
Gui, Submit, NoHide
;socket := ConnectToAddress(Network_Address, Network_Port)
socket := ConnectToAddress(ServerIp, Network_Port)
if socket = -1 ; Connection failed (it already displayed the reason).
ExitApp
; Find this script's main window:
Process, Exist ; This sets ErrorLevel to this script's PID (it's done this way to support compiled scripts).
DetectHiddenWindows On
ScriptMainWindowId := WinExist("ahk_class AutoHotkey ahk_pid " . ErrorLevel)
DetectHiddenWindows Off
; When the OS notifies the script that there is incoming data waiting to be received,
; the following causes a function to be launched to read the data:
NotificationMsg = 0x5555 ; An arbitrary message number, but should be greater than 0x1000.
OnMessage(NotificationMsg, "ReceiveData")
; Set up the connection to notify this script via message whenever new data has arrived.
; This avoids the need to poll the connection and thus cuts down on resource usage.
FD_READ = 1 ; Received when data is available to be read.
FD_CLOSE = 32 ; Received when connection has been closed.
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int", FD_READ|FD_CLOSE)
{
MsgBox % "WSAAsyncSelect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
ExitApp
}
return
;Clear:
;ShowReceived=
;LinesReceived:=0
;GuiControl,, MyEdit,
;return
ConnectToAddress(IPAddress, Port)
; Returns -1 (INVALID_SOCKET) upon failure or the socket ID upon success.
{
VarSetCapacity(wsaData, 32) ; The struct is only about 14 in size, so 32 is conservative.
result := DllCall("Ws2_32\WSAStartup", "UShort", 0x0002, "UInt", &wsaData) ; Request Winsock 2.0 (0x0002)
; Since WSAStartup() will likely be the first Winsock function called by this script,
; check ErrorLevel to see if the OS has Winsock 2.0 available:
if ErrorLevel
{
MsgBox WSAStartup() could not be called due to error %ErrorLevel%. Winsock 2.0 or higher is required.
return -1
}
if result ; Non-zero, which means it failed (most Winsock functions return 0 upon success).
{
MsgBox % "WSAStartup() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
return -1
}
AF_INET = 2
SOCK_STREAM = 1
IPPROTO_TCP = 6
socket := DllCall("Ws2_32\socket", "Int", AF_INET, "Int", SOCK_STREAM, "Int", IPPROTO_TCP)
if socket = -1
{
MsgBox % "socket() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
return -1
}
; Prepare for connection:
SizeOfSocketAddress = 16
VarSetCapacity(SocketAddress, SizeOfSocketAddress)
InsertInteger(2, SocketAddress, 0, AF_INET) ; sin_family
InsertInteger(DllCall("Ws2_32\htons", "UShort", Port), SocketAddress, 2, 2) ; sin_port
InsertInteger(DllCall("Ws2_32\inet_addr", "Str", IPAddress), SocketAddress, 4, 4) ; sin_addr.s_addr
; Attempt connection:
if DllCall("Ws2_32\connect", "UInt", socket, "UInt", &SocketAddress, "Int", SizeOfSocketAddress)
{
MsgBox % "connect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError") . "?"
return -1
}
return socket ; Indicate success by returning a valid socket ID rather than -1.
}
ReceiveData(wParam, lParam)
; By means of OnMessage(), this function has been set up to be called automatically whenever new data
; arrives on the connection.
{
Critical
global ShowReceived
global MyEdit
global LinesReceived
socket := wParam
ReceivedDataSize = 4096 ; Large in case a lot of data gets buffered due to delay in processing previous data.
VarSetCapacity(ReceivedData, ReceivedDataSize, 0) ; 0 for last param terminates string for use with recv().
Data := ""
Loop ; This loop solves the issue of the notification message being discarded due to thread-already-running.
{
ReceivedDataLength := DllCall("Ws2_32\recv", "UInt", socket, "Str", ReceivedData, "Int", ReceivedDataSize, "Int", 0)
if ReceivedDataLength = 0 ; The connection was gracefully closed,
ExitApp ; The OnExit routine will call WSACleanup() for us.
if ReceivedDataLength = -1
{
WinsockError := DllCall("Ws2_32\WSAGetLastError")
if ( WinsockError = 10035 ) { ; WSAEWOULDBLOCK, which means "no more data to be read".
break
}
if WinsockError <> 10054 ; WSAECONNRESET, which happens when Network closes via system shutdown/logoff.
; Since it's an unexpected error, report it. Also exit to avoid infinite loop.
MsgBox % "recv() indicated Winsock error " . WinsockError
ExitApp ; The OnExit routine will call WSACleanup() for us.
}
Data .= ReceivedData
}
; Otherwise, process the data received.
Loop, parse, Data, `n, `r
{
;LinesReceived++
;if (LinesReceived = 1) {
; ShowReceived = %LinesReceived%: %A_LoopField%
;} else {
; ShowReceived = %ShowReceived%`n%LinesReceived%: %A_LoopField%
;}
;Tooltip % ShowReceived
;GuiControl,, MyEdit, %ShowReceived%
PressKey(A_LoopField)
}
return 1 ; Tell the program that no further processing of this message is needed.
}
InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
; The caller must ensure that pDest has sufficient capacity. To preserve any existing contents in pDest,
; only pSize number of bytes starting at pOffset are altered in it.
{
Loop %pSize% ; Copy each byte in the integer into the structure as raw binary data.
DllCall("RtlFillMemory", "UInt", &pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF)
}
;Send specified key to all instances of the configured program.
PressKey(key)
{
global targetWindowIds
Loop, %targetWindowIds%
{
target_window := targetWindowIds%A_index%
InternalPressKey(key, target_window)
}
}
InternalPressKey(key, target_window)
{
global
Connect With Us