public void TestMethod1()
        {
            string testText = "adasdsada\0\0\0\0\0\0\0\0";

            TransferCommandObject t = new TransferCommandObject {
                Command = "Login", Value = testText
            };


            var t2 = ClientServerPipeline.BufferDeserialize(ClientServerPipeline.BufferSerialize(t));

            Assert.AreEqual(t.Value, t2.Value);
        }
Exemple #2
0
        /// <summary>
        /// The async callback of receivedata
        /// </summary>
        /// <param name="result"></param>
        private void ReceiveData(IAsyncResult result)
        {
            try
            {
                Socket clientSocket = (Socket)result.AsyncState;
                Client client       = GetClientBySocket(clientSocket);
                if (client == null)
                {
                    return;
                }

                int          bytesReceived = clientSocket.EndReceive(result);
                DataByteType type          = (DataByteType)client.Data[SharedProperties.TypeByte];
                if (bytesReceived == 0)
                {
                    CloseSocket(clientSocket);
                    serverSocket.BeginAccept(new AsyncCallback(HandleIncomingConnection), serverSocket);
                }
                else if (Enum.IsDefined(typeof(DataByteType), (DataByteType)client.Data[SharedProperties.TypeByte]))
                {
                    int  length = BitConverter.ToInt32(new byte[] { client.Data[SharedProperties.LengthByte1], client.Data[SharedProperties.LengthByte2], 0, 0 }, 0);
                    int  series = BitConverter.ToInt32(new byte[] { client.Data[SharedProperties.SeriesByte1], client.Data[SharedProperties.SeriesByte2], 0, 0 }, 0);
                    Guid guid   = new Guid(client.Data.SubArray(SharedProperties.GuidStartByte, 16));

                    DataBufferModel buffer = Buffers.FirstOrDefault(n => n.DataId == guid);
                    if (buffer != null)
                    {
                        buffer.BufferedData.Add(series, client.Data.SubArray(SharedProperties.HeaderByteSize, SharedProperties.DataLength));
                        buffer.LatestSeries = series;
                    }
                    else
                    {
                        buffer = new DataBufferModel();
                        buffer.BufferedData.Add(series, client.Data.SubArray(SharedProperties.HeaderByteSize, SharedProperties.DataLength));
                        buffer.DataId       = guid;
                        buffer.SeriesLength = length;
                        buffer.LatestSeries = series;
                        Buffers.Add(buffer);
                    }
                    BConsole.WriteLine($"Received data with id: {guid.ToString()}");

                    if (buffer.BufferedData.Count == buffer.SeriesLength)
                    {
                        if (HandleIncomingData(ClientServerPipeline.BufferDeserialize(buffer), client, type))
                        {
                            Buffers.Remove(buffer);
                        }
                    }
                }
                clientSocket.BeginReceive(client.Data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), clientSocket);
            }
            catch (SocketException)
            {
                Socket clientSocket = (Socket)result.AsyncState;
                Client client       = GetClientBySocket(clientSocket);
                KickClient(client);
            }
            catch (Exception e)
            {
                BConsole.WriteLine("Recieve error occured: " + e.Message, ConsoleColor.Red);
            }
        }
Exemple #3
0
        /// <summary>
        /// Connects to the Remote host and receives data, returns if connection is lost
        /// </summary>
        /// <param name="ip"></param>
        /// <returns>Returns if the client should attempt a reconnect</returns>
        public bool ConnectAndReceive(string ip)
        {
            remoteSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            remoteSocket.Connect(new IPEndPoint(IPAddress.Parse(ip), SharedProperties.MainPort));
            BConsole.WriteLine($"Connected to Trollkit host at {ip}:{SharedProperties.MainPort}", ConsoleColor.DarkGreen);
            while (remoteSocket.IsConnected())
            {
                //receive data from server.
                byte[] array = new byte[SharedProperties.DataSize];
                try
                {
                    remoteSocket.Receive(array);
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode == SocketError.ConnectionReset)
                    {
                        BConsole.WriteLine("Host connection closed unexpectedly...", ConsoleColor.Red);
                        return(true);
                    }
                    else
                    {
                        BConsole.WriteLine("Application crashed, closing now");
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    BConsole.WriteLine("Application crashed, closing now");
                    return(false);
                }
                int    length    = BitConverter.ToInt32(new byte[] { array[SharedProperties.LengthByte1], array[SharedProperties.LengthByte2], 0, 0 }, 0);
                int    series    = BitConverter.ToInt32(new byte[] { array[SharedProperties.SeriesByte1], array[SharedProperties.SeriesByte2], 0, 0 }, 0);
                byte[] guidBytes = Extensions.SubArray(array, SharedProperties.GuidStartByte, 16);
                Guid   guid      = new Guid(guidBytes);
                if (guid != Guid.Empty)
                {
                    DataBufferModel buffer = buffers.FirstOrDefault(n => n.DataId == guid);
                    if (buffer != null)
                    {
                        buffer.BufferedData.Add(series, Extensions.SubArray(array, SharedProperties.HeaderByteSize, SharedProperties.DataLength));
                        buffer.LatestSeries = series;
                    }
                    else
                    {
                        buffer = new DataBufferModel();
                        buffer.BufferedData.Add(series, Extensions.SubArray(array, SharedProperties.HeaderByteSize, SharedProperties.DataLength));
                        buffer.LatestSeries = series;
                        buffer.DataId       = guid;
                        buffer.SeriesLength = length;
                        buffers.Add(buffer);
                    }

                    if (buffer.BufferedData.Count == buffer.SeriesLength)
                    {
                        OnDataReceived?.Invoke(remoteSocket, ClientServerPipeline.BufferDeserialize(buffer));
                    }
                }
            }

            return(false);            //if code reaches here, the client was gracefully kicked
        }