/// <summary> /// Creates a package for a resending /// </summary> /// <param name="connectionRecord">Current <see cref="ReliableUdpConnectionRecord"/> (ConnectionRecord)</param> /// <param name="requestedSequence">Lost packet number </param> /// <returns>Array of bytes with header and data</returns> internal static byte[] RetransmissionCreateUdpPayload(ReliableUdpConnectionRecord connectionRecord, int requestedSequence) { //RU: проверка на последний пакет, //EN: check for last packet //bool isLast = ((RequestedSequence + 1) - connectionRecord.NumberOfPackets == 0); bool isLast = ((requestedSequence + 1) == connectionRecord.NumberOfPackets); //RU: установка размера данных для отправки //EN: set the size of data for sending int length = isLast ? connectionRecord.OutcomingStream.Length - requestedSequence * connectionRecord.BufferSize : connectionRecord.BufferSize; //RU: создаем заголовок для пакета ReliableUdpHeader header = CreateReliableUdpHeader(connectionRecord.TransmissionId, isLast ? ReliableUdpHeaderFlags.LastPacket : ReliableUdpHeaderFlags.None, connectionRecord.ReliableUdpMessageType, requestedSequence, 0); byte[] udpPayload = new byte[length + ReliableUdpHeader.Length]; Array.Copy(header.ToBytes(), udpPayload, ReliableUdpHeader.Length); Array.Copy(connectionRecord.OutcomingStream, connectionRecord.BufferSize * requestedSequence, udpPayload, ReliableUdpHeader.Length, length); return(udpPayload); }
/// <summary> /// Sends acknowledge /// </summary> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="ObjectDisposedException"></exception> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="SocketException"></exception> internal static void SendAcknowledgePacket(ReliableUdpConnectionRecord connectionRecord, bool isCheckForLastOk = false) { //Debug.WriteLine( "To {0}. Ask {1} | connectionRecord.RcvCurrent {2} | connectionRecord.WLB {3}", connectionRecord.RemoteClient, connectionRecord.RcvCurrent + 1, connectionRecord.RcvCurrent, connectionRecord.WindowLowerBound ); ReliableUdpHeader header = CreateAcknowledgeHeader(connectionRecord); if (isCheckForLastOk) { header.Flags = header.Flags | ReliableUdpHeaderFlags.LastPacket; } connectionRecord.Tcb.SendUdp(header.ToBytes(), connectionRecord.RemoteClient); }
/// <summary> /// Creates udp payload /// </summary> /// <param name="connectionRecord">Current <see cref="ReliableUdpConnectionRecord"/> (ConnectionRecord)</param> /// <param name="header"><see cref="ReliableUdpHeader"/></param> /// <returns>Array of bytes with header and data</returns> internal static byte[] CreateUdpPayload(ReliableUdpConnectionRecord connectionRecord, ReliableUdpHeader header) { //byte[] bytes = Header.Flags.HasFlag(ReliableUdpHeaderFlags.LastPacket) // ? new byte[connectionRecord.OutcomingStream.Length - connectionRecord.RcvCurrent*connectionRecord.BufferSize] // :new byte[connectionRecord.BufferSize]; //Array.Copy(connectionRecord.OutcomingStream,connectionRecord.BufferSize * connectionRecord.SndCurrent,bytes,0,bytes.Length); //byte[] udpPayload = new byte[bytes.Length + ReliableUdpHeader.Length]; //Array.Copy( Header.ToBytes(), udpPayload, ReliableUdpHeader.Length ); //Array.Copy( bytes, 0, udpPayload, 0, bytes.Length ); int length = header.Flags.HasFlag(ReliableUdpHeaderFlags.LastPacket) ? connectionRecord.OutcomingStream.Length - connectionRecord.SndNext * connectionRecord.BufferSize : connectionRecord.BufferSize; byte[] udpPayload = new byte[length + ReliableUdpHeader.Length]; Array.Copy(header.ToBytes(), udpPayload, ReliableUdpHeader.Length); Array.Copy(connectionRecord.OutcomingStream, connectionRecord.BufferSize * connectionRecord.SndNext, udpPayload, ReliableUdpHeader.Length, length); return(udpPayload); }
/// <summary> /// Creates udp payload /// </summary> /// <param name="connectionRecord">Current <see cref="ReliableUdpConnectionRecord"/> (ConnectionRecord)</param> /// <param name="header"><see cref="ReliableUdpHeader"/></param> /// <returns>Array of bytes with header and data</returns> internal static byte[] CreateUdpPayload(ReliableUdpConnectionRecord connectionRecord, ReliableUdpHeader header) { //byte[] bytes = Header.Flags.HasFlag(ReliableUdpHeaderFlags.LastPacket) // ? new byte[connectionRecord.OutcomingStream.Length - connectionRecord.RcvCurrent*connectionRecord.BufferSize] // :new byte[connectionRecord.BufferSize]; //Array.Copy(connectionRecord.OutcomingStream,connectionRecord.BufferSize * connectionRecord.SndCurrent,bytes,0,bytes.Length); //byte[] udpPayload = new byte[bytes.Length + ReliableUdpHeader.Length]; //Array.Copy( Header.ToBytes(), udpPayload, ReliableUdpHeader.Length ); //Array.Copy( bytes, 0, udpPayload, 0, bytes.Length ); int length = header.Flags.HasFlag(ReliableUdpHeaderFlags.LastPacket) ? connectionRecord.OutcomingStream.Length - connectionRecord.SndNext*connectionRecord.BufferSize : connectionRecord.BufferSize; byte[] udpPayload = new byte[length + ReliableUdpHeader.Length]; Array.Copy(header.ToBytes(), udpPayload, ReliableUdpHeader.Length); Array.Copy(connectionRecord.OutcomingStream, connectionRecord.BufferSize*connectionRecord.SndNext, udpPayload, ReliableUdpHeader.Length, length); return udpPayload; }