/// <summary>
 /// Initializes a new instance of the ReliableUdpConnectionRecord
 /// to send message
 /// </summary>
 /// <param name="key">EndPoint and TransmissionId key</param>
 /// <param name="tcb">Connection control block. <see cref="ReliableUdpConnectionControlBlock"/></param>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="cToken">CancellationToken</param>
 /// <param name="asyncResult"><see cref="AsyncResultSendMessage"/></param>
 /// <exception cref="ArgumentNullException"><paramref name="key"/> or <paramref name="tcb"/> is a null reference</exception>
 public ReliableUdpConnectionRecord(Tuple <EndPoint, Int32> key, ReliableUdpConnectionControlBlock tcb,
                                    ReliableUdpMessage reliableUdpMessage, CancellationToken cToken, AsyncResultSendMessage asyncResult)
     : this(key, tcb)
 {
     if (reliableUdpMessage == null)
     {
         throw new ArgumentNullException("reliableUdpMessage");
     }
     if (reliableUdpMessage.Body == null || reliableUdpMessage.Body.Length == 0)
     {
         throw new ArgumentNullException("reliableUdpMessage", "reliableUdpMessage.Body can not be null.");
     }
     CToken                      = cToken;
     this.AsyncResult            = asyncResult;
     this.ReliableUdpMessageType = reliableUdpMessage.Type;
     this.IsNoAnswerNeeded       = reliableUdpMessage.NoAsk;
     //RU: добавляем содержимое ReliableUdpMessage в словарь ReliableUdpConnectionControlBlock'a
     //EN: fills the array of outcoming message with message bytes
     this.OutcomingStream = Tcb.OutcomingStreams.GetOrAdd(key, reliableUdpMessage.Body);
     //RU: Расчитываем количество пакетов необходимых для отправки сообщения
     this.NumberOfPackets = (int)Math.Ceiling((double)((double)OutcomingStream.Length / (double)this.BufferSize));
     //RU: переключаем состояние на отправку первого пакета
     //EN: set initial state
     this.State = Tcb.States.FirstPacketSending;
 }
 /// <summary>
 /// Passes message to subscribers
 /// </summary>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/></param>
 /// <param name="remoteClient">Endpoint of sender</param>
 internal void PassMessageToSubscribers(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteClient)
 {
     try
     {
         //EN: To protect from calling removed callback delegate
         System.Threading.Monitor.Enter(m_locker);
         foreach (ReliableUdpSubscribeObject subscriber in m_subscribers)
         {
             bool isRequiredEp = (subscriber.IpEndPoint == null) ||
                                 ((subscriber.IpEndPoint.Port == 0) &&
                                  subscriber.IpEndPoint.Address.Equals(remoteClient.Address)) ||
                                 (subscriber.IpEndPoint.Equals(remoteClient));
             bool isRequiredMessageType = (subscriber.ReliableUdpMessageType == ReliableUdpMessageTypes.Any) ||
                                          (subscriber.ReliableUdpMessageType == reliableUdpMessage.Type);
             if (isRequiredEp && isRequiredMessageType)
             {
                 if (subscriber.ReliableUdpMessageCallback != null)
                 {
                     subscriber.ReliableUdpMessageCallback(reliableUdpMessage, remoteClient);
                 }
             }
         }
     }
     finally
     {
         System.Threading.Monitor.Exit(m_locker);
     }
 }
        public Task <bool> SendMessageAsync(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint, CancellationToken cToken)
        {
            TaskFactory <bool> taskFactory = new TaskFactory <bool>(cToken);

            return(taskFactory.FromAsync(BeginSendTask, EndSend, new WrappedBeginSendParameters(reliableUdpMessage, remoteEndPoint, cToken),
                                         null));
        }
 /// <summary>
 /// Initializes a new instance of the AsyncResultSendMessage
 /// </summary>
 /// <param name="reliableUdpMessage">Message to send</param>
 /// <param name="endPoint">Remote endpoint</param>
 /// <param name="asyncCallback">asyncCallback method</param>
 /// <param name="state">An object that contains state information for this request</param>
 /// <param name="owner">Object who calls the BeginSend method (instance of <see cref="ReliableUdpConnectionControlBlock"/>)</param>
 public AsyncResultSendMessage(ReliableUdpMessage reliableUdpMessage, EndPoint endPoint, CancellationToken cToken,
                               AsyncCallback asyncCallback, object state, object owner)
     : base(asyncCallback, state, owner)
 {
     ReliableUdpMessage = reliableUdpMessage;
     EndPoint           = endPoint;
     Token = cToken;
 }
        /// <summary>
        /// Creates message
        /// </summary>
        /// <param name="connectionRecord">Current <see cref="ReliableUdpConnectionRecord"/> (ConnectionRecord)</param>
        internal static void CreateMessageFromMemoryStream(ReliableUdpConnectionRecord connectionRecord)
        {
            ReliableUdpMessage msg = new ReliableUdpMessage(connectionRecord.ReliableUdpMessageType,
                                                            connectionRecord.Tcb.IncomingStreams[connectionRecord.Key],
                                                            connectionRecord.IsNoAnswerNeeded);

            connectionRecord.Tcb.PassMessageToSubscribers(msg, connectionRecord.RemoteClient);
            Debug.WriteLine("Done {0}!", connectionRecord.TransmissionId);
        }
        /// <summary>
        /// Sends the <paramref name="reliableUdpMessage"/> asynchronously to the specified endpoint.
        /// </summary>
        /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
        /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
        /// <param name="callback">The <see cref="AsyncCallback"/> delegate.</param>
        /// <param name="state">An object that contains state information for this request.</param>
        /// <returns>An IAsyncResult that references the asynchronous send.</returns>
        public IAsyncResult BeginSend(ReliableUdpMessage reliableUdpMessage,
                                      IPEndPoint remoteEndPoint, AsyncCallback callback, Object state)
        {
            //RU: упаковали данные в собственный AsyncResult
            //EN: wrapped data in our own AsyncResult
            AsyncResultSendMessage ar = new AsyncResultSendMessage(reliableUdpMessage, remoteEndPoint, CancellationToken.None, callback, state, this);

            ThreadPool.QueueUserWorkItem(StartTransmissionHelper, ar);
            return(ar);
        }
 /// <summary>
 /// Sends the <paramref name="reliableUdpMessage"/> pseudo-synchronously to the specified endpoint.
 /// </summary>
 /// <remarks>Asynchronous methods of sending and receiving packets are actually used.
 /// But initial start of sending is carried out in the current thread.
 /// If <see cref="ReliableUdpMessage.NoAsk"/> is set to <c>true</c> than method returns only when
 /// all packets will be sent.</remarks>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="ReliableUdpConfigurationException"></exception>
 /// <exception cref="ObjectDisposedException"></exception>
 /// <exception cref="InvalidOperationException"></exception>
 /// <exception cref="SocketException"></exception>
 public void SendMessage(ReliableUdpMessage reliableUdpMessage,
                         IPEndPoint remoteEndPoint)
 {
     StartTransmission(reliableUdpMessage, remoteEndPoint, CancellationToken.None, null);
 }
Example #8
0
 /// <summary>
 /// Sends the <paramref name="reliableUdpMessage"/> pseudo-synchronously to the specified endpoint.
 /// </summary>
 /// <remarks>Asynchronous methods of sending and receiving packets are actually used.
 /// But initial start of sending is carried out in the current thread.
 /// If <see cref="ReliableUdpMessage.NoAsk"/> is set to <c>true</c> than method returns only when
 /// all packets will be sent.</remarks>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="ReliableUdpConfigurationException"></exception>
 /// <exception cref="ObjectDisposedException"></exception>
 /// <exception cref="InvalidOperationException"></exception>
 /// <exception cref="SocketException"></exception>
 public void SendMessage(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint)
 {
     m_tcb.SendMessage(reliableUdpMessage, remoteEndPoint);
 }
 public WrappedBeginSendParameters( ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint, CancellationToken cToken )
 {
     ReliableUdpMessage = reliableUdpMessage;
     RemoteEndPoint = remoteEndPoint;
     Token = cToken;
 }
        /// <summary>
        /// Creates new ReliableUdpConnectionRecord and starts finite state automaton
        /// </summary>
        /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
        /// <param name="endPoint">Ip endpoint of recipient of the message.</param>
        /// <param name="asyncResult"><see cref="AsyncResultSendMessage"/> object</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ReliableUdpConfigurationException"></exception>
        /// <returns></returns>
        private void StartTransmission(ReliableUdpMessage reliableUdpMessage, EndPoint endPoint, CancellationToken cToken,
            AsyncResultSendMessage asyncResult)
        {
            try
              {
            if (m_isListenerStarted == 0)
            {
              if (this.LocalEndpoint == null)
              {
            throw new ArgumentNullException( "", "You must use constructor with parameters or start listener before sending message" );
              }
              StartListener(LocalEndpoint);
            }
            //RU: создаем ключ для словаря, на основе EndPoint и ReliableUdpHeader.TransmissionId
            //EN: create key based on EndPoint and ReliableUdpHeader.TransmissionId for dictionary
            byte[] transmissionId = new byte[4];

            //RU: только криптопровайдер! random выдает одинаковые номера для двух одновременно созданных TCB
            //EN: should use only cryptographic random function. System.Random gives the same values for two
            //EN: or more ConnnectionControlBlock created at the same time
            m_randomCrypto.GetBytes(transmissionId);
            Tuple<EndPoint, Int32> key = new Tuple<EndPoint, Int32>(endPoint, BitConverter.ToInt32(transmissionId, 0));
            Debug.WriteLine("Transmission Id is {0}", key.Item2);
            //EN: make two attemps to add key into dictionary
            if (!m_listOfHandlers.TryAdd(key, new ReliableUdpConnectionRecord(key, this, reliableUdpMessage, cToken, asyncResult)))
            {
              m_randomCrypto.GetBytes(transmissionId);
              key = new Tuple<EndPoint, Int32>(endPoint, BitConverter.ToInt32(transmissionId, 0));
              if (!m_listOfHandlers.TryAdd(key, new ReliableUdpConnectionRecord(key, this, reliableUdpMessage,cToken, asyncResult)))
            throw new ArgumentException("Pair TransmissionId & EndPoint is already exists in the dictionary");
            }
            //RU: запустили состояние в обработку.
            //EN: run state handling
            m_listOfHandlers[key].State.SendPacket(m_listOfHandlers[key]);
              }
            //RU: Все исключения в состояниях обрабатываются отдельно
            //RU: здесь ловятся только те, которые могут появиться до запуска конечного автомата
            //EN: All exceptions that raised in states are handled separately
              catch (ArgumentNullException ex)
              {
            if (asyncResult != null)
            {
              asyncResult.SetAsCompleted(ex);
            }
            else
            {
              throw;
            }
              }
              catch (ArgumentOutOfRangeException ex)
              {
            if (asyncResult != null)
            {
              asyncResult.SetAsCompleted(ex);
            }
            else
            {
              throw;
            }
              }
              catch (ArgumentException ex)
              {
            if (asyncResult != null)
            {
              asyncResult.SetAsCompleted(ex);
            }
            else
            {
              throw;
            }
              }
              catch (ReliableUdpConfigurationException ex)
              {
            if (asyncResult != null)
            {
              asyncResult.SetAsCompleted(ex);
            }
            else
            {
              throw;
            }
              }
        }
 /// <summary>
 /// Passes message to subscribers
 /// </summary>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/></param>
 /// <param name="remoteClient">Endpoint of sender</param>
 internal void PassMessageToSubscribers( ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteClient )
 {
     try
       {
     //EN: To protect from calling removed callback delegate
     System.Threading.Monitor.Enter( m_locker );
     foreach ( ReliableUdpSubscribeObject subscriber in m_subscribers )
     {
       bool isRequiredEp = (subscriber.IpEndPoint == null)
                       || ((subscriber.IpEndPoint.Port == 0)
                           && subscriber.IpEndPoint.Address.Equals( remoteClient.Address ))
                       || (subscriber.IpEndPoint.Equals( remoteClient ));
       bool isRequiredMessageType = (subscriber.ReliableUdpMessageType == ReliableUdpMessageTypes.Any)
                                || (subscriber.ReliableUdpMessageType == reliableUdpMessage.Type);
       if ( isRequiredEp && isRequiredMessageType )
       {
     if ( subscriber.ReliableUdpMessageCallback != null )
       subscriber.ReliableUdpMessageCallback( reliableUdpMessage, remoteClient );
       }
     }
       }
       finally
       {
     System.Threading.Monitor.Exit( m_locker );
       }
 }
 public Task<bool> SendMessageAsync(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint, CancellationToken cToken)
 {
     TaskFactory<bool> taskFactory = new TaskFactory<bool>(cToken);
       return taskFactory.FromAsync(BeginSendTask, EndSend, new WrappedBeginSendParameters(reliableUdpMessage, remoteEndPoint, cToken),
                            null);
 }
Example #13
0
 /// <summary>
 /// Creates message
 /// </summary>
 /// <param name="connectionRecord">Current <see cref="ReliableUdpConnectionRecord"/> (ConnectionRecord)</param>
 internal static void CreateMessageFromMemoryStream(ReliableUdpConnectionRecord connectionRecord)
 {
     ReliableUdpMessage msg = new ReliableUdpMessage(connectionRecord.ReliableUdpMessageType,
                                               connectionRecord.Tcb.IncomingStreams[connectionRecord.Key],
                                               connectionRecord.IsNoAnswerNeeded);
       connectionRecord.Tcb.PassMessageToSubscribers(msg, connectionRecord.RemoteClient);
       Debug.WriteLine("Done {0}!", connectionRecord.TransmissionId);
 }
Example #14
0
 /// <summary>
 /// Sends message async (can be used with .net 4 framework)
 /// </summary>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
 /// <returns><c>true</c> if the message was successfully sent.
 /// <c>false</c> if sending the message was interrupted on a timeout.</returns>
 /// <exception cref="AggregateException">Contains one or more errors that occur during application execution.</exception>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="ReliableUdpConfigurationException"></exception>
 /// <exception cref="ObjectDisposedException"></exception>
 /// <exception cref="InvalidOperationException"></exception>
 /// <exception cref="SocketException"></exception>
 public Task <bool> SendMessageAsync(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint)
 {
     return(m_tcb.SendMessageAsync(reliableUdpMessage, remoteEndPoint, CancellationToken.None));
 }
        /// <summary>
        /// Creates new ReliableUdpConnectionRecord and starts finite state automaton
        /// </summary>
        /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
        /// <param name="endPoint">Ip endpoint of recipient of the message.</param>
        /// <param name="asyncResult"><see cref="AsyncResultSendMessage"/> object</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ReliableUdpConfigurationException"></exception>
        /// <returns></returns>
        private void StartTransmission(ReliableUdpMessage reliableUdpMessage, EndPoint endPoint, CancellationToken cToken,
                                       AsyncResultSendMessage asyncResult)
        {
            try
            {
                if (m_isListenerStarted == 0)
                {
                    if (this.LocalEndpoint == null)
                    {
                        throw new ArgumentNullException("", "You must use constructor with parameters or start listener before sending message");
                    }
                    StartListener(LocalEndpoint);
                }
                //RU: создаем ключ для словаря, на основе EndPoint и ReliableUdpHeader.TransmissionId
                //EN: create key based on EndPoint and ReliableUdpHeader.TransmissionId for dictionary
                byte[] transmissionId = new byte[4];

                //RU: только криптопровайдер! random выдает одинаковые номера для двух одновременно созданных TCB
                //EN: should use only cryptographic random function. System.Random gives the same values for two
                //EN: or more ConnnectionControlBlock created at the same time
                m_randomCrypto.GetBytes(transmissionId);
                Tuple <EndPoint, Int32> key = new Tuple <EndPoint, Int32>(endPoint, BitConverter.ToInt32(transmissionId, 0));
                Debug.WriteLine("Transmission Id is {0}", key.Item2);
                //EN: make two attemps to add key into dictionary
                if (!m_listOfHandlers.TryAdd(key, new ReliableUdpConnectionRecord(key, this, reliableUdpMessage, cToken, asyncResult)))
                {
                    m_randomCrypto.GetBytes(transmissionId);
                    key = new Tuple <EndPoint, Int32>(endPoint, BitConverter.ToInt32(transmissionId, 0));
                    if (!m_listOfHandlers.TryAdd(key, new ReliableUdpConnectionRecord(key, this, reliableUdpMessage, cToken, asyncResult)))
                    {
                        throw new ArgumentException("Pair TransmissionId & EndPoint is already exists in the dictionary");
                    }
                }
                //RU: запустили состояние в обработку.
                //EN: run state handling
                m_listOfHandlers[key].State.SendPacket(m_listOfHandlers[key]);
            }
            //RU: Все исключения в состояниях обрабатываются отдельно
            //RU: здесь ловятся только те, которые могут появиться до запуска конечного автомата
            //EN: All exceptions that raised in states are handled separately
            catch (ArgumentNullException ex)
            {
                if (asyncResult != null)
                {
                    asyncResult.SetAsCompleted(ex);
                }
                else
                {
                    throw;
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                if (asyncResult != null)
                {
                    asyncResult.SetAsCompleted(ex);
                }
                else
                {
                    throw;
                }
            }
            catch (ArgumentException ex)
            {
                if (asyncResult != null)
                {
                    asyncResult.SetAsCompleted(ex);
                }
                else
                {
                    throw;
                }
            }
            catch (ReliableUdpConfigurationException ex)
            {
                if (asyncResult != null)
                {
                    asyncResult.SetAsCompleted(ex);
                }
                else
                {
                    throw;
                }
            }
        }
Example #16
0
 /// <summary>
 /// Sends message async (can be used with .net 4 framework)
 /// </summary>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
 /// <returns><c>true</c> if the message was successfully sent. 
 /// <c>false</c> if sending the message was interrupted on a timeout.</returns>
 /// <exception cref="AggregateException">Contains one or more errors that occur during application execution.</exception>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="ReliableUdpConfigurationException"></exception>
 /// <exception cref="ObjectDisposedException"></exception>
 /// <exception cref="InvalidOperationException"></exception>
 /// <exception cref="SocketException"></exception>
 public Task<bool> SendMessageAsync( ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint )
 {
     return m_tcb.SendMessageAsync( reliableUdpMessage, remoteEndPoint, CancellationToken.None );
 }
Example #17
0
 /// <summary>
 /// Sends the <paramref name="reliableUdpMessage"/> pseudo-synchronously to the specified endpoint.
 /// </summary>
 /// <remarks>Asynchronous methods of sending and receiving packets are actually used.
 /// But initial start of sending is carried out in the current thread.
 /// If <see cref="ReliableUdpMessage.NoAsk"/> is set to <c>true</c> than method returns only when
 /// all packets will be sent.</remarks>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="ReliableUdpConfigurationException"></exception>
 /// <exception cref="ObjectDisposedException"></exception>
 /// <exception cref="InvalidOperationException"></exception>
 /// <exception cref="SocketException"></exception>
 public void SendMessage(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint)
 {
     m_tcb.SendMessage(reliableUdpMessage, remoteEndPoint);
 }
Example #18
0
 /// <summary>
 /// Sends the <paramref name="reliableUdpMessage"/> asynchronously to the specified endpoint.
 /// </summary>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
 /// <param name="asyncCallback">The <see cref="AsyncCallback"/> delegate.</param>
 /// <param name="state">An object that contains state information for this request.</param>
 /// <returns>An IAsyncResult that references the asynchronous send.</returns>
 public IAsyncResult BeginSendMessage(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint,
                                  AsyncCallback asyncCallback, Object state)
 {
     return m_tcb.BeginSend(reliableUdpMessage, remoteEndPoint, asyncCallback, state);
 }
 /// <summary>
 /// Sends the <paramref name="reliableUdpMessage"/> pseudo-synchronously to the specified endpoint.
 /// </summary>
 /// <remarks>Asynchronous methods of sending and receiving packets are actually used.
 /// But initial start of sending is carried out in the current thread.
 /// If <see cref="ReliableUdpMessage.NoAsk"/> is set to <c>true</c> than method returns only when
 /// all packets will be sent.</remarks>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="ReliableUdpConfigurationException"></exception>
 /// <exception cref="ObjectDisposedException"></exception>
 /// <exception cref="InvalidOperationException"></exception>
 /// <exception cref="SocketException"></exception>
 public void SendMessage(ReliableUdpMessage reliableUdpMessage,
     IPEndPoint remoteEndPoint)
 {
     StartTransmission(reliableUdpMessage, remoteEndPoint,CancellationToken.None, null);
 }
        /// <summary>
        /// Sends the <paramref name="reliableUdpMessage"/> asynchronously to the specified endpoint.
        /// </summary>
        /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
        /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
        /// <param name="callback">The <see cref="AsyncCallback"/> delegate.</param>
        /// <param name="state">An object that contains state information for this request.</param>
        /// <returns>An IAsyncResult that references the asynchronous send.</returns>
        public IAsyncResult BeginSend(ReliableUdpMessage reliableUdpMessage,
            IPEndPoint remoteEndPoint, AsyncCallback callback, Object state)
        {
            //RU: упаковали данные в собственный AsyncResult
              //EN: wrapped data in our own AsyncResult
              AsyncResultSendMessage ar = new AsyncResultSendMessage(reliableUdpMessage, remoteEndPoint,CancellationToken.None, callback, state, this);

              ThreadPool.QueueUserWorkItem(StartTransmissionHelper, ar);
              return ar;
        }
 /// <summary>
 /// Initializes a new instance of the ReliableUdpConnectionRecord
 /// to send message 
 /// </summary>
 /// <param name="key">EndPoint and TransmissionId key</param>
 /// <param name="tcb">Connection control block. <see cref="ReliableUdpConnectionControlBlock"/></param>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="cToken">CancellationToken</param>
 /// <param name="asyncResult"><see cref="AsyncResultSendMessage"/></param>
 /// <exception cref="ArgumentNullException"><paramref name="key"/> or <paramref name="tcb"/> is a null reference</exception>
 public ReliableUdpConnectionRecord(Tuple<EndPoint, Int32> key, ReliableUdpConnectionControlBlock tcb,
                                ReliableUdpMessage reliableUdpMessage, CancellationToken cToken, AsyncResultSendMessage asyncResult)
     : this(key, tcb)
 {
     if (reliableUdpMessage == null)
       {
     throw new ArgumentNullException("reliableUdpMessage");
       }
       if (reliableUdpMessage.Body == null || reliableUdpMessage.Body.Length == 0)
       {
     throw new ArgumentNullException("reliableUdpMessage", "reliableUdpMessage.Body can not be null.");
       }
       CToken = cToken;
       this.AsyncResult = asyncResult;
       this.ReliableUdpMessageType = reliableUdpMessage.Type;
       this.IsNoAnswerNeeded = reliableUdpMessage.NoAsk;
       //RU: добавляем содержимое ReliableUdpMessage в словарь ReliableUdpConnectionControlBlock'a
       //EN: fills the array of outcoming message with message bytes
       this.OutcomingStream = Tcb.OutcomingStreams.GetOrAdd(key, reliableUdpMessage.Body);
       //RU: Расчитываем количество пакетов необходимых для отправки сообщения
       this.NumberOfPackets = (int) Math.Ceiling((double) ((double) OutcomingStream.Length/(double) this.BufferSize));
       //RU: переключаем состояние на отправку первого пакета
       //EN: set initial state
       this.State = Tcb.States.FirstPacketSending;
 }
 public WrappedBeginSendParameters(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint, CancellationToken cToken)
 {
     ReliableUdpMessage = reliableUdpMessage;
     RemoteEndPoint     = remoteEndPoint;
     Token = cToken;
 }
Example #23
0
 /// <summary>
 /// Sends the <paramref name="reliableUdpMessage"/> asynchronously to the specified endpoint.
 /// </summary>
 /// <param name="reliableUdpMessage"><see cref="ReliableUdpMessage"/> message to send.</param>
 /// <param name="remoteEndPoint">Ip endpoint of recipient of the message.</param>
 /// <param name="asyncCallback">The <see cref="AsyncCallback"/> delegate.</param>
 /// <param name="state">An object that contains state information for this request.</param>
 /// <returns>An IAsyncResult that references the asynchronous send.</returns>
 public IAsyncResult BeginSendMessage(ReliableUdpMessage reliableUdpMessage, IPEndPoint remoteEndPoint,
                                      AsyncCallback asyncCallback, Object state)
 {
     return(m_tcb.BeginSend(reliableUdpMessage, remoteEndPoint, asyncCallback, state));
 }