/// <summary> /// Thread message loop. /// To stop windows becoming unresponsive due to us not returning from the hook callback methods fast enough, we process /// inputs on a dedicated thread. /// </summary> private void ThreadLoop(CancellationToken token) { try { while (!token.IsCancellationRequested) { Win32Msg msg = inputQueue.Take(token); int cmd = (int)msg.wParam; if (cmd == WM_KEYDOWN || cmd == WM_KEYUP || cmd == WM_SYSKEYDOWN || cmd == WM_SYSKEYUP) { IsThreadHandleKeyboard(msg.wParam, (KBDLLHOOKSTRUCT)msg.lParam); } else if (cmd >= 512 && cmd <= 524) { IsThreadHandleMouse(msg.wParam, (MSLLHOOKSTRUCT)msg.lParam); } else if (cmd == INPUTSHARE_CLIPBOARDTEXTCOPY) { ClipboardTextCopied?.Invoke(this, msg.cbText); } else { ISLogger.Write("Unhandled wparam code {0}", msg.wParam); } } } catch (OperationCanceledException) { } ISLogger.Write("Input thread message queue exited"); }
private void SocketReceiveCallback(IAsyncResult ar) { try { if (!Connected) { return; } int bytesIn = clientSocket.EndReceive(ar); int pSize = BitConverter.ToInt32(clientBuffer, 0); if (bytesIn == 0) { ISLogger.Write("Client {0} lost connection", ClientName); OnConnectionError(); return; } if (pSize > ServerClientMaxPacketSize) { ISLogger.Write("Client {0} attempted to sent a packet that was too large ({1})", ClientName, pSize); OnConnectionError(); return; } dRem = pSize; bPos = 4; do { int bIn = clientSocket.Receive(clientBuffer, bPos, dRem, 0); bPos += bIn; dRem = pSize - bPos + 4; } while (dRem > 0); MessageType msg = (MessageType)clientBuffer[4]; switch (msg) { case MessageType.SetClipboardText: { ClipboardSetTextMessage cbData = ClipboardSetTextMessage.FromBytes(clientBuffer); if (cbData.Part == 1 && cbData.PartCount == 1) { ClipboardTextCopied?.Invoke(this, cbData.Text); break; } if (cbData.Part == 1) { clipboardMsgBuffer = new List <ClipboardSetTextMessage>(); clipboardMsgBuffer.Add(cbData); } else if (cbData.Part == cbData.PartCount) { clipboardMsgBuffer.Add(cbData); string str = ""; foreach (var part in clipboardMsgBuffer) { str = str + part.Text; } ClipboardTextCopied?.Invoke(this, str); } else { clipboardMsgBuffer.Add(cbData); } break; } case MessageType.ClientBoundsBottom: ClientEdgeHit?.Invoke(this, BoundEdge.Bottom); break; case MessageType.ClientBoundsLeft: ClientEdgeHit?.Invoke(this, BoundEdge.Left); break; case MessageType.ClientBoundsRight: ClientEdgeHit?.Invoke(this, BoundEdge.Right); break; case MessageType.ClientBoundsTop: ClientEdgeHit?.Invoke(this, BoundEdge.Top); break; } clientSocket.BeginReceive(clientBuffer, 0, 4, 0, SocketReceiveCallback, null); } catch (ObjectDisposedException) { //This just means that the socket was disposed from elsewhere return; }catch (SocketException ex) { ISLogger.Write("Connection error on client {0}: {1}", ClientName, ex.Message); OnConnectionError(); return; } }