Exemplo n.º 1
0
        private bool ReadMessage(List <IScsMessage> messages)
        {
            Stream.Position = 0;

            if (Stream.Length < 4)
            {
                return(false);
            }

            byte[] headerBytes = new byte[4];
            Stream.Read(headerBytes, 0, 4);

            int length = BitConverter.ToUInt16(headerBytes, 2);

            if (Stream.Length < length)
            {
                return(false);
            }

            OuterNetworkMessage message = new OuterNetworkMessage
            {
                OpCode = BitConverter.ToInt16(headerBytes, 0),
                Data   = new byte[length]
            };

            Stream.Read(message.Data, 0, message.Data.Length);

            messages.Add(message);

            TrimStream();

            return(true);
        }
Exemplo n.º 2
0
        private bool ReadMessage(List<IScsMessage> messages)
        {
            Stream.Position = 0;

            if (Stream.Length < 4)
                return false;

            byte[] headerBytes = new byte[4];
            Stream.Read(headerBytes, 0, 4);

            int length = BitConverter.ToUInt16(headerBytes, 2);

            if (Stream.Length < length)
                return false;

            OuterNetworkMessage message = new OuterNetworkMessage
            {
                OpCode = BitConverter.ToInt16(headerBytes, 0),
                Data = new byte[length]
            };

            Stream.Read(message.Data, 0, message.Data.Length);

            messages.Add(message);

            TrimStream();

            return true;
        }
Exemplo n.º 3
0
        private bool Send()
        {
            OuterNetworkMessage message;

            if (SendLock == null)
            {
                return(false);
            }

            lock (SendLock)
            {
                if (SendData.Count == 0)
                {
                    return(Client.CommunicationState == CommunicationStates.Connected);
                }

                message = new OuterNetworkMessage {
                    Data = new byte[SendDataSize]
                };

                int pointer = 0;
                for (int i = 0; i < SendData.Count; i++)
                {
                    Array.Copy(SendData[i], 0, message.Data, pointer, SendData[i].Length);
                    pointer += SendData[i].Length;
                }

                SendData.Clear();
                SendDataSize = 0;
            }

            try
            {
                Client.SendMessage(message);
            }
            catch
            {
                //Already closed
                return(false);
            }

            return(true);
        }
Exemplo n.º 4
0
        private void OnMessageReceived(object sender, MessageEventArgs e)
        {
            OuterNetworkMessage message = (OuterNetworkMessage)e.Message;

            Buffer = message.Data;

            if (OuterNetworkOpcode.Recv.ContainsKey(message.OpCode))
            {
                //Log.Info("Recieve Packet:{0} [{1}]", OuterNetworkOpcode.Recv[message.OpCode].Name, Buffer.Length);
                ((OuterNetworkRecvPacket)Activator.CreateInstance(OuterNetworkOpcode.Recv[message.OpCode])).Process(this);
            }
            else
            {
                string opCodeLittleEndianHex = BitConverter.GetBytes(message.OpCode).ToHex();
                Log.Debug("Unknown InnerOpcode: 0x{0}{1} [{2}]",
                          opCodeLittleEndianHex.Substring(2),
                          opCodeLittleEndianHex.Substring(0, 2),
                          Buffer.Length);

                Log.Debug("Data:\n{0}", Buffer.FormatHex());
            }
        }
Exemplo n.º 5
0
        private bool Send()
        {
            OuterNetworkMessage message;

            if (SendLock == null)
                return false;

            lock (SendLock)
            {
                if (SendData.Count == 0)
                    return Client.CommunicationState == CommunicationStates.Connected;

                message = new OuterNetworkMessage { Data = new byte[SendDataSize] };

                int pointer = 0;
                for (int i = 0; i < SendData.Count; i++)
                {
                    Array.Copy(SendData[i], 0, message.Data, pointer, SendData[i].Length);
                    pointer += SendData[i].Length;
                }

                SendData.Clear();
                SendDataSize = 0;
            }

            try
            {
                Client.SendMessage(message);
            }
            catch
            {
                //Already closed
                return false;
            }

            return true;
        }