Exemplo n.º 1
0
//
//        void EndConnect(IAsyncResult iar)
//        {
//            Socket handler = (Socket)iar.AsyncState;
//            try
//            {
//                handler.EndConnect(iar);
//                times = 0;
//				m_state = SocketArgsStats.CONNECTING;
//            }
//            catch (Exception e)
//            {
//                times++;
//				Reconnect();
//                LogMgr.LogError(e);
//            }
//
//        }

		public void CloseConnect ()
		{
			try {
				if (m_socket.Connected) {
					m_state = SocketArgsStats.UNCONNECT;
					m_socket.Shutdown (SocketShutdown.Both);

					m_socket.Close ();

				}
			} catch (Exception ex) {
				LogMgr.LogError ("CloseConnect " + ex);
				CloseConnect ();
			} finally {
				m_state = SocketArgsStats.UNCONNECT;
			}
		}
Exemplo n.º 2
0
		public void SetStateFree ()
		{
			lock (m_lock) {
				m_state = SocketArgsStats.FREE;
			}

		}
Exemplo n.º 3
0
		public bool ReceiveMessageFromAsync (SocketAsyncEventArgs args, Action<SocketAsyncEventArgs> ev)
		{
			try {
				bool ret = false;
				if (isAviliable ()) {
					ret = m_socket.ReceiveMessageFromAsync (args);
					m_state = SocketArgsStats.RECEIVE;
					if (!ret) {
						m_state = SocketArgsStats.FREE;
						ev (args);
					}
					return true;
				} else {
					LogMgr.LogError ("  Socket Is Null or Error");
					m_state = SocketArgsStats.ERROR;
					return false;
				}
			} catch (SocketException ex) {
				LogMgr.LogError (ex);
				m_state = SocketArgsStats.ERROR;
				Reconnect ();
				return false;
			}

		}
Exemplo n.º 4
0
		public void Reconnect (Action<bool> callback = null)
		{

			try {
//				LogMgr.Log("cur Connected = "+m_socket.Connected );
				if (times > Config.mIns.Retry_Times) {
					LogMgr.LogError ("Reconnect Failed ");
					CloseConnect ();
					if (callback != null) {
						callback (false);
					}
				} else if (m_socket != null && !m_socket.Connected && m_InitData.RemoteEndPoint != null) {
					m_socket = new Socket (m_InitData.addressFamily, m_InitData.socketType, m_InitData.protocolTtype);
					
					m_socket.BeginConnect (m_InitData.RemoteEndPoint, (IAsyncResult iar) =>
					{
						Socket handler = (Socket)iar.AsyncState;
						try {
							handler.EndConnect (iar);
							times = 0;
							m_state = SocketArgsStats.CONNECTING;
							if (callback != null) {
								callback (true);
							}
						} catch (Exception e) {
							times++;
							Reconnect ();
							LogMgr.LogError (e);
						}
					}, m_socket);
				} else {
					if (callback != null) {
						callback (false);
					}
				}
				
			} catch (Exception ex) {
				times++;

				if (times > Config.mIns.Retry_Times) {
					LogMgr.LogError ("Reconnect Failed " + ex);
					CloseConnect ();
				} else
					Reconnect ();
			}
		}
Exemplo n.º 5
0
		public bool ConnectAsync (SocketAsyncEventArgs args, Action<SocketAsyncEventArgs> ev)
		{
			try {
				bool ret = false;
				if (isAviliable ()) {
					args.RemoteEndPoint = m_InitData.RemoteEndPoint;
					ret = m_socket.ConnectAsync (args);
					m_state = SocketArgsStats.CONNECTING;
					if (!ret) {
						m_state = SocketArgsStats.FREE;
						ev (args);
					}
					return true;
				} else {
					LogMgr.LogError ("  Socket Is Null Or Error");
					m_state = SocketArgsStats.ERROR;
					return false;
				}
			} catch (SocketException ex) {
				LogMgr.LogError (ex);
				m_state = SocketArgsStats.ERROR;
				Reconnect ();
				return false;
			}

		}
Exemplo n.º 6
0
		public bool DisconnectAsync (SocketAsyncEventArgs args, Action<SocketAsyncEventArgs> ev)
		{
			try {
				bool ret = false;
				if (isAviliable ()) {
					ret = m_socket.DisconnectAsync (args);
					m_state = SocketArgsStats.UNCONNECT;
					if (!ret) {

						ev (args);
					}
					return true;
				} else {
					LogMgr.LogError ("  Socket Is Null or Error");
					m_state = SocketArgsStats.ERROR;
					return false;
				}
			} catch (SocketException ex) {
				LogMgr.LogError (ex);
				m_state = SocketArgsStats.ERROR;

				return false;
			}

		}
Exemplo n.º 7
0
		public bool AcceptAsync (SocketAsyncEventArgs args, Action<SocketAsyncEventArgs> ev)
		{
			try {
				bool ret = false;
				if (isAviliable ()) {
					ret = m_socket.AcceptAsync (args);
					m_state = SocketArgsStats.ACCEPT;
					if (!ret) {
						m_state = SocketArgsStats.FREE;
						ev (args);
					}
					return true;
				} else {
					LogMgr.LogError ("  Socket Is Null Or ERROR");
					m_state = SocketArgsStats.ERROR;
					return false;
				}
			} catch (SocketException ex) {
				LogMgr.LogError (ex);
				m_state = SocketArgsStats.ERROR;
				Reconnect ();
				return false;
			}

		}
Exemplo n.º 8
0
		public bool TryConnect (SocketAsyncEventArgs args, Action<SocketAsyncEventArgs> ev)
		{
			try {
				bool ret = false;
				if (isAviliable ()) {
					m_state = SocketArgsStats.CONNECTING;
					ret = m_socket.ConnectAsync (args);
					
					if (!ret) {
						m_state = SocketArgsStats.FREE;
						ev (args);
					}
					return true;
				} else {
					Reconnect ();
					return false;
				}
			} catch (SocketException ex) {
				LogMgr.LogError (ex);
				m_state = SocketArgsStats.ERROR;
				Reconnect ();
				return false;
			}
		}
Exemplo n.º 9
0
		public void Reconnect (Action<bool> callback = null,bool force =false)
		{

			if(m_socket != null && !m_socket.Connected && times > Config.mIns.Retry_Times && !force)
			{
				LogMgr.Log("重连失败次数过多,请使用强制重连");
			}
			else
			{
				times =0;
			}

			if (m_socket != null 
			    && m_state == SocketArgsStats.UNCONNECT || m_state == SocketArgsStats.ERROR 
			    && !m_socket.Connected 
			    && m_InitData.RemoteEndPoint != null) 
			{
				LogMgr.Log ("state =" + m_state);
				Action ReconnectWork;

				ReconnectWork =delegate() 
				{
					m_socket = new Socket (m_InitData.addressFamily, m_InitData.socketType, m_InitData.protocolTtype);
					m_socket.BeginConnect (m_InitData.RemoteEndPoint, (IAsyncResult iar) =>
					{
						Socket handler = (Socket)iar.AsyncState;
						handler.EndConnect (iar);
						times = 0;
						m_state = SocketArgsStats.CONNECTING;
						if (callback != null) {
							callback (true);
						}
					}, m_socket);
				};

				try {

					ReconnectWork();

				} catch (Exception ex) {
					times++;
					LogMgr.LogError("rc error times ="+ times);
						
					if (times > Config.mIns.Retry_Times) {
						LogMgr.LogError ("Reconnect Failed " + ex);
						CloseConnect ();
					} 
					else 
						ReconnectWork ();

				}
			} 
			else 
			{
				LogMgr.Log (" false state >> =" + m_state);
				if (callback != null) callback (false);
				
			}
		}
Exemplo n.º 10
0
		public bool ReceiveMessageFromAsync (SocketAsyncEventArgs args, Action<SocketAsyncEventArgs> ev)
		{
			try {
				bool ret = false;
				if (isAviliable ()) {
					args.AcceptSocket = this.m_socket;
					ret = m_socket.ReceiveMessageFromAsync (args);
					m_state = SocketArgsStats.RECEIVE;
					if (!ret) {
						m_state = SocketArgsStats.FREE;
						ev (args);
					}
					return true;
				} else {
					if (m_socket == null)
						LogMgr.LogError ("  Socket Is Null ");
					else
						LogMgr.LogError ("  Socket Error >> Connected =" + m_socket.Connected + " >> May shutdown");
					m_state = SocketArgsStats.ERROR;
					return false;
				}
			} catch (SocketException ex) {
				LogMgr.LogError (ex);
				m_state = SocketArgsStats.ERROR;
				Reconnect ();
				return false;
			}
			
		}
Exemplo n.º 11
0
		public bool DisconnectAsync (SocketAsyncEventArgs args, Action<SocketAsyncEventArgs> ev)
		{
			try {
				bool ret = false;
				if (isAviliable ()) {
					args.AcceptSocket = this.m_socket;
					ret = m_socket.DisconnectAsync (args);
					m_state = SocketArgsStats.UNCONNECT;
					if (!ret) {
						
						ev (args);
					}
					return true;
				} else {
					if (m_socket == null)
						LogMgr.LogError ("  Socket Is Null ");
					else
						LogMgr.LogError ("  Socket Error >> Connected =" + m_socket.Connected + " >> May shutdown");
					m_state = SocketArgsStats.ERROR;
					return false;
				}
			} catch (SocketException ex) {
				LogMgr.LogError (ex);
				m_state = SocketArgsStats.ERROR;
				
				return false;
			}
			
		}