Example #1
0
        /* On each message received. */
        private void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliverymethod)
        {
            byte[] rawBytes      = reader.GetRemainingBytes();
            var    rawNetMessage = new RawNetMessage(rawBytes, peer, reader, deliverymethod);

            MessageHandler.Handle(ref rawNetMessage);
        }
        /// <summary>
        /// Given a raw network message, decodes the message and delegates it to an appropriate handling method.
        /// </summary>
        public void Handle(ref RawNetMessage parameters)
        {
            var messageType = MessageBase <TMessageType> .GetMessageType(parameters.Message);

            if (_mapping.TryGetValue(messageType, out RawNetMessageHandler value))
            {
                value(ref parameters);
            }
        }
        protected void ReadCallback(IAsyncResult state)
        {
            ServerPipe.EndRead(state);
            if (!ServerPipe.IsConnected)
            {
                ServerPipe.Close();
                ClientPipe.Close();
                ServerPipe = CreatePipe(PipeDirection.In);
                ClientPipe = CreatePipe(PipeDirection.Out);
                ServerPipe.BeginWaitForConnection(ConnectionCallback, ServerPipe);
                ClientPipe.BeginWaitForConnection(ConnectionCallback, ClientPipe);
                return;
            }

            var msg = new RawNetMessage(DataBuffer, null, null, DeliveryMethod.ReliableOrdered);

            MessageHandler.Handle(ref msg);
            ServerPipe.BeginRead(DataBuffer, 0, DataBuffer.Length, ReadCallback, this);
        }