예제 #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, 0);

            if (Stream.Length < length)
                return false;

            InnerNetworkMessage message = new InnerNetworkMessage
            {
                OpCode = BitConverter.ToInt16(headerBytes, 2),
                Data = new byte[length - 4]
            };

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

            messages.Add(message);

            TrimStream();

            return true;
        }
예제 #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, 0);

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

            InnerNetworkMessage message = new InnerNetworkMessage
            {
                OpCode = BitConverter.ToInt16(headerBytes, 2),
                Data   = new byte[length - 4]
            };

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

            messages.Add(message);

            TrimStream();

            return(true);
        }
예제 #3
0
        private bool Send()
        {
            InnerNetworkMessage message;

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

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

                message = new InnerNetworkMessage {
                    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);
        }
예제 #4
0
        private void OnMessageReceived(object sender, MessageEventArgs e)
        {
            InnerNetworkMessage message = (InnerNetworkMessage)e.Message;

            Buffer = message.Data;

            if (InnerNetworkOpcode.Recv.ContainsKey(message.OpCode))
            {
                ((InnerNetworkRecvPacket)Activator.CreateInstance(InnerNetworkOpcode.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());
            }
        }
예제 #5
0
        private bool Send()
        {
            InnerNetworkMessage message;

            if (SendLock == null)
                return false;

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

                message = new InnerNetworkMessage { 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;
        }