Пример #1
0
        private void ProcessCbCopy(ClipboardSetTextMessage msg)
        {
            if (msg.Part == 1)
            {
                cbTextBuilder = new StringBuilder();
            }

            cbTextBuilder.Append(msg.Text);

            if (msg.Part == msg.PartCount)
            {
                ClipboardTextReceived?.Invoke(this, cbTextBuilder.ToString());
            }
        }
Пример #2
0
        public void SetClipboardText(string text)
        {
            if (!Connected)
            {
                throw new InvalidOperationException("Client not connected");
            }

            if (text == null)
            {
                ISLogger.Write("Warning: Attempted to send null clipboard text");
                return;
            }

            ISLogger.Write("Socket->Sending clipboard text to {0}", ClientName);

            int partsNeeded = (text.Length / ClipboardTextPartSize);
            int part        = 0;
            int strPos      = 0;

            if (partsNeeded == 1)
            {
                ClipboardSetTextMessage msg = new ClipboardSetTextMessage(text, 1, 1);
                SendQueue.Add(msg);
                return;
            }

            while (part <= partsNeeded)
            {
                int copyLen = ClipboardTextPartSize;
                if (part == partsNeeded)
                {
                    if (strPos + copyLen > text.Length)
                    {
                        copyLen = text.Length - strPos;
                    }
                }
                string str = text.Substring(strPos, copyLen);
                ClipboardSetTextMessage msg = new ClipboardSetTextMessage(str, part + 1, partsNeeded + 1);
                strPos = strPos + copyLen;
                SendQueue.Add(msg);
                part++;
            }

            text = "";
        }
Пример #3
0
        public void SendClipboardText(string text)
        {
            if (!IsStateConnected())
            {
                throw new InvalidOperationException($"Attempted to send clipboard data when connection state is {State}");
            }

            if (text == null)
            {
                ISLogger.Write("Warning: Attempted to send a null string");
                return;
            }

            int partsNeeded = (text.Length / ClipboardTextPartSize);
            int part        = 0;
            int strPos      = 0;

            if (partsNeeded == 1)
            {
                ClipboardSetTextMessage msg = new ClipboardSetTextMessage(text, 1, 1);
                byte[] data = msg.ToBytes();
                tcpSocket.BeginSend(data, 0, data.Length, 0, SendCallback, null);
                return;
            }

            while (part <= partsNeeded)
            {
                int copyLen = ClipboardTextPartSize;
                if (part == partsNeeded)
                {
                    if (strPos + copyLen > text.Length)
                    {
                        copyLen = text.Length - strPos;
                    }
                }
                string str = text.Substring(strPos, copyLen);
                ClipboardSetTextMessage msg = new ClipboardSetTextMessage(str, part + 1, partsNeeded + 1);
                strPos = strPos + copyLen;
                byte[] d = msg.ToBytes();
                tcpSocket.BeginSend(d, 0, d.Length, 0, SendCallback, null);
                part++;
            }
            text = "";
        }
Пример #4
0
        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;
            }
        }
Пример #5
0
        private void SocketReceiveThreadLoop()
        {
            try
            {
                ISLogger.Write("Socket receive thread started");
                byte[] header = new byte[4];
                while (!cancelToken.IsCancellationRequested)
                {
                    int hRem = 4;
                    int hPos = 0;
                    do
                    {
                        int hIn = tcpSocket.Receive(header, hPos, hRem, 0);   //make sure we read all 4 bytes of header
                        hPos += hIn;
                        hRem -= hIn;
                    } while (hRem > 0);
                    int pSize = BitConverter.ToInt32(header, 0);

                    if (pSize > Settings.ClientMaxPacketSize)
                    {
                        OnConnectionError(new Exception("Connection error: Server sent invalid packet size of " + pSize), ServerSocketState.ConnectionError);
                        return;
                    }
                    int dRem = pSize;
                    int bPos = 4;
                    do
                    {
                        int bIn = tcpSocket.Receive(socketBuffer, bPos, dRem, 0);
                        bPos += bIn;
                        dRem  = pSize - bPos + 4;
                    } while (dRem > 0);
                    MessageType cmd = (MessageType)socketBuffer[4];
                    switch (cmd)
                    {
                    case MessageType.Input:
                    {
                        InputMessage msg = InputMessage.FromBytes(socketBuffer);
                        InputReceived?.Invoke(this, msg.Input);
                        break;
                    }

                    case MessageType.ServerOK:
                        ISLogger.Write("Server sent OK");
                        SetState(ServerSocketState.Connected);
                        MessageReceived?.Invoke(this, cmd);
                        break;

                    case MessageType.SetClipboardText:
                        ProcessCbCopy(ClipboardSetTextMessage.FromBytes(socketBuffer));
                        break;

                    case MessageType.FileTransferPart:
                        FileTransferPartMessage fileMsg = FileTransferPartMessage.FromBytes(ref socketBuffer);
                        ReadFilePart(fileMsg);
                        break;

                    default:
                        MessageReceived?.Invoke(this, cmd);
                        break;
                    }
                }
            }catch (ObjectDisposedException)
            {
            }catch (Exception ex)
            {
                if (!disconnecting)
                {
                    disconnecting = true;
                }
                else
                {
                    return;
                }

                if (cancelToken.IsCancellationRequested)
                {
                    return;
                }

                if (!ex.Message.Contains("WSACancelBlockingCall"))
                {
                    //ISLogger.Write("Serversocket error: " + ex.Message);
                }
                OnConnectionError(ex, ServerSocketState.ConnectionError);
            }
        }
Пример #6
0
        private void SocketReceiveThreadLoop()
        {
            try
            {
                ISLogger.Write("Socket receive thread started");
                byte[] header = new byte[4];
                while (!cancelToken.IsCancellationRequested)
                {
                    tcpSocket.Receive(header, 4, 0);
                    int pSize = BitConverter.ToInt32(header, 0);

                    int dRem = pSize;
                    int bPos = 4;
                    do
                    {
                        int bIn = tcpSocket.Receive(socketBuffer, bPos, dRem, 0);
                        bPos += bIn;
                        dRem  = pSize - bPos + 4;
                    } while (dRem > 0);

                    MessageType cmd = (MessageType)socketBuffer[4];
                    switch (cmd)
                    {
                    case MessageType.Input:
                    {
                        InputMessage msg = InputMessage.FromBytes(socketBuffer);
                        InputReceived?.Invoke(this, msg.Input);
                        break;
                    }

                    case MessageType.ServerOK:
                        ISLogger.Write("Server sent OK");
                        SetState(ServerSocketState.Connected);
                        MessageReceived?.Invoke(this, cmd);
                        break;

                    case MessageType.SetClipboardText:
                        ProcessCbCopy(ClipboardSetTextMessage.FromBytes(socketBuffer));
                        break;

                    default:
                        MessageReceived?.Invoke(this, cmd);
                        break;
                    }
                }
            }catch (ObjectDisposedException)
            {
            }catch (Exception ex)
            {
                if (!disconnecting)
                {
                    disconnecting = true;
                }
                else
                {
                    return;
                }

                if (cancelToken.IsCancellationRequested)
                {
                    return;
                }

                if (!ex.Message.Contains("WSACancelBlockingCall"))
                {
                    ISLogger.Write("Serversocket error: " + ex.Message);
                }
                OnConnectionError(ex, ServerSocketState.ConnectionError);
            }
        }