示例#1
0
        public void Response(byte[] package, ResponseType responseType, PackageCompression packageCompression = PackageCompression.Auto)
        {
            if (responseType != ResponseType.CommandResponse)
            {
                return;
            }

            ResponseData?.Invoke(this, package);
        }
示例#2
0
        public void CommandResponse(Command command, byte[] data, PackageCompression packageCompression = PackageCompression.Auto)
        {
            var package = new List <byte>();

            package.AddRange(BitConverter.GetBytes(command.Identifier));
            package.AddRange(data);

            Response(package.ToArray(), ResponseType.CommandResponse, packageCompression);
        }
        public void CommandResponse(Command command, byte[] data,
                                    PackageCompression packageCompression = PackageCompression.Auto)
        {
            var package = new byte[4 + data.Length];

            Array.Copy(BitConverter.GetBytes(command.Identifier), package, 4);
            Array.Copy(data, 0, package, 4, data.Length);
            Response(package, ResponseType.CommandResponse, packageCompression);
        }
示例#4
0
        public Task SendCommand(Command command, byte[] data,
                                PackageCompression packageCompression = PackageCompression.Auto)
        {
            var package = new byte[data.Length + 4];

            Buffer.BlockCopy(BitConverter.GetBytes(command.Identifier), 0, package, 0, 4);
            Buffer.BlockCopy(data, 0, package, 4, data.Length);

            Sender.SendCommand(ClientInformation.Id, package, packageCompression);
            return(Task.FromResult(false));
        }
        public void Response(byte[] package, ResponseType responseType,
                             PackageCompression packageCompression = PackageCompression.Auto)
        {
            if (_isFailed)
            {
                return;
            }

            byte[] compressedData = null;
            var    compressed     = false;

            if ((package.Length > 75 && packageCompression == PackageCompression.Auto) ||
                packageCompression == PackageCompression.Compress)
            //Because there is a lot of overhead, we don't compress below 75 B
            {
                compressedData = LZF.Compress(package, 0);
                if (package.Length > compressedData.Length)
                {
                    //If the compression isn't larger than the source, we will send the compressed data
                    compressed = true;
                }
            }

            lock (_sendLock)
            {
                try
                {
                    ServerConnection.BinaryWriter.Write(
                        (byte)
                        (compressed
                                ? FromClientPackage.ResponseToAdministrationCompressed
                                : FromClientPackage.ResponseToAdministration));

                    ServerConnection.BinaryWriter.Write((compressed ? compressedData.Length : package.Length) + 3);
                    //1 for the responseType and 2 for the ushort
                    ServerConnection.BinaryWriter.Write(BitConverter.GetBytes(AdministrationId));
                    ServerConnection.BinaryWriter.Write((byte)responseType);
                    ServerConnection.BinaryWriter.Write(compressed ? compressedData : package);
                    ServerConnection.BinaryWriter.Flush();
                }
                catch (Exception)
                {
                    OnFailed();
                }
            }
        }
示例#6
0
        public void SendCommand(int id, byte[] bytes, PackageCompression packageCompression = PackageCompression.Auto)
        {
            try
            {
                byte[] compressedData = null;
                var    compressed     = false;
                if ((bytes.Length > 75 && packageCompression == PackageCompression.Auto) || packageCompression == PackageCompression.Compress) //Because there is a lot of overhead, we don't compress below 75 B
                {
                    compressedData = LZF.Compress(bytes, 0);
                    if (bytes.Length > compressedData.Length)
                    {
                        //If the compression isn't larger than the source, we will send the compressed data
                        compressed = true;
                    }
                }

                lock (WriterLock)
                {
                    Connection.BinaryWriter.Write(
                        (byte)
                        (compressed
                                ? FromAdministrationPackage.SendCommandCompressed
                                : FromAdministrationPackage.SendCommand));
                    Connection.BinaryWriter.Write((compressed ? compressedData.Length : bytes.Length) + 5);
                    Connection.BinaryWriter.Write(BitConverter.GetBytes(id));
                    Connection.BinaryWriter.Write((byte)SendingType.Command);
                    Connection.BinaryWriter.Write(compressed ? compressedData : bytes);
                    Connection.BinaryWriter.Flush();
                }

                if (compressed)
                {
                    Debug.Print(
                        $"Saved {bytes.Length - compressedData.Length} ({compressedData.Length}/{bytes.Length}");
                }
            }
            catch (Exception)
            {
                // ignored
            }
        }
示例#7
0
 public void SendCommand(int id, byte[] bytes, PackageCompression packageCompression)
 {
     SendCommandEvent?.Invoke(this, Tuple.Create(BitConverter.ToUInt32(bytes, 0), bytes.Skip(4).ToArray()));
 }