Пример #1
0
        /// <summary>
        /// Функция сбора пакета
        /// </summary>
        /// <param name="lowCommand">Низкоуровневая команда</param>
        /// <param name="data">Данные, усекается до 65529</param>
        /// <returns></returns>
        static byte[] AssemblyRawPack(LowCommands lowCommand, params byte[] data)
        {
            int length = 0;

            byte[] pack = null;

            if (data != null)
            {
                int dataLength = 0;
                dataLength = data.Length > 65530 ? 65530 : data.Length;
                length     = 2 + 1 + 1 + dataLength + 1;
                pack       = new byte[length];
                System.Buffer.BlockCopy(data, 0, pack, 4, dataLength);
                pack[pack.Length - 1] = Crc8.ComputeChecksum(4, dataLength, pack);
            }
            else
            {
                length = 2 + 1 + 1;
                pack   = new byte[length];
            }

            System.Buffer.BlockCopy(BitConverter.GetBytes(length), 0, pack, 0, 2);
            pack[2] = (byte)lowCommand;
            pack[3] = Crc8.ComputeChecksum(0, 3, pack);
            return(pack);
        }
Пример #2
0
        void ServerModule_ClientCommand(FromClientCommand Command)
        {
            if (Command.Command == ClientActions.Disconnected || Command.Command == ClientActions.Shutdown)
            {
                LB_Users.Items.Remove(Command.Client);
            }
            else if (Command.Command == ClientActions.Connected)
            {
                LB_Users.Items.Add(Command.Client);
            }

            if (Command.ReceiveBufferLength > 0)
            {
                if (Command.Command == ClientActions.Receive)
                {
                    if (Command.ReceiveBufferLength >= 4 && Crc8.ComputeChecksum(0, 3, Command.ReceiveBuffer) == Command.ReceiveBuffer[3])
                    {
                        if (Command.Client == null)
                        {
                            TB_Messages.AppendText(DateTime.Now.ToString() + ": " + "Message: " + Command.Command + ", Buffer length: " + Command.ReceiveBufferLength + "\n");
                        }
                        else
                        {
                            if ((LowCommands)Command.ReceiveBuffer[2] == LowCommands.Version)
                            {
                                TB_Messages.AppendText(DateTime.Now.ToString() + ": " + Command.Client.ClientEndPoint + " Message: " + Command.Command + ", Buffer: " + Encoding.ASCII.GetString(Command.ReceiveBuffer, 4, Command.ReceiveBufferLength - 4 - 1) + ", Type: " + (LowCommands)Command.ReceiveBuffer[2] + "\n");
                            }
                            else if ((LowCommands)Command.ReceiveBuffer[2] == LowCommands.RunType)
                            {
                                TB_Messages.AppendText(DateTime.Now.ToString() + ": " + Command.Client.ClientEndPoint + " Message: " + Command.Command + ", Value: " + BitConverter.ToInt32(Command.ReceiveBuffer, 4) + ", Type: " + (LowCommands)Command.ReceiveBuffer[2] + "\n");
                            }
                            else
                            {
                                TB_Messages.AppendText(DateTime.Now.ToString() + ": " + Command.Client.ClientEndPoint + " Message: " + Command.Command + ", Buffer length: " + Command.ReceiveBufferLength + ", Type: " + (LowCommands)Command.ReceiveBuffer[2] + "\n");
                            }
                        }
                    }
                    else
                    {
                        ServerModule.Send(new ToServerCommand()
                        {
                            Action = ServerCommands.Disconnect, ToClient = new List <OnServerClientConnection>()
                            {
                                Command.Client
                            }
                        });
                    }
                }

                //TB_Messages.AppendText(Command.Client.ClientInfo + " Message: " + Command.Command + ", Buffer: " + Encoding.ASCII.GetString(Command.ReceiveBuffer, 0, Command.ReceiveBufferLength) + "\n");

                //TB_Messages.AppendText(Command.Client.ClientInfo + " Message: " + Command.Command + ", Buffer: " + BitConverter.ToString(Command.ReceiveBuffer, 0, Command.ReceiveBufferLength) + "\n");
            }
            else
            {
                if (Command.Client == null)
                {
                    TB_Messages.AppendText(DateTime.Now.ToString() + ": " + "Message: " + Command.Command + "\n");
                }
                else
                {
                    TB_Messages.AppendText(DateTime.Now.ToString() + ": " + Command.Client.ClientEndPoint + " Message: " + Command.Command + "\n");
                }
            }

            if ((TB_Messages.ExtentHeight - TB_Messages.VerticalOffset - TB_Messages.ViewportHeight) < 50)
            {
                TB_Messages.ScrollToEnd();
            }
        }