DreamDancer

Winsock::ServerProc

Mar 14th, 2011
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' WinProc message handler for receiving server data
  2. ' pumps it into a queue via ServerMessageToClient at READ
  3. Public Function ServerProc(ByVal localHwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  4. Dim wsaError As Long
  5.   Select Case uMsg
  6.     Case WINSOCK_MESSAGE
  7.       Select Case lParam
  8.         Case FD_ACCEPT
  9.         Case FD_CONNECT
  10.           ' note, you should never see this message :-p
  11.            PumpInternalMessage "Winsock Connected to the game server."
  12.         Case FD_WRITE
  13.         Case FD_READ
  14.           Dim sTemp As String, lRet As Long, szBuf As String
  15.           Do
  16.             szBuf = String(4096, 0)
  17.             lRet = recv(wParam, ByVal szBuf, Len(szBuf), 0)
  18.             If lRet > 0 Then sTemp = sTemp + Left$(szBuf, lRet)
  19.           Loop Until lRet <= 0
  20.         ' original where it should be send action, to process the dataflow, we redirect it to the message pump
  21.          ServerMessageToClient Replace$(sTemp, Chr$(0), "")
  22.         Case Else
  23.           wsaError = WSAGetLastError()
  24.           If wsaError > 0 Then PumpInternalMessage GetWSAErrorString(wsaError)
  25.         ' unlike the the client to server connection below, this on has a FD_CLOSE case
  26.        ' I have no idea why the original coding was done this way, seemed best not to tamper with it
  27.        ' it works after all, ...
  28.          PumpInternalMessage "Lost connection with the game server."
  29.           stopServices
  30.           If BotScript.AutoConnect Then
  31.             addAutomation axAutoConnect, axAutoTime * axAutoAttempts, "Reconnection in " & axAutoTime * axAutoAttempts & " seconds"
  32.             proxy.btnConnect.Caption = "Abort " & axAutoTime * axAutoAttempts
  33.             If axAutoAttempts < 10 Then axAutoAttempts = axAutoAttempts + 1
  34.           End If
  35.       End Select
  36.       Exit Function
  37.     Case WM_CLOSE
  38.     ' only WinMessage processed here, might be redundant seeing as the Form_Unload also closes the sockets
  39.    ' unlike the WINSOCK_MESSAGE case above, this one falls through to permit DefWindowProc to clean up
  40.    ' fall through is is required by windows
  41.      If socketServer <> INVALID_SOCKET Then
  42.         closesocket socketServer
  43.         socketServer = INVALID_SOCKET
  44.       End If
  45.   End Select
  46.   ServerProc = DefWindowProc(localHwnd, uMsg, wParam, lParam)
  47. End Function
Advertisement
Add Comment
Please, Sign In to add comment