Esempio n. 1
0
        public List <NetworkPacket> SendStream(EndPoint ep, NetworkMessage message)
        {
            IPEndPoint           ip      = GetIPv6(ep);
            NetworkMessageStream stream  = (NetworkMessageStream)message;
            List <NetworkPacket> packets = new List <NetworkPacket>();

            stream.header.channelType = channel.GetChannelType(stream);
            stream.header.isReliable  = true;
            stream.header.isStream    = true;
            stream.header.sendType    = SendType.Message;
            stream.header.sequence    = ident.local.NextSequence(stream);
            stream.header.id          = ident.local.id;

            while (stream.segmentLen > 0 && stream.startPos < stream.byteData.Length)
            {
                NetworkPacket packet = socket.Prepare(ep);
                packet.messages.Add(stream);

                WriteHeader(packet, stream);
                WriteRequest(packet, stream);

                socket.Send(packet);
                Console.WriteLine("Sent " + (stream.segmentLen) + " bytes");
            }

            return(packets);
        }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
0
        private void OnStreamDataContent(object sender, NetworkMessage e)
        {
            NetworkMessageStream stream = (NetworkMessageStream)e;

            Console.WriteLine("Command: " + stream.command);
            string result = Encoding.UTF8.GetString(stream.byteData);

            latency = NetworkTime.Milliseconds() - latencyStartTime;
            Console.WriteLine("Stream took " + (latency) + " ms");
            Console.WriteLine("Text: " + result);
        }