コード例 #1
0
        /// <summary>
        /// Transmit message via UDP.
        /// </summary>
        /// <param name="message">Message to send via UDP.</param>
        /// <returns></returns>
        private bool TransmitMessage(NiawaNetDatagram message)
        {
            bool succeeded = false;

            //attempt lock
            bool tryLock = lockSection.WaitOne(60000);
            if (!tryLock) throw new TimeoutException("[" + _description + "-T] Could not obtain lock while sending message");

            try
            {
                Byte[] messageBytes = message.ToByteArray();

                //send message
                /*v1*/
                //_netUdpClient.Connect(_groupEndpoint);
                //_netUdpClient.Send(messageBytes, messageBytes.Length, _localEP);
                //_netUdpClient.Close();
                //_netUdpClient = null;
                //messageInProgressGuid = string.Empty;

                /*v2*/
                //_netUdpClient.Send(messageBytes, messageBytes.Length, _localEP);

                /*v3*/
                if (messageBytes.Length > 65000)
                {
                    //message is too long
                    throw new InvalidOperationException("UdpTransmitter cannot send a message with length greater than 65000 bytes.");
                }
                else
                {

                    bool tryLock2 = lockSentMessageGuidBuffer.WaitOne(5000);
                    if (tryLock2 == false) throw new TimeoutException("Could not obtain lock on Sent Message GUID Buffer while sending message.");
                    try
                    {
                        //add guid to sent buffer
                        _sentMessageGuidBuffer.Add(message.Guid);
                        if (_sentMessageGuidBuffer.Count > 1000) _sentMessageGuidBuffer.RemoveAt(0);
                    }
                    finally
                    {
                        lockSentMessageGuidBuffer.Release();
                    }

                    //send
                    _netUdpClient.Send(messageBytes, messageBytes.Length);
                }

                succeeded = true;

            }
            catch (Exception ex)
            {
                logger.Error("[" + _description + "-M] Error while transmitting message: " + ex.Message, ex);
                throw ex;
            }
            finally
            {
                //release lock
                lockSection.Release();
            }

            logger.Info("[" + _description + "-T] Message sent: Type [" + message.MessageType + "] SerialID [" + message.SerialID + "] Guid [" + message.Guid.ToString() + "]");

            _evtRaiser.RaiseEvent("Message", "Message sent [" + message.SerialID + "]"
                , Niawa.Utilities.InlineSortedListCreator.CreateStrStr("NiawaNetMessage", message.ToJson())
                , _threadStatus.NodeID
                , _threadStatus.ParentNodeID);
            _threadStatus.MessageCount += 1;

            return succeeded;
        }