TCP UDF, Event driven!


SUBMITTED BY: Guest

DATE: May 28, 2013, 5:35 p.m.

FORMAT: Text only

SIZE: 24.9 kB

HITS: 903

  1. #cs ----------------------------------------------------------------------------
  2. AutoIt Version: 3.3.0.0
  3. Author: Kip
  4. Script Function:
  5. TCP UDF v3
  6. #ce ----------------------------------------------------------------------------
  7. ; Script Start - Add your code below here
  8. #cs
  9. Functions:
  10. _TCP_Server_Create($iPort, $sIP="0.0.0.0")
  11. _TCP_Server_Broadcast($sData)
  12. _TCP_Server_ClientList()
  13. _TCP_Server_ClientIP($hSocket)
  14. _TCP_Server_DisconnectClient($hSocket)
  15. _TCP_Server_Stop()
  16. _TCP_Client_Create($sIP , $iPort)
  17. _TCP_Client_Stop($hSocket)
  18. _TCP_Send($hSocket, $sText)
  19. _TCP_RegisterEvent($hSocket, $iEvent, $sFunction)
  20. Register event values:
  21. $TCP_SEND ; Function ($hSocket, $iError)
  22. $TCP_RECEIVE ; Function ($hSocket, $sReceived, $iError)
  23. $TCP_CONNECT ; Function ($hSocket, $iError) => Client only
  24. $TCP_DISCONNECT ; Function ($hSocket, $iError)
  25. $TCP_NEWCLIENT ; Function ($hSocket, $iError) => Server only
  26. #ce
  27. Global Const $FD_READ = 1
  28. Global Const $FD_WRITE = 2
  29. Global Const $FD_OOB = 4
  30. Global Const $FD_ACCEPT = 8
  31. Global Const $FD_CONNECT = 16
  32. Global Const $FD_CLOSE = 32
  33. Global $hWs2_32 = -1
  34. Global Const $TCP_SEND = 1
  35. Global Const $TCP_RECEIVE = 2
  36. Global Const $TCP_CONNECT = 4
  37. Global Const $TCP_DISCONNECT = 8
  38. Global Const $TCP_NEWCLIENT = 16
  39. TCPStartup()
  40. Global Const $__TCP_WINDOW = GUICreate("Async Sockets UDF")
  41. Global $__TCP_SOCKETS[1][7]
  42. ; #FUNCTION# ;===============================================================================
  43. ;
  44. ; Name...........: _TCP_Server_Create
  45. ; Description ...: Initializes the server.
  46. ; Syntax.........: _TCP_Server_Create($iPort, $sIP="0.0.0.0")
  47. ; Parameters ....: $iPort - The port number the server should listen to.
  48. ; $sIP - IP address. (Default = "0.0.0.0")
  49. ; Return values .: The socket handle.
  50. ; Author ........: Kip
  51. ; Modified.......:
  52. ; Remarks .......: Only 1 server can be created per script.
  53. ; Related .......:
  54. ; Link ..........;
  55. ; Example .......;
  56. ;
  57. ; ;==========================================================================================
  58. Func _TCP_Server_Create($iPort, $sIP="0.0.0.0")
  59. Local $hListenSocket = ___ASocket()
  60. ___ASockSelect( $hListenSocket, $__TCP_WINDOW, 0x0400, $FD_ACCEPT)
  61. GUIRegisterMsg( 0x0400, "___TCP_OnAccept" )
  62. ___ASockListen( $hListenSocket, $sIP, $iPort )
  63. $__TCP_SOCKETS[0][0] = $hListenSocket
  64. $__TCP_SOCKETS[0][1] = 0x0400
  65. Return $hListenSocket
  66. EndFunc
  67. Func ___TCP_OnAccept($hWnd, $iMsgID, $WParam, $LParam)
  68. Local $hSocket = $WParam
  69. Local $iError = ___HiWord( $LParam )
  70. Local $iEvent = ___LoWord( $LParam )
  71. Local $hClient, $uBound
  72. Abs($hWnd) ; Stupid AU3Check...
  73. If $iMsgID = $__TCP_SOCKETS[0][1] Then
  74. If $iEvent = $FD_ACCEPT Then
  75. If Not $iError Then
  76. ReDim $__TCP_SOCKETS[UBound($__TCP_SOCKETS)+1][7]
  77. $uBound = UBound($__TCP_SOCKETS)
  78. $hClient = TCPAccept($hSocket)
  79. ___ASockSelect($hClient, $__TCP_WINDOW, 0x0400 + $uBound - 1, BitOR($FD_READ, $FD_WRITE, $FD_CLOSE))
  80. GUIRegisterMsg(0x0400 + $uBound - 1, "___TCP_Server_OnSocketEvent" )
  81. $__TCP_SOCKETS[UBound($__TCP_SOCKETS)-1][0] = $hClient
  82. $__TCP_SOCKETS[UBound($__TCP_SOCKETS)-1][1] = 0x0400 + $uBound - 1
  83. Call($__TCP_SOCKETS[0][6], $hClient, $iError)
  84. Else
  85. Call($__TCP_SOCKETS[0][6], 0, $iError)
  86. EndIf
  87. ElseIf $iEvent = $FD_CONNECT Then
  88. Call($__TCP_SOCKETS[0][4], $hSocket, $iError)
  89. EndIf
  90. EndIf
  91. EndFunc
  92. ; #FUNCTION# ;===============================================================================
  93. ;
  94. ; Name...........: _TCP_Client_Stop
  95. ; Description ...: Stops the client.
  96. ; Syntax.........: _TCP_Client_Stop($hSocket)
  97. ; Parameters ....: $hSocket - Client socket.
  98. ; Return values .: Success - True
  99. ; Failure - False
  100. ; Author ........: Kip
  101. ; Modified.......:
  102. ; Remarks .......: The client socket is the return value of _TCP_Client_Create().
  103. ; Related .......:
  104. ; Link ..........;
  105. ; Example .......;
  106. ;
  107. ; ;==========================================================================================
  108. Func _TCP_Client_Stop($hSocket)
  109. Local $iElement, $i
  110. $iElement = 0
  111. For $i = 1 to UBound($__TCP_SOCKETS)-1
  112. If $__TCP_SOCKETS[$i][0] = $hSocket Then
  113. $iElement = $i
  114. ExitLoop
  115. EndIf
  116. Next
  117. If $iElement Then
  118. ___ASockShutdown($__TCP_SOCKETS[$iElement][0])
  119. TCPCloseSocket($__TCP_SOCKETS[$iElement][0])
  120. ___ArrayDelete($__TCP_SOCKETS, $iElement)
  121. Return True
  122. EndIf
  123. Return False
  124. EndFunc
  125. ; #FUNCTION# ;===============================================================================
  126. ;
  127. ; Name...........: _TCP_Server_Stop
  128. ; Description ...: Stops the server, and closes all client connections.
  129. ; Syntax.........: _TCP_Server_Stop()
  130. ; Parameters ....:
  131. ; Return values .: True
  132. ; Author ........: Kip
  133. ; Modified.......:
  134. ; Remarks .......:
  135. ; Related .......:
  136. ; Link ..........;
  137. ; Example .......;
  138. ;
  139. ; ;==========================================================================================
  140. Func _TCP_Server_Stop()
  141. Local $i
  142. ___ASockShutdown($__TCP_SOCKETS[0][0])
  143. TCPCloseSocket($__TCP_SOCKETS[0][0])
  144. $__TCP_SOCKETS[0][0] = ""
  145. $__TCP_SOCKETS[0][1] = ""
  146. $__TCP_SOCKETS[0][2] = ""
  147. $__TCP_SOCKETS[0][3] = ""
  148. $__TCP_SOCKETS[0][4] = ""
  149. $__TCP_SOCKETS[0][5] = ""
  150. $__TCP_SOCKETS[0][6] = ""
  151. For $i = UBound($__TCP_SOCKETS)-1 to 1 Step -1
  152. ___ArrayDelete($__TCP_SOCKETS, $i)
  153. Next
  154. Return True
  155. EndFunc
  156. Func ___TCP_Server_OnSocketEvent( $hWnd, $iMsgID, $WParam, $LParam )
  157. Local $hSocket = $WParam
  158. Local $iError = ___HiWord( $LParam )
  159. Local $iEvent = ___LoWord( $LParam )
  160. Local $sDataBuff, $iElement, $i
  161. Abs($hWnd)
  162. $hSocket = 0
  163. $iElement = 0
  164. For $i = 1 to UBound($__TCP_SOCKETS)-1
  165. If $__TCP_SOCKETS[$i][1] = $iMsgID Then
  166. $hSocket = $__TCP_SOCKETS[$i][0]
  167. $iElement = $i
  168. ExitLoop
  169. EndIf
  170. Next
  171. If $hSocket Then
  172. Switch $iEvent
  173. Case $FD_READ
  174. $sDataBuff = TCPRecv($hSocket, 1024)
  175. Call($__TCP_SOCKETS[0][2], $hSocket, $sDataBuff, $iError)
  176. Case $FD_WRITE
  177. Call($__TCP_SOCKETS[0][3], $hSocket, $iError)
  178. Case $FD_CLOSE
  179. ___ASockShutdown($hSocket)
  180. TCPCloseSocket($hSocket)
  181. Call($__TCP_SOCKETS[0][5], $hSocket, $iError)
  182. ___ArrayDelete($__TCP_SOCKETS, $iElement)
  183. EndSwitch
  184. EndIf
  185. EndFunc
  186. ; #FUNCTION# ;===============================================================================
  187. ;
  188. ; Name...........: _TCP_Server_DisconnectClient
  189. ; Description ...: Disconnects a client of the server.
  190. ; Syntax.........: _TCP_Server_DisconnectClient($hSocket)
  191. ; Parameters ....: $hSocket - Client socket.
  192. ; Return values .: Success - True
  193. ; Failure - False
  194. ; Author ........: Kip
  195. ; Modified.......:
  196. ; Remarks .......: The client socket is the $hSocket parameter of a _TCP_RegisterEvent callback function.
  197. ; Related .......:
  198. ; Link ..........;
  199. ; Example .......;
  200. ;
  201. ; ;==========================================================================================
  202. Func _TCP_Server_DisconnectClient($hSocket)
  203. Local $iElement, $i
  204. $iElement = 0
  205. For $i = 1 to UBound($__TCP_SOCKETS)-1
  206. If $__TCP_SOCKETS[$i][0] = $hSocket Then
  207. $iElement = $i
  208. ExitLoop
  209. EndIf
  210. Next
  211. If $iElement Then
  212. ___ASockShutdown($hSocket)
  213. TCPCloseSocket($hSocket)
  214. ___ArrayDelete($__TCP_SOCKETS, $iElement)
  215. Return True
  216. EndIf
  217. Return False
  218. EndFunc
  219. ; #FUNCTION# ;===============================================================================
  220. ;
  221. ; Name...........: _TCP_Server_ClientList
  222. ; Description ...: Returns the sockets of all connected clients.
  223. ; Syntax.........: _TCP_Server_ClientList()
  224. ; Parameters ....:
  225. ; Return values .: An 1 dimensional array of all connected clients.
  226. ; Author ........: Kip
  227. ; Modified.......:
  228. ; Remarks .......:
  229. ; Related .......:
  230. ; Link ..........;
  231. ; Example .......;
  232. ;
  233. ; ;==========================================================================================
  234. Func _TCP_Server_ClientList()
  235. Local $aReturn[1], $i
  236. For $i = 1 to UBound($__TCP_SOCKETS)-1
  237. If $__TCP_SOCKETS[$i][0] Then
  238. ReDim $aReturn[UBound($aReturn)+1]
  239. $aReturn[UBound($aReturn)-1] = $__TCP_SOCKETS[$i][0]
  240. EndIf
  241. Next
  242. $aReturn[0] = UBound($aReturn)-1
  243. Return $aReturn
  244. EndFunc
  245. ; #FUNCTION# ;===============================================================================
  246. ;
  247. ; Name...........: _TCP_Server_Broadcast
  248. ; Description ...: Sends data to all connected clients.
  249. ; Syntax.........: _TCP_Server_Broadcast($sData)
  250. ; Parameters ....: $sData - The data to send.
  251. ; Return values .: True
  252. ; Author ........: Kip
  253. ; Modified.......:
  254. ; Remarks .......:
  255. ; Related .......:
  256. ; Link ..........;
  257. ; Example .......;
  258. ;
  259. ; ;==========================================================================================
  260. Func _TCP_Server_Broadcast($sData)
  261. Local $i
  262. For $i = 1 to UBound($__TCP_SOCKETS)-1
  263. If $__TCP_SOCKETS[$i][0] Then TCPSend($__TCP_SOCKETS[$i][0], $sData)
  264. Next
  265. Return True
  266. EndFunc
  267. ; #FUNCTION# ;===============================================================================
  268. ;
  269. ; Name...........: _TCP_Client_Create
  270. ; Description ...: Creates a new client.
  271. ; Syntax.........: _TCP_Client_Create($sIP , $iPort)
  272. ; Parameters ....: $sIP - The IP address to connect to.
  273. ; $iPort - Port on which the created socket will be connected.
  274. ; Return values .: Client socket handle.
  275. ; Author ........: Kip
  276. ; Modified.......:
  277. ; Remarks .......:
  278. ; Related .......:
  279. ; Link ..........;
  280. ; Example .......;
  281. ;
  282. ; ;==========================================================================================
  283. Func _TCP_Client_Create($sIP , $iPort)
  284. ReDim $__TCP_SOCKETS[UBound($__TCP_SOCKETS)+1][7]
  285. local $hSocket = ___ASocket()
  286. $__TCP_SOCKETS[UBound($__TCP_SOCKETS)-1][0] = $hSocket
  287. $__TCP_SOCKETS[UBound($__TCP_SOCKETS)-1][1] = 0x0400 + (UBound($__TCP_SOCKETS)-1)
  288. ___ASockSelect( $hSocket, $__TCP_WINDOW, 0x0400 + (UBound($__TCP_SOCKETS)-1), BitOR( $FD_READ, $FD_WRITE, $FD_CONNECT, $FD_CLOSE ) )
  289. GUIRegisterMsg( 0x0400 + (UBound($__TCP_SOCKETS)-1), "___TCP_Client_OnSocketEvent" )
  290. ___ASockConnect( $hSocket, $sIP, $iPort )
  291. Return $hSocket
  292. EndFunc
  293. ; #FUNCTION# ;===============================================================================
  294. ;
  295. ; Name...........: _TCP_RegisterEvent
  296. ; Description ...: Registers an event.
  297. ; Syntax.........: _TCP_RegisterEvent($hSocket, $iEvent, $sFunction)
  298. ; Parameters ....: $hSocket - Socket of the server or a client.
  299. ; $iEvent - Event number. It can be any these values:
  300. ; * $TCP_SEND
  301. ; * $TCP_RECEIVE
  302. ; * $TCP_CONNECT => Client only
  303. ; * $TCP_DISCONNECT
  304. ; * $TCP_NEWCLIENT => Server only
  305. ; Return values .: Success - True
  306. ; Failure - False
  307. ; Author ........: Kip
  308. ; Modified.......:
  309. ; Remarks .......:
  310. ; Related .......:
  311. ; Link ..........;
  312. ; Example .......;
  313. ;
  314. ; ;==========================================================================================
  315. Func _TCP_RegisterEvent($hSocket, $iEvent, $sFunction)
  316. Local $iSelected = 0
  317. Local $i
  318. If $__TCP_SOCKETS[0][0] Then
  319. $iSelected = 0
  320. Else
  321. For $i = 0 to UBound($__TCP_SOCKETS)-1
  322. If $__TCP_SOCKETS[$i][0] = $hSocket Then
  323. $iSelected = $i
  324. ExitLoop
  325. EndIf
  326. Next
  327. If Not $iSelected Then Return 0
  328. EndIf
  329. Switch $iEvent
  330. Case $TCP_SEND
  331. $__TCP_SOCKETS[$iSelected][3] = $sFunction
  332. Case $TCP_RECEIVE
  333. $__TCP_SOCKETS[$iSelected][2] = $sFunction
  334. Case $TCP_CONNECT
  335. $__TCP_SOCKETS[$iSelected][4] = $sFunction
  336. Case $TCP_DISCONNECT
  337. $__TCP_SOCKETS[$iSelected][5] = $sFunction
  338. Case $TCP_NEWCLIENT
  339. $__TCP_SOCKETS[$iSelected][6] = $sFunction
  340. Case Else
  341. Return False
  342. EndSwitch
  343. Return True
  344. EndFunc
  345. ; #FUNCTION# ;===============================================================================
  346. ;
  347. ; Name...........: _TCP_Server_ClientIP
  348. ; Description ...: Converts a client socket handle to IP address.
  349. ; Syntax.........: _TCP_Server_ClientIP($hSocket)
  350. ; Parameters ....: $hSocket - Client socket handle.
  351. ; Return values .: A string with the IP address.
  352. ; Author ........: Unknown
  353. ; Modified.......: Kip
  354. ; Remarks .......:
  355. ; Related .......:
  356. ; Link ..........;
  357. ; Example .......;
  358. ;
  359. ; ;==========================================================================================
  360. Func _TCP_Server_ClientIP($hSocket)
  361. Local $pSocketAddress, $aReturn
  362. $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")
  363. $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $hSocket, "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))
  364. If @error Or $aReturn[0] <> 0 Then Return 0
  365. $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))
  366. If @error Then Return 0
  367. $pSocketAddress = 0
  368. Return $aReturn[0]
  369. EndFunc
  370. ; #FUNCTION# ;===============================================================================
  371. ;
  372. ; Name...........: _TCP_Send
  373. ; Description ...: Sends data to a server or client.
  374. ; Syntax.........: _TCP_Send($hSocket, $sText)
  375. ; Parameters ....: $hSocket - Client or server socket handle.
  376. ; $sText - Data to send.
  377. ; Return values .:
  378. ; Author ........: Kip
  379. ; Modified.......:
  380. ; Remarks .......:
  381. ; Related .......:
  382. ; Link ..........;
  383. ; Example .......;
  384. ;
  385. ; ;==========================================================================================
  386. Func _TCP_Send($hSocket, $sText)
  387. Return TCPSend($hSocket, $sText)
  388. EndFunc
  389. Func ___TCP_Client_OnSocketEvent( $hWnd, $iMsgID, $WParam, $LParam )
  390. Local $iError = ___HiWord( $LParam )
  391. Local $iEvent = ___LoWord( $LParam )
  392. Local $hSocket, $iElement, $i, $sDataBuff
  393. Abs($hWnd)
  394. Abs($WParam)
  395. $hSocket = 0
  396. $iElement = 0
  397. For $i = 1 to UBound($__TCP_SOCKETS)-1
  398. If $__TCP_SOCKETS[$i][1] = $iMsgID Then
  399. $hSocket = $__TCP_SOCKETS[$i][0]
  400. $iElement = $i
  401. ExitLoop
  402. EndIf
  403. Next
  404. If $hSocket Then
  405. Switch $iEvent
  406. Case $FD_READ; Data has arrived!
  407. $sDataBuff = TCPRecv( $hSocket, 1024)
  408. Call($__TCP_SOCKETS[$i][2], $hSocket, $sDataBuff, $iError)
  409. $sDataBuff = ""
  410. Case $FD_WRITE
  411. Call($__TCP_SOCKETS[$i][3], $hSocket, $iError)
  412. Case $FD_CONNECT
  413. Call($__TCP_SOCKETS[$i][4], $hSocket, $iError)
  414. Case $FD_CLOSE
  415. ___ASockShutdown( $hSocket )
  416. TCPCloseSocket( $hSocket )
  417. Call($__TCP_SOCKETS[$i][5], $hSocket, $iError)
  418. ___ArrayDelete($__TCP_SOCKETS, $iElement)
  419. EndSwitch
  420. EndIf
  421. EndFunc
  422. ;==================================================================================================================
  423. ;
  424. ; Zatorg's Asynchronous Sockets UDF Starts from here.
  425. ;
  426. ;==================================================================================================================
  427. Func ___ASocket($iAddressFamily = 2, $iType = 1, $iProtocol = 6)
  428. If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" )
  429. Local $hSocket = DllCall($hWs2_32, "uint", "socket", "int", $iAddressFamily, "int", $iType, "int", $iProtocol)
  430. If @error Then
  431. SetError(1, @error)
  432. Return -1
  433. EndIf
  434. If $hSocket[ 0 ] = -1 Then
  435. SetError(2, ___WSAGetLastError())
  436. Return -1
  437. EndIf
  438. Return $hSocket[ 0 ]
  439. EndFunc ;==>_ASocket
  440. Func ___ASockShutdown($hSocket)
  441. If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" )
  442. Local $iRet = DllCall($hWs2_32, "int", "shutdown", "uint", $hSocket, "int", 2)
  443. If @error Then
  444. SetError(1, @error)
  445. Return False
  446. EndIf
  447. If $iRet[ 0 ] <> 0 Then
  448. SetError(2, ___WSAGetLastError())
  449. Return False
  450. EndIf
  451. Return True
  452. EndFunc ;==>_ASockShutdown
  453. Func ___ASockClose($hSocket)
  454. If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" )
  455. Local $iRet = DllCall($hWs2_32, "int", "closesocket", "uint", $hSocket)
  456. If @error Then
  457. SetError(1, @error)
  458. Return False
  459. EndIf
  460. If $iRet[ 0 ] <> 0 Then
  461. SetError(2, ___WSAGetLastError())
  462. Return False
  463. EndIf
  464. Return True
  465. EndFunc ;==>_ASockClose
  466. Func ___ASockSelect($hSocket, $hWnd, $uiMsg, $iEvent)
  467. If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" )
  468. Local $iRet = DllCall( _
  469. $hWs2_32, _
  470. "int", "WSAAsyncSelect", _
  471. "uint", $hSocket, _
  472. "hwnd", $hWnd, _
  473. "uint", $uiMsg, _
  474. "int", $iEvent _
  475. )
  476. If @error Then
  477. SetError(1, @error)
  478. Return False
  479. EndIf
  480. If $iRet[ 0 ] <> 0 Then
  481. SetError(2, ___WSAGetLastError())
  482. Return False
  483. EndIf
  484. Return True
  485. EndFunc ;==>_ASockSelect
  486. ; Note: you can see that $iMaxPending is set to 5 by default.
  487. ; IT DOES NOT MEAN THAT DEFAULT = 5 PENDING CONNECTIONS
  488. ; 5 == SOMAXCONN, so don't worry be happy
  489. Func ___ASockListen($hSocket, $sIP, $uiPort, $iMaxPending = 5); 5 == SOMAXCONN => No need to change it.
  490. Local $iRet
  491. Local $stAddress
  492. If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" )
  493. $stAddress = ___SockAddr($sIP, $uiPort)
  494. If @error Then
  495. SetError(@error, @extended)
  496. Return False
  497. EndIf
  498. $iRet = DllCall($hWs2_32, "int", "bind", "uint", $hSocket, "ptr", DllStructGetPtr($stAddress), "int", DllStructGetSize($stAddress))
  499. If @error Then
  500. SetError(3, @error)
  501. Return False
  502. EndIf
  503. If $iRet[ 0 ] <> 0 Then
  504. $stAddress = 0; Deallocate
  505. SetError(4, ___WSAGetLastError())
  506. Return False
  507. EndIf
  508. $iRet = DllCall($hWs2_32, "int", "listen", "uint", $hSocket, "int", $iMaxPending)
  509. If @error Then
  510. SetError(5, @error)
  511. Return False
  512. EndIf
  513. If $iRet[ 0 ] <> 0 Then
  514. $stAddress = 0; Deallocate
  515. SetError(6, ___WSAGetLastError())
  516. Return False
  517. EndIf
  518. Return True
  519. EndFunc ;==>_ASockListen
  520. Func ___ASockConnect($hSocket, $sIP, $uiPort)
  521. Local $iRet
  522. Local $stAddress
  523. If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" )
  524. $stAddress = ___SockAddr($sIP, $uiPort)
  525. If @error Then
  526. SetError(@error, @extended)
  527. Return False
  528. EndIf
  529. $iRet = DllCall($hWs2_32, "int", "connect", "uint", $hSocket, "ptr", DllStructGetPtr($stAddress), "int", DllStructGetSize($stAddress))
  530. If @error Then
  531. SetError(3, @error)
  532. Return False
  533. EndIf
  534. $iRet = ___WSAGetLastError()
  535. If $iRet = 10035 Then; WSAEWOULDBLOCK
  536. Return True; Asynchronous connect attempt has been started.
  537. EndIf
  538. SetExtended(1); Connected immediately
  539. Return True
  540. EndFunc ;==>_ASockConnect
  541. ; A wrapper function to ease all the pain in creating and filling the sockaddr struct
  542. Func ___SockAddr($sIP, $iPort, $iAddressFamily = 2)
  543. Local $iRet
  544. Local $stAddress
  545. If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" )
  546. $stAddress = DllStructCreate("short; ushort; uint; char[8]")
  547. If @error Then
  548. SetError(1, @error)
  549. Return False
  550. EndIf
  551. DllStructSetData($stAddress, 1, $iAddressFamily)
  552. $iRet = DllCall($hWs2_32, "ushort", "htons", "ushort", $iPort)
  553. DllStructSetData($stAddress, 2, $iRet[ 0 ])
  554. $iRet = DllCall($hWs2_32, "uint", "inet_addr", "str", $sIP)
  555. If $iRet[ 0 ] = 0xffffffff Then; INADDR_NONE
  556. $stAddress = 0; Deallocate
  557. SetError(2, ___WSAGetLastError())
  558. Return False
  559. EndIf
  560. DllStructSetData($stAddress, 3, $iRet[ 0 ])
  561. Return $stAddress
  562. EndFunc ;==>__SockAddr
  563. Func ___WSAGetLastError()
  564. If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" )
  565. Local $iRet = DllCall($hWs2_32, "int", "WSAGetLastError")
  566. If @error Then
  567. ;ConsoleWrite("+> _WSAGetLastError(): WSAGetLastError() failed. Script line number: " & @ScriptLineNumber & @CRLF)
  568. SetExtended(1)
  569. Return 0
  570. EndIf
  571. Return $iRet[ 0 ]
  572. EndFunc ;==>_WSAGetLastError
  573. ; Got these here:
  574. ; http://www.autoitscript.com/forum/index.php?showtopic=5620&hl=MAKELONG
  575. Func ___MakeLong($LoWord, $HiWord)
  576. Return BitOR($HiWord * 0x10000, BitAND($LoWord, 0xFFFF)); Thanks Larry
  577. EndFunc ;==>_MakeLong
  578. Func ___HiWord($Long)
  579. Return BitShift($Long, 16); Thanks Valik
  580. EndFunc ;==>_HiWord
  581. Func ___LoWord($Long)
  582. Return BitAND($Long, 0xFFFF); Thanks Valik
  583. EndFunc ;==>_LoWord
  584. ; ========================================= Array functions
  585. ; #FUNCTION# ====================================================================================================================
  586. ; Name...........: _ArrayDelete
  587. ; Description ...: Deletes the specified element from the given array.
  588. ; Syntax.........: _ArrayDelete(ByRef $avArray, $iElement)
  589. ; Parameters ....: $avArray - Array to modify
  590. ; $iElement - Element to delete
  591. ; Return values .: Success - New size of the array
  592. ; Failure - 0, sets @error to:
  593. ; |1 - $avArray is not an array
  594. ; |3 - $avArray has too many dimensions (only up to 2D supported)
  595. ; |(2 - Deprecated error code)
  596. ; Author ........: Cephas <cephas at clergy dot net>
  597. ; Modified.......: Jos van der Zande <jdeb at autoitscript dot com> - array passed ByRef, Ultima - 2D arrays supported, reworked function (no longer needs temporary array; faster when deleting from end)
  598. ; Remarks .......: If the array has one element left (or one row for 2D arrays), it will be set to "" after _ArrayDelete() is used on it.
  599. ; Related .......: _ArrayAdd, _ArrayInsert, _ArrayPop, _ArrayPush
  600. ; Link ..........;
  601. ; Example .......; Yes
  602. ; ===============================================================================================================================
  603. Func ___ArrayDelete(ByRef $avArray, $iElement)
  604. If Not IsArray($avArray) Then Return SetError(1, 0, 0)
  605. Local $iUBound = UBound($avArray, 1) - 1
  606. If Not $iUBound Then
  607. $avArray = ""
  608. Return 0
  609. EndIf
  610. ; Bounds checking
  611. If $iElement < 0 Then $iElement = 0
  612. If $iElement > $iUBound Then $iElement = $iUBound
  613. ; Move items after $iElement up by 1
  614. Switch UBound($avArray, 0)
  615. Case 1
  616. For $i = $iElement To $iUBound - 1
  617. $avArray[$i] = $avArray[$i + 1]
  618. Next
  619. ReDim $avArray[$iUBound]
  620. Case 2
  621. Local $iSubMax = UBound($avArray, 2) - 1
  622. For $i = $iElement To $iUBound - 1
  623. For $j = 0 To $iSubMax
  624. $avArray[$i][$j] = $avArray[$i + 1][$j]
  625. Next
  626. Next
  627. ReDim $avArray[$iUBound][$iSubMax + 1]
  628. Case Else
  629. Return SetError(3, 0, 0)
  630. EndSwitch
  631. Return $iUBound
  632. EndFunc ;==>_ArrayDelete

comments powered by Disqus