Beispiel #1
0
        public void HandleReceiveMessage(NetworkMessage message, NetworkPacket packet)
        {
            switch (message.header.sendType)
            {
            case SendType.Message: message.ReadRequest(packet); break;

            case SendType.Response: message.ReadResponse(packet); break;
            }

            if ((message.header.sendType == SendType.Response) &&
                message.header.isReliable)
            {
                lock (socket.thread.ACKNOWLEDGED)
                {
                    if (!socket.thread.ACKNOWLEDGED.ContainsKey(message.header.ackkey))
                    {
                        socket.thread.ACKNOWLEDGED.Add(message.header.ackkey, packet);
                    }
                }
            }

            NetworkChannelEvent channelEvent = GetChannelEvent(message.header.channelType);

            channelEvent.InvokeEvent(packet, message);

            channel.FreeMessage(message);
        }
Beispiel #2
0
        /// <summary>
        /// Setup Network Channels
        /// Cache channels to a dictionary for fast access
        /// </summary>
        public void SetupNetworkChannels()
        {
            string enumName = "";
            NetworkChannelEvent channelEvent = null;

            for (uint i = 0; i < (uint)ChannelType.LAST; i++)
            {
                //these are used for channel events
                channelEvent             = new NetworkChannelEvent();
                channelEvent.channelType = (ChannelType)i;
                channels.Add(i, channelEvent);

                try
                {
                    //these are used to map message object types to channel types
                    enumName = Enum.GetName(typeof(ChannelType), (ChannelType)i);
                    NetworkMessage message = (NetworkMessage)GetInstance("OpenP2P.Message" + enumName);
                    Type           t       = message.GetType();
                    messageToChannelType.Add(message.GetType(), (ChannelType)i);
                    channelTypeToMessage.Add((ChannelType)i, message.GetType());
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e.ToString());
                }
            }
        }
Beispiel #3
0
        public void HandleReceiveStream(NetworkMessage message, NetworkPacket packet)
        {
            NetworkMessageStream stream = (NetworkMessageStream)message;
            uint streamID = ((uint)stream.header.id << 8) | (uint)stream.header.sequence;

            NetworkMessageStream response = (NetworkMessageStream)channel.CreateMessage(stream.header.channelType);

            if (message.header.sendType == SendType.Response)
            {
                if (message.header.isReliable)
                {
                    lock (socket.thread.ACKNOWLEDGED)
                    {
                        if (!socket.thread.ACKNOWLEDGED.ContainsKey(message.header.ackkey))
                        {
                            socket.thread.ACKNOWLEDGED.Add(message.header.ackkey, packet);
                        }
                    }
                }

                message.ReadResponse(packet);
                NetworkChannelEvent channelEvent = GetChannelEvent(message.header.channelType);
                channelEvent.InvokeEvent(packet, message);
            }
            else if (message.header.sendType == SendType.Message)
            {
                //send acknowledgement

                NetworkMessageStream first = stream;
                if (cachedStreams.ContainsKey(streamID))
                {
                    first = cachedStreams[streamID];
                }
                else
                {
                    cachedStreams.Add(streamID, first);
                }

                stream.ReadRequest(packet);

                first.SetBuffer(stream.byteData, stream.startPos);

                if (stream.startPos > 0 &&
                    first.byteData.Length == (stream.startPos + stream.byteData.Length))
                {
                    NetworkChannelEvent channelEvent = GetChannelEvent(first.header.channelType);
                    channelEvent.InvokeEvent(packet, first);

                    channel.FreeMessage(first);
                }

                if (first != stream)
                {
                    channel.FreeMessage(stream);
                }
            }
        }