Ejemplo n.º 1
0
        UdpSendStatus SendFragmentedMessage(UdpRawMessage message, IChannel channel)
        {
            message.Position = 0;
            int    mtu     = this.Mtu;
            ushort frame   = 1;
            ushort frames  = (ushort)Math.Ceiling(message.Length / (float)(mtu - Datagram.GetHeaderSize(true)));
            ushort groupId = GetNextFragementationGroupId();

            do
            {
                Debug.Assert(frame <= frames, "frame > frames");

                Datagram datagramFrag = Datagram.CreateNew(peer.Configuration.MemoryStreamPool, mtu);
                datagramFrag.Type              = MessageType.UserData;
                datagramFrag.Channel           = channel.Descriptor.Channel;
                datagramFrag.DeliveryType      = channel.Descriptor.DeliveryType;
                datagramFrag.FragmentationInfo = new Datagram.FragmentInfo(groupId, frame, frames);
                datagramFrag.ConnectionKey     = this.EndPoint.ConnectionKey;

                int toCopy = mtu - Datagram.GetHeaderSize(true);
                if (toCopy > message.Length - message.Position)
                {
                    toCopy = message.Length - message.Position;
                }
                message.CopyTo(datagramFrag.BaseStream, toCopy);

                channel.SendDatagram(datagramFrag);
                frame++;
            } while (message.Position < message.Length);

            message.Dispose();

            return(UdpSendStatus.Enqueued);
        }
Ejemplo n.º 2
0
 internal void StartConnect(UdpRawMessage payload)
 {
     if (!CheckStatus(UdpConnectionStatus.Waiting))
     {
         throw new InvalidOperationException($"Couldn't connect wrong status: {Status}, expected {UdpConnectionStatus.Waiting}");
     }
     logger.Info($"Connecting to {EndPoint.EndPoint}");
     ChangeStatus(UdpConnectionStatus.Connecting);
     lastStatusPacketSent = DateTime.UtcNow;
     if (payload == null)
     {
         connectReq = CreateSpecialDatagram(MessageType.ConnectReq);
     }
     else
     {
         connectReq = CreateSpecialDatagram(MessageType.ConnectReq, payload);
         payload.Dispose();
     }
     connectReq.DontDisposeOnSend = true;
     SendDatagramAsync(connectReq);
 }
Ejemplo n.º 3
0
        public virtual void Close(UdpRawMessage payload)
        {
            if (Status >= UdpConnectionStatus.Disconnecting)
            {
                return;
            }
            ChangeStatus(UdpConnectionStatus.Disconnecting);
            Datagram disconnectReq;

            if (payload == null)
            {
                disconnectReq = CreateSpecialDatagram(MessageType.DisconnectReq);
            }
            else
            {
                disconnectReq = CreateSpecialDatagram(MessageType.DisconnectReq, payload);
            }
            disconnectReq.DontDisposeOnSend = true;
            this.disconnectReq   = disconnectReq;
            lastStatusPacketSent = DateTime.UtcNow;
            SendDatagramAsync(disconnectReq);
            payload?.Dispose();
        }