Esempio n. 1
0
        public static void SendMessage(Client recipient, PacketType packetType, object data)
        {
            if (recipient == null)
            {
                return;
            }

            #if DEBUG
            //
            // If we're debugging, make sure that the data type matches that required by the packetType, or else throw an exception.
            //

            var type = DataPacket.PacketTypeMap[packetType];

            if (type != null)
            {
                var testObject = Convert.ChangeType(data, type);
            }
            else
            {
                if (data != null)
                {
                    throw new ArgumentException("Data from this packet type was expected to be null, but wasn't.");
                }
            }
            #endif

            // ToDo: Don't hardcode Packet Version 1 here.  Instead, add a packet version property to Client, and switch on that.
            var packetBase = new PacketBase {PacketVersion = PacketVersion.Version1};

            var packetV1 = new PacketV1
            {
                PacketType = packetType,
                Data = data,
                Sender = ServerName
            };

            packetV1.Recipients.Add(recipient.UserInfo.EmailAddress);

            packetBase.Data = packetV1;

            dynamic baseBuffer;

            using (var stream = new MemoryStream())
            {
                var binaryWriter = new BinaryWriter(stream);

                packetBase.Write(binaryWriter);

                baseBuffer = stream.ToArray();
            }

            var messageSize = baseBuffer.Length;

            byte[] bytes = BitConverter.GetBytes(messageSize);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            var offset = 0;

            var messageBuffer = new byte[sizeof(uint) + messageSize];

            bytes.CopyTo(messageBuffer, offset);

            offset += sizeof(uint);

            baseBuffer.CopyTo(messageBuffer, offset);

            recipient.Send(messageBuffer);
        }
Esempio n. 2
0
        private async Task SendMessage(PacketType packetType, IEnumerable<Friend> recipientList, object data)
        {
            var mainPageViewModel = MainPageViewModel.GetInstance();

            if ((ServerProxy.TcpSocket == null) || mainPageViewModel.ConnectionStatus == false)
            {
                return;
            }

#if DEBUG
            //
            // If we're debugging, make sure that the data type matches that required by the packetType, or else throw an exception.
            //

            var type = DataPacket.PacketTypeMap[packetType];

            if (type != null)
            {
                var testObject = Convert.ChangeType(data, type);
            }
            else
            {
                if (data != null)
                {
                    throw new ArgumentException("Data from this packet type was expected to be null, but wasn't.");
                }
            }
#endif

            using (var dataWriter = new DataWriter(ServerProxy.TcpSocket.OutputStream))
            {
                byte[] msgSizeBuffer;
                dynamic baseBuffer = null;

                if (PacketType.c_HeartBeat == packetType)
                {
                    // Message Size is specified by the size of an int.
                    msgSizeBuffer = new byte[] {0, 0, 0, 0};
                }
                else
                {
                    var packetBase = new PacketBase {PacketVersion = PacketVersion.Version1};

                    var packetV1 = new PacketV1
                        {
                            PacketType = packetType,
                            Data = data,
                            Sender = Settings.EmailAddress
                        };

                    foreach (var b in recipientList)
                    {
                        packetV1.Recipients.Add(b.EmailAddress);
                    }

                    packetBase.Data = packetV1;

                    using (var stream = new MemoryStream())
                    {
                        var binaryWriter = new BinaryWriter(stream);

                        packetBase.Write(binaryWriter);

                        baseBuffer = stream.ToArray();
                    }


                    var messageSize = baseBuffer.Length;

                    msgSizeBuffer = BitConverter.GetBytes(messageSize);

                    if (!BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(msgSizeBuffer);
                    }
                }


                //
                // 1. Write the message size
                // 2. Write the object buffer (unless it's a heartbeat)
                //

                dataWriter.WriteBytes(msgSizeBuffer);

                if (baseBuffer != null)
                {
                    dataWriter.WriteBytes(baseBuffer);
                }

                await dataWriter.StoreAsync();

                dataWriter.DetachStream();
            }
        }