Пример #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);
 }