Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }