-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Socket.ahk
249 lines (226 loc) · 6.55 KB
/
Socket.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
class Socket
{
static WM_SOCKET := 0x9987, MSG_PEEK := 2
static FD_READ := 1, FD_ACCEPT := 8, FD_CLOSE := 32
static Blocking := True, BlockSleep := 50
__New(Socket:=-1)
{
static Init
if (!Init)
{
DllCall("LoadLibrary", "Str", "Ws2_32", "Ptr")
VarSetCapacity(WSAData, 394+A_PtrSize)
if (Error := DllCall("Ws2_32\WSAStartup", "UShort", 0x0202, "Ptr", &WSAData))
throw Exception("Error starting Winsock",, Error)
if (NumGet(WSAData, 2, "UShort") != 0x0202)
throw Exception("Winsock version 2.2 not available")
Init := True
}
this.Socket := Socket
}
__Delete()
{
if (this.Socket != -1)
this.Disconnect()
}
Connect(Address)
{
if (this.Socket != -1)
throw Exception("Socket already connected")
Next := pAddrInfo := this.GetAddrInfo(Address)
while Next
{
ai_addrlen := NumGet(Next+0, 16, "UPtr")
ai_addr := NumGet(Next+0, 16+(2*A_PtrSize), "Ptr")
if ((this.Socket := DllCall("Ws2_32\socket", "Int", NumGet(Next+0, 4, "Int")
, "Int", this.SocketType, "Int", this.ProtocolId, "UInt")) != -1)
{
if (DllCall("Ws2_32\WSAConnect", "UInt", this.Socket, "Ptr", ai_addr
, "UInt", ai_addrlen, "Ptr", 0, "Ptr", 0, "Ptr", 0, "Ptr", 0, "Int") == 0)
{
DllCall("Ws2_32\freeaddrinfo", "Ptr", pAddrInfo) ; TODO: Error Handling
return this.EventProcRegister(this.FD_READ | this.FD_CLOSE)
}
this.Disconnect()
}
Next := NumGet(Next+0, 16+(3*A_PtrSize), "Ptr")
}
throw Exception("Error connecting")
}
Bind(Address)
{
if (this.Socket != -1)
throw Exception("Socket already connected")
Next := pAddrInfo := this.GetAddrInfo(Address)
while Next
{
ai_addrlen := NumGet(Next+0, 16, "UPtr")
ai_addr := NumGet(Next+0, 16+(2*A_PtrSize), "Ptr")
if ((this.Socket := DllCall("Ws2_32\socket", "Int", NumGet(Next+0, 4, "Int")
, "Int", this.SocketType, "Int", this.ProtocolId, "UInt")) != -1)
{
if (DllCall("Ws2_32\bind", "UInt", this.Socket, "Ptr", ai_addr
, "UInt", ai_addrlen, "Int") == 0)
{
DllCall("Ws2_32\freeaddrinfo", "Ptr", pAddrInfo) ; TODO: ERROR HANDLING
return this.EventProcRegister(this.FD_READ | this.FD_ACCEPT | this.FD_CLOSE)
}
this.Disconnect()
}
Next := NumGet(Next+0, 16+(3*A_PtrSize), "Ptr")
}
throw Exception("Error binding")
}
Listen(backlog=32)
{
return DllCall("Ws2_32\listen", "UInt", this.Socket, "Int", backlog) == 0
}
Accept()
{
if ((s := DllCall("Ws2_32\accept", "UInt", this.Socket, "Ptr", 0, "Ptr", 0, "Ptr")) == -1)
throw Exception("Error calling accept",, this.GetLastError())
Sock := new Socket(s)
Sock.ProtocolId := this.ProtocolId
Sock.SocketType := this.SocketType
Sock.EventProcRegister(this.FD_READ | this.FD_CLOSE)
return Sock
}
Disconnect()
{
; Return 0 if not connected
if (this.Socket == -1)
return 0
; Unregister the socket event handler and close the socket
this.EventProcUnregister()
if (DllCall("Ws2_32\closesocket", "UInt", this.Socket, "Int") == -1)
throw Exception("Error closing socket",, this.GetLastError())
this.Socket := -1
return 1
}
MsgSize()
{
static FIONREAD := 0x4004667F
if (DllCall("Ws2_32\ioctlsocket", "UInt", this.Socket, "UInt", FIONREAD, "UInt*", argp) == -1)
throw Exception("Error calling ioctlsocket",, this.GetLastError())
return argp
}
Send(pBuffer, BufSize, Flags:=0)
{
if ((r := DllCall("Ws2_32\send", "UInt", this.Socket, "Ptr", pBuffer, "Int", BufSize, "Int", Flags)) == -1)
throw Exception("Error calling send",, this.GetLastError())
return r
}
SendText(Text, Flags:=0, Encoding:="UTF-8")
{
VarSetCapacity(Buffer, StrPut(Text, Encoding) * ((Encoding="UTF-16"||Encoding="cp1200") ? 2 : 1))
Length := StrPut(Text, &Buffer, Encoding)
return this.Send(&Buffer, Length - 1)
}
Recv(ByRef Buffer, BufSize:=0, Flags:=0)
{
while (!(Length := this.MsgSize()) && this.Blocking)
Sleep, this.BlockSleep
if !Length
return 0
if !BufSize
BufSize := Length
VarSetCapacity(Buffer, BufSize)
if ((r := DllCall("Ws2_32\recv", "UInt", this.Socket, "Ptr", &Buffer, "Int", BufSize, "Int", Flags)) == -1)
throw Exception("Error calling recv",, this.GetLastError())
return r
}
RecvText(BufSize:=0, Flags:=0, Encoding:="UTF-8")
{
if (Length := this.Recv(Buffer, BufSize, flags))
return StrGet(&Buffer, Length, Encoding)
return ""
}
RecvLine(BufSize:=0, Flags:=0, Encoding:="UTF-8", KeepEnd:=False)
{
while !(i := InStr(this.RecvText(BufSize, Flags|this.MSG_PEEK, Encoding), "`n"))
{
if !this.Blocking
return ""
Sleep, this.BlockSleep
}
if KeepEnd
return this.RecvText(i, Flags, Encoding)
else
return RTrim(this.RecvText(i, Flags, Encoding), "`r`n")
}
GetAddrInfo(Address)
{
; TODO: Use GetAddrInfoW
Host := Address[1], Port := Address[2]
VarSetCapacity(Hints, 16+(4*A_PtrSize), 0)
NumPut(this.SocketType, Hints, 8, "Int")
NumPut(this.ProtocolId, Hints, 12, "Int")
if (Error := DllCall("Ws2_32\getaddrinfo", "AStr", Host, "AStr", Port, "Ptr", &Hints, "Ptr*", Result))
throw Exception("Error calling GetAddrInfo",, Error)
return Result
}
OnMessage(wParam, lParam, Msg, hWnd)
{
Critical
if (Msg != this.WM_SOCKET || wParam != this.Socket)
return
if (lParam & this.FD_READ)
this.onRecv()
else if (lParam & this.FD_ACCEPT)
this.onAccept()
else if (lParam & this.FD_CLOSE)
this.EventProcUnregister(), this.OnDisconnect()
}
EventProcRegister(lEvent)
{
this.AsyncSelect(lEvent)
if !this.Bound
{
this.Bound := this.OnMessage.Bind(this)
OnMessage(this.WM_SOCKET, this.Bound)
}
}
EventProcUnregister()
{
this.AsyncSelect(0)
if this.Bound
{
OnMessage(this.WM_SOCKET, this.Bound, 0)
this.Bound := False
}
}
AsyncSelect(lEvent)
{
if (DllCall("Ws2_32\WSAAsyncSelect"
, "UInt", this.Socket ; s
, "Ptr", A_ScriptHwnd ; hWnd
, "UInt", this.WM_SOCKET ; wMsg
, "UInt", lEvent) == -1) ; lEvent
throw Exception("Error calling WSAAsyncSelect",, this.GetLastError())
}
GetLastError()
{
return DllCall("Ws2_32\WSAGetLastError")
}
}
class SocketTCP extends Socket
{
static ProtocolId := 6 ; IPPROTO_TCP
static SocketType := 1 ; SOCK_STREAM
}
class SocketUDP extends Socket
{
static ProtocolId := 17 ; IPPROTO_UDP
static SocketType := 2 ; SOCK_DGRAM
SetBroadcast(Enable)
{
static SOL_SOCKET := 0xFFFF, SO_BROADCAST := 0x20
if (DllCall("Ws2_32\setsockopt"
, "UInt", this.Socket ; SOCKET s
, "Int", SOL_SOCKET ; int level
, "Int", SO_BROADCAST ; int optname
, "UInt*", !!Enable ; *char optval
, "Int", 4) == -1) ; int optlen
throw Exception("Error calling setsockopt",, this.GetLastError())
}
}