Example #1
0
        public override void OnMessageReceived(MessageReceivedEventArgs e)
        {
            var proudMessage = (ProudMessage)e.Message;

            using (var ms = new PooledMemoryStream(Service.ArrayPool))
                using (var r = ms.ToBinaryReader(true))
                {
                    e.Message.Serialize(ms);
                    ms.Position = 0;

                    var opCode        = r.ReadUInt16();
                    var leagueMessage = _messageFactory.GetMessage(e.Session, opCode, r);
                    leagueMessage.IsRelayed    = proudMessage.IsRelayed;
                    leagueMessage.SenderHostId = proudMessage.SenderHostId;
                    leagueMessage.TargetHostId = proudMessage.TargetHostId;

                    if (!r.IsEOF())
#if DEBUG
                    { throw new NetsphereBadFormatException(leagueMessage.GetType(), ms.ToSegment()); }
#else
                    { throw new NetsphereBadFormatException(leagueMessage.GetType()); }
#endif

                    e.Message = leagueMessage;
                    base.OnMessageReceived(e);
                }
        }
            public void Serialize(Stream stream)
            {
                using (var ms = new PooledMemoryStream(_session.Service.ArrayPool))
                {
                    _message.Serialize(ms);
                    var segment = ms.ToSegment();

                    using (var w = stream.ToBinaryWriter(true))
                    {
                        w.Write(Magic);
                        w.WriteScalar(segment.Count);
                        w.Write(segment.Array, segment.Offset, segment.Count);
                    }
                }
            }
Example #3
0
        public void Send(ProudSession session, CoreMessage message)
        {
            using (var ms = new PooledMemoryStream(_pipe.Service.ArrayPool))
                using (var w = new BinaryWriter(ms))
                {
                    var @out = _protocol.Serialize(session, message);
                    using (var ms2 = new PooledMemoryStream(_pipe.Service.ArrayPool))
                    {
                        @out.Serialize(ms2);
                        var segment = ms2.ToSegment();

                        w.Write((ushort)43981);
                        w.Write((ushort)0);
                        w.Write(segment.Count);
                        w.Write(0);
                        w.Write(0);
                        w.Write(segment.Array, segment.Offset, segment.Count);

                        segment = ms.ToSegment();
                        _socket.SendTo(segment.Array, segment.Offset, segment.Count, SocketFlags.None, session.UdpEndPoint);
                    }
                }
        }