Example #1
0
 private void DetecterCallback(object stateInfo)
 {
     try
     {
         if (Session != null)
         {
             if (Session.Socket == null)
             {
                 Session.Reconnect();
                 return;
             }
             if (!Session.Socket.Connected)
             {
                 if (!Session.IsSocketConnected())
                 {
                     Session.Reconnect();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         EE.LogError("Detecter connection Error", ex, log);
     }
 }
Example #2
0
        void OnSessionDataReceived(object sender, DataEventArgs e)
        {
            ProcessResult result;

            try
            {
                result = PipeLineProcessor.Process(new ArraySegment <byte>(e.Data, e.Offset, e.Length));
            }
            catch (Exception exc)
            {
                OnError(exc);
                m_Session.Close();
                EE.LogError("Receive Data Fail:", exc, log);
                return;
            }

            if (result.State == ProcessState.Error)
            {
                m_Session.Close();
                return;
            }
            else if (result.State == ProcessState.Cached)
            {
                // allocate new receive buffer if the previous one was cached
                var session = m_Session;

                if (session != null)
                {
                    var bufferSetter = session as IBufferSetter;

                    if (bufferSetter != null)
                    {
                        bufferSetter.SetBuffer(new ArraySegment <byte>(new byte[session.ReceiveBufferSize]));
                    }
                }
            }

            if (result.Packages != null && result.Packages.Count > 0)
            {
                foreach (var item in result.Packages)
                {
                    HandlePackage(item);
                }
            }
        }
 /// <summary>
 /// 断线重连函数
 /// </summary>
 /// <returns></returns>
 public override bool Reconnect()
 {
     try
     {
         string remote = "";
         if (m_RemoteEndPoint != null && m_RemoteEndPoint is IPEndPoint ipEndPoint)
         {
             remote = $"{ ipEndPoint.Address}:{ipEndPoint.Port}";
         }
         log.Warn($"Reconnect to {remote}");
         Close();
         Connect(m_RemoteEndPoint);
         ConnectTimeout.Reset();
         if (ConnectTimeout.WaitOne(10000, false))//直到timeout,或者TimeoutObject.se
         {
             return(IsConnected);
         }
     }
     catch (Exception ex)
     {
         EE.LogError("Reconnect Error", ex, log);
     }
     return(false);
 }
        protected void ProcessConnect(Socket socket, object state, SocketAsyncEventArgs e, Exception exception)
        {
            if (exception != null)
            {
                m_InConnecting = false;
                OnError(exception);

                if (e != null)
                {
                    e.Dispose();
                }
                ConnectTimeout.Set();
                return;
            }

            if (e != null && e.SocketError != SocketError.Success)
            {
                m_InConnecting = false;
                OnError(new SocketException((int)e.SocketError));
                e.Dispose();
                ConnectTimeout.Set();
                return;
            }

            if (socket == null)
            {
                m_InConnecting = false;
                OnError(new SocketException((int)SocketError.ConnectionAborted));
                ConnectTimeout.Set();
                return;
            }

            //To walk around a MonoTouch's issue
            //one user reported in some cases the e.SocketError = SocketError.Succes but the socket is not connected in MonoTouch
            if (!socket.Connected)
            {
                m_InConnecting = false;
                ConnectTimeout.Set();
                var socketError = SocketError.HostUnreachable;

#if !SILVERLIGHT && !NETFX_CORE
                try
                {
                    socketError = (SocketError)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
                }
                catch (Exception ex)
                {
                    socketError = SocketError.HostUnreachable;
                    EE.LogError("GetSocketOption Error", ex, log);
                }
#endif
                OnError(new SocketException((int)socketError));
                return;
            }

            if (e == null)
            {
                e = new SocketAsyncEventArgs();
            }

            e.Completed += SocketEventArgsCompleted;

            Client         = socket;
            m_InConnecting = false;

#if !SILVERLIGHT
            try
            {
                // mono may throw an exception here
                LocalEndPoint = socket.LocalEndPoint;
            }
            catch (Exception ex)
            {
                EE.LogError("Get socket.LocalEndPoint Error", ex, log);
            }
#endif

            var finalRemoteEndPoint = e.RemoteEndPoint != null ? e.RemoteEndPoint : socket.RemoteEndPoint;

            if (string.IsNullOrEmpty(HostName))
            {
                HostName = GetHostOfEndPoint(finalRemoteEndPoint);
            }
            else// connect with DnsEndPoint
            {
                var finalDnsEndPoint = finalRemoteEndPoint as DnsEndPoint;

                if (finalDnsEndPoint != null)
                {
                    var hostName = finalDnsEndPoint.Host;

                    if (!string.IsNullOrEmpty(hostName) && !HostName.Equals(hostName, StringComparison.OrdinalIgnoreCase))
                    {
                        HostName = hostName;
                    }
                }
            }

#if !SILVERLIGHT && !NETFX_CORE
            try
            {
                //Set keep alive
                Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                //byte[] inValue = new byte[] { 1, 0, 0, 0, 0x20, 0x4e, 0, 0, 0xd0, 0x07, 0, 0 };// 首次探测时间20 秒, 间隔侦测时间2 秒
                byte[] inValue = new byte[] { 1, 0, 0, 0, 0x88, 0x13, 0, 0, 0xd0, 0x07, 0, 0 };// 首次探测时间5 秒, 间隔侦测时间2 秒
                Client.IOControl(IOControlCode.KeepAliveValues, inValue, null);
            }
            catch (Exception ex)
            {
                EE.LogError("SetSocketOption Error", ex, log);
            }
            ConnectTimeout.Set();
#endif
            OnGetSocket(e);
        }