示例#1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            TcpAdapterEndPoint endPoint = null;

            try
            {
                endPoint = (TcpAdapterEndPoint)ar.AsyncState;

                Socket clientSocket = endPoint.Socket;

                int byteCount = clientSocket.EndReceive(ar);

                if (byteCount > 0)
                {
                    MultiPartMessage msg = new MultiPartMessage("",
                                                                new MemoryStream(endPoint.buffer, 0, byteCount));

                    OnMessageReceived(msg, endPoint);

                    InitializeEndPoint(endPoint);
                }
                else
                {
                    Disconnect(endPoint);
                }
            }
            catch (Exception ex)
            {
                Disconnect(endPoint, ex);
            }
        }
示例#2
0
        private TcpAdapterEndPoint CreateEndPoint(Socket socket)
        {
            TcpAdapterEndPoint endPoint = new TcpAdapterEndPoint(this, socket);

            try
            {
                Uri uri = new UriBuilder("tcp", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(),
                                         ((IPEndPoint)socket.RemoteEndPoint).Port).Uri;

                TcpAdapterEndPoint existingEndPoint = MessageEngine.Instance.AdapterProxy.ResolveUriToEndPoint(uri) as TcpAdapterEndPoint;

                if (existingEndPoint != null)
                {
                    Disconnect(existingEndPoint);
                }

                OnEndPointCreated(endPoint);

                InitializeEndPoint(endPoint);
            }
            catch (Exception)
            {
                Disconnect(endPoint);
                throw;
            }

            return(endPoint);
        }
示例#3
0
        private void InitializeEndPoint(TcpAdapterEndPoint endPoint)
        {
            int timeout = Configuration.ReadAsInt("TcpEndPointTimeout");

            if (timeout > 0)
            {
                SetEndPointTimeout(endPoint, DateTime.Now.AddMilliseconds(timeout));
            }

            endPoint.Socket.BeginReceive(endPoint.buffer, 0, TcpAdapterEndPoint.bufferSize, 0, ReceiveCallback, endPoint);
        }
示例#4
0
        public void SetEndPointTimeout(TcpAdapterEndPoint endPoint, DateTime timeout)
        {
            lock (endPoint)
            {
                endPoint.Timeout = timeout;
            }

            lock (_syncObject)
            {
                if (timeout < _currentTimeout)
                {
                    _timeoutWaitEvent.Set();
                }
            }
        }
示例#5
0
        private void OnEndpointTimeout(TcpAdapterEndPoint endPoint)
        {
            //Mark as timed out
            lock (endPoint)
            {
                endPoint.Timeout = DateTime.MaxValue;
            }

            Disconnect(endPoint);

            if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Warning) == SourceLevels.Warning)
            {
                MessageEngine.Instance.Tracing.TraceEvent(TraceEventType.Warning, 0, "EndPoint: \"{0}\" timed out due to inactivity.", endPoint);
            }
        }
示例#6
0
 private void Disconnect(TcpAdapterEndPoint endPoint, Exception exception)
 {
     if (endPoint != null)
     {
         try
         {
             endPoint.Socket.Close();
         }
         catch (SocketException)
         {
         }
         catch (ObjectDisposedException)
         {
         }
         finally
         {
             endPoint.Exception = exception;
             OnEndPointDestroyed(endPoint);
         }
     }
 }
示例#7
0
        /// <summary>
        /// Transmits a message over the adapter protocol.
        /// </summary>
        /// <param name="msg">The message to transmit.</param>
        public override void TransmitMessage(MultiPartMessage msg)
        {
            Uri sendUri = (Uri)msg.Metadata.Read("SendUri");

            TcpAdapterEndPoint endPoint
                = (TcpAdapterEndPoint)MessageEngine.Instance.AdapterProxy.ResolveUriToEndPoint(sendUri);

            if (endPoint == null)
            {
                throw new AdapterException("Failed to transmit message to Uri: \"" + sendUri + "\". The EndPoint does not exist.");
            }

            try
            {
                byte[] buffer = new byte[msg.Data.Length];
                msg.Data.Seek(0, SeekOrigin.Begin);
                msg.Data.Read(buffer, 0, (int)msg.Data.Length);

                endPoint.Socket.Send(buffer, 0, (int)msg.Data.Length, SocketFlags.None);
            }
            catch (NullReferenceException ex)
            {
                throw new AdapterException("Failed to transmit message to EndPoint: \"" + endPoint + "\".", ex);
            }
            catch (ArgumentNullException ex)
            {
                throw new AdapterException("Failed to transmit message to EndPoint: \"" + endPoint + "\".", ex);
            }
            catch (SocketException ex)
            {
                Disconnect(endPoint, ex);
                throw new AdapterException("Failed to transmit message to EndPoint: \"" + endPoint + "\".", ex);
            }
            catch (ObjectDisposedException ex)
            {
                //The socket has been closed by another thread
                Disconnect(endPoint, ex);
                throw new AdapterException("Failed to transmit message to EndPoint: \"" + endPoint + "\".", ex);
            }
        }
示例#8
0
 public void Disconnect(TcpAdapterEndPoint endPoint)
 {
     Disconnect(endPoint, null);
 }