DreamDancer

WinSock::ClientProc

Mar 14th, 2011
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' WinProc message handler for receiving client data
  2. ' pumps it into a queue via ClientMessageToServer at FD_READ
  3. Public Function ClientProc(ByVal localHwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  4. Dim reConnect As Long
  5.   Select Case uMsg
  6.     Case WINSOCK_MESSAGE
  7.       Select Case lParam
  8.         Case FD_ACCEPT
  9.           If socketClient = INVALID_SOCKET Then
  10.             Dim tempAddr As sockaddr
  11.             socketClient = accept(wParam, tempAddr, Len(tempAddr))
  12.             closesocket socketListen
  13.             PumpInternalMessage "Client connected:" & socketClient
  14.             StandAloneMode = BotScript.KeepConnection
  15.           End If
  16.           Exit Function
  17.         Case FD_WRITE
  18.           PumpInternalMessage "Client Write Detected:"
  19.         ' this seemed to be the best point to cause connection to the server
  20.        ' winsock services generate a write event to the socket once the client connection is solid
  21.        ' so if the client is now talking to the proxy, proxy must now start talking to the server
  22.          If socketServer = INVALID_SOCKET Then
  23.             socketServer = ConnectSock(IIf(CTRL.cfgUseProxyFile = True, CTRL.cfgUseProxyAddress, "lightbringer.furcadia.com"), _
  24.                             IIf(CTRL.cfgUseProxyPort = 0, CTRL.cfgPreferredUserPort, CTRL.cfgUseProxyPort), _
  25.                             vbNullString, hServerSocket, False)
  26.             If socketServer = INVALID_SOCKET Then
  27.               PumpInternalMessage "Failed to connect to the game server."
  28.             ' ConnectSocket will generate the WSAError message, duplication not needed here
  29.              If ClientPID <> 0 Then
  30.                 PumpMessageToClient "(Whoops! Could not connect to the game server, everything go boom now." & vbCrLf & "Dismissing attempt in 5 seconds."
  31.                 axAutoAttempts = IIf(axAutoAttempts >= 10, 24, axAutoAttempts)
  32.               End If
  33.               addAutomation axStopServices, 2
  34.               addAutomation axRestoreSettings, 6
  35.               If BotScript.AutoConnect Then
  36.                 reConnect = axAutoTime * axAutoAttempts + axAutoTime
  37.                 addAutomation axAutoConnect, reConnect, "Reconnection in " & reConnect & " seconds"
  38.                 proxy.btnConnect.Caption = "Abort " & axAutoTime * axAutoAttempts
  39.                 If axAutoAttempts < 10 Then axAutoAttempts = axAutoAttempts + 1
  40.               End If
  41.               Exit Function
  42.             Else
  43.               PumpInternalMessage "Connected to the game server."
  44.               proxy.MainStatus.Panels(pStatus) = "Connected to server, Port: " & CTRL.cfgPreferredUserPort
  45.               MessagePump.processTrigger "", tConnectOpen, True
  46.             End If
  47.           End If
  48.           Exit Function
  49.         Case FD_READ
  50.           If socketClient = INVALID_SOCKET Then Exit Function
  51.             Dim sTemp As String, lRet As Long, szBuf As String
  52.             Do
  53.                 szBuf = String(1024, 0)
  54.                 lRet = recv(wParam, ByVal szBuf, Len(szBuf), 0)
  55.                 If lRet > 0 Then sTemp = sTemp + Left$(szBuf, lRet)
  56.             Loop Until lRet <= 0
  57.           ' original where it should be send action, to process the dataflow, we redirect it to the message pump
  58.            ClientMessageToServer Replace$(sTemp, Chr$(0), "")
  59.             Exit Function
  60.         Case Else 'FD_CLOSE
  61.          ' possible a strange message from winsock will also cause this
  62.          ' unknown what that strange message would be
  63.            PumpInternalMessage "Client DisConnected"
  64.             ClientPID = 0
  65.             Exit Function
  66.       End Select
  67.     Case WM_CLOSE
  68.     ' only WinMessage processed here, might be redundant seeing as the Form_Unload also closes the sockets
  69.    ' unlike the WINSOCK_MESSAGE case above, this one falls through to permit DefWindowProc to clean up
  70.    ' fall through is is required by windows
  71.      If socketListen <> INVALID_SOCKET Then
  72.         closesocket socketListen
  73.         socketListen = INVALID_SOCKET
  74.       End If
  75.       If socketClient <> INVALID_SOCKET Then
  76.         closesocket socketClient
  77.         socketClient = INVALID_SOCKET
  78.       End If
  79.   End Select
  80.   ClientProc = DefWindowProc(localHwnd, uMsg, wParam, lParam)
  81. End Function
Advertisement
Add Comment
Please, Sign In to add comment