Пример #1
0
        internal void DataIn(object clientData)
        {
            BaseClientData client       = (BaseClientData)clientData;
            var            clientStream = client.ClientStream;

            try
            {
                while (true)
                {
                    byte[] buffer;                 //Daten
                    byte[] dataSize = new byte[4]; //Länge

                    int readBytes = clientStream.Read(dataSize, 0, 4);

                    while (readBytes != 4)
                    {
                        readBytes += clientStream.Read(dataSize, readBytes, 4 - readBytes);
                    }
                    var contentLength = BitConverter.ToInt32(dataSize, 0);

                    buffer    = new byte[contentLength];
                    readBytes = 0;
                    while (readBytes != buffer.Length)
                    {
                        readBytes += clientStream.Read(buffer, readBytes, buffer.Length - readBytes);
                    }

                    //Daten sind im Buffer-Array
                    Router.DistributePackage(BasePackage.Deserialize <BasePackage>(buffer), client.TcpClient);
                }
            }
            catch (IOException e)
            {
                Log.Debug(e.ToString());
                BaseClientData disconnectedClient = GetClientFromClientList(client.TcpClient);
                Log.Info("Client disconnected with Uid: " + disconnectedClient.Uid);
                Clients.Remove(disconnectedClient);
                Log.Info("Client removed from list.");

                OnClientDisconnected(new ClientDisconnectedEventArgs(disconnectedClient.Uid));
            }
        }
Пример #2
0
        /// <summary>
        /// Reads the incomming data from the <see cref="NetworkStream"/>.
        /// </summary>
        private void HandleIncommingData()
        {
            try
            {
                while (true)
                {
                    byte[] buffer;                 //Daten
                    byte[] dataSize = new byte[4]; //Länge

                    int readBytes = ClientStream.Read(dataSize, 0, 4);

                    while (readBytes != 4)
                    {
                        readBytes += ClientStream.Read(dataSize, readBytes, 4 - readBytes);
                    }
                    var contentLength = BitConverter.ToInt32(dataSize, 0);

                    buffer    = new byte[contentLength];
                    readBytes = 0;
                    while (readBytes != buffer.Length)
                    {
                        readBytes += ClientStream.Read(buffer, readBytes, buffer.Length - readBytes);
                    }

                    //Daten sind im Buffer-Array gespeichert
                    PackageReceived?.Invoke(this,
                                            new PackageReceivedEventArgs(BasePackage.Deserialize(buffer), TcpClient));
                }
            }
            catch (IOException ex)
            {
                Log.Info(ex.Message);
                Log.Info("Server connection lost!");
                ConnectionLost?.Invoke(this, EventArgs.Empty);
            }
        }