Beispiel #1
0
        /// <summary>
        ///   Returns a handler through which to be notified and provided with the 
        ///   response message for the specified outbound datagram when such response 
        ///   arrives.
        /// </summary>
        /// <param name="dgram"> The outbound datagram for which a response is expected. </param>
        public ResponseHandler CreateOrGetHandler(IOutboundDatagram dgram)
        {
            if (dgram.Type == DatagramType.Login)
            {
                if (this.loginHandler != null)
                {
                    return this.loginHandler;
                }

                this.loginHandler = new ResponseHandler(dgram);
                return this.loginHandler;
            }

            // it's a command.
            var cmdDgram = (CommandDatagram)dgram;
            lock (this.cmdResponseHandlers)
            {
                if (this.cmdResponseHandlers.ContainsKey(cmdDgram.SequenceNumber))
                {
                    return this.cmdResponseHandlers[cmdDgram.SequenceNumber];
                }

                var newHandler = new ResponseHandler(dgram);
                this.cmdResponseHandlers.Add(cmdDgram.SequenceNumber, newHandler);
                return newHandler;
            }
        }
Beispiel #2
0
        public ResponseHandler SendDatagram(IOutboundDatagram dgram)
        {
            // this.outboundQueue.Enqueue(dgram);
            byte[] bytes = dgram.Build();

            ResponseHandler handler = null;
            if (dgram.ExpectsResponse)
            {
                handler = this.responseDispatcher.CreateOrGetHandler(dgram);
            }

            this.Log.Trace("BEFORE SendDatagram");

            // socket is thread safe
            // i.e. it is ok to send & receive at the same time from different threads
            // also, there's no UDP ack, so there's no need to send asynchronously
            int transferredBytes = this.udpClient.Send(bytes, bytes.Length);

            this.Log.Trace("AFTER  SendDatagram");

            dgram.SentTime = DateTime.Now;
            this.Metrics.OutboundDatagramCount++;

            if (dgram.Type == DatagramType.Command)
            {
                this.lastCommandSequenceNumber = this.lastCommandSequenceNumber + 1;
            }

            Debug.Assert(
                         transferredBytes == bytes.Length,
                         "Sent bytes count equal count of bytes meant to be sent.");

            if (dgram.Type == DatagramType.Command)
            {
                this.LastCommandSentTime = DateTime.Now;
            }

            return handler;
        }
Beispiel #3
0
 private void RegisterAcknowledgedDatagram(IOutboundDatagram acknowledgedDatagram)
 {
     var sentTime = acknowledgedDatagram.SentTime;
     if (sentTime > this.LastAcknowledgedDatagramSentTime)
     {
         this.LastAcknowledgedDatagramSentTime = sentTime;
     }
 }
Beispiel #4
0
 internal ResponseHandler SendDatagram(IOutboundDatagram dgram)
 {
     return this.datagramSender.SendDatagram(dgram);
 }
Beispiel #5
0
 /// <summary>
 ///   Creates a new instance of <see cref="ResponseHandler" /> for
 ///   the specified sent datagram and timeout in milliseconds.
 /// </summary>
 /// <param name="sentDatagram"> The sent datagram of which a response is awaited. </param>
 internal ResponseHandler(IOutboundDatagram sentDatagram)
 {
     this.SentDatagram = sentDatagram;
     this.log = LogManager.GetLogger(this.GetType());
 }