Example #1
0
        public void Read(BinaryReader reader)
        {
            var hasInstance = reader.ReadBoolean();

            if (!hasInstance)
            {
                return;
            }

            PacketVersion = (PacketVersion)reader.ReadByte();

            switch (PacketVersion)
            {
                case PacketVersion.Version1:
                    Data = new PacketV1();
                    break;

                default:
            #if DEBUG
                    throw new ArgumentException(string.Format("Invalid Packet Version : {0}", PacketVersion.ToString()));
            #else
                    return;
            #endif
            }

            Data.Read(reader);
        }
        public static IncomingMessage GetMessage(PacketV1 packet)
        {
            var newIncomingMessage = new IncomingMessage()
            {
                PacketType = packet.PacketType,
                Sender = packet.Sender,
                Data = packet.Data,
            };

            CreateMessageProcessor(ref newIncomingMessage);

            return newIncomingMessage;
        }
Example #3
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);
        }
        public void Initialize()
        {
            _testGameMoveDataPoint = new GameMoveDataPoint { MoveType = MoveType.Dump, Player = "NewPlayer", TimeOfMove = 5 };

            _playerGameStats = new PlayerGameStats
                {
                    AvgTimeBetweenDumps = 10,
                    AvgTimeBetweenPeels = 5,
                    IsWinner = true,
                    NumberOfDumps = 3,
                    NumberOfPeels = 0,
                    PerformedFirstPeel = false,
                    RawGameData = new List<GameMoveDataPoint>() {_testGameMoveDataPoint, _testGameMoveDataPoint, _testGameMoveDataPoint}
                };

            _gameData = new GameData {GameDuration = 10, GameHost = "GameHost", GameStartTime = DateTime.Now, Winner = "NewPlayer"};
            _gameData.GameMoves.Add(_testGameMoveDataPoint);
            _gameData.GameMoves.Add(_testGameMoveDataPoint);
            _gameData.PlayerDictionary.Add("NewPlayer", _playerGameStats);
            _gameData.PlayerDictionary.Add("NewPlayer2", _playerGameStats);

            _gameDataList = new GameDataList();
            _gameDataList._GameData.Add(_gameData);
            _gameDataList._GameData.Add(_gameData);

            _stringPacketV1 = new PacketV1
                {
                    PacketType = PacketType.c_QueryIfAliasAvailable,
                    Data = "Mobius",
                    Recipients = null,
                    Sender = "*****@*****.**"
                };

            _IBinarySerializablePacketV1 = new PacketV1
            {
                PacketType = PacketType.c_UploadGameData,
                Data = _gameData,
                Recipients = null,
                Sender = "*****@*****.**"
            };

            _packetBase = new PacketBase
                {
                    PacketVersion = PacketVersion.Version1,
                    Data = _stringPacketV1
                };
        }
Example #5
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();
            }
        }