private void ConnectCallback(IAsyncResult ar) { Socket client = (Socket)ar.AsyncState; try { client.EndConnect(ar); } catch (SocketException x) { if (connection != null) { connection.Disconnect(); } if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager :: Excepiton:" + x.GetErrorCode()); } Connecting = false; return; } catch (Exception e) { if (connection != null) { connection.Disconnect(); } if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager :: Excepiton:" + e.ToString()); } Connecting = false; return; } try { connection.SetHandlers(m_MessageHandlers); connection.StartReceiving(client); HandShakeMsg packet = new HandShakeMsg { Version = NetConfig.Version, OP = 0, }; connection.Send(InternalMessages.HeandShake_Server, packet); Connecting = false; Connected = true; m_RecconectTry = 0; } catch (Exception e) { if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager :: Excepiton:" + e.ToString()); } Connecting = false; } }
public void AcceptCallback(IAsyncResult ar) { // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); uint connectionId = IncrementCount(); JHSConnection per = new JHSConnection { connectionId = connectionId }; per.Init(false); per.SetHandlers(m_MessageHandlers); per.StartReceiving(handler); lock (m_Connections) { if (!m_Connections.ContainsKey(connectionId)) { m_Connections.Add(connectionId, per); } } // Signal the main thread to continue. _receiveSocket.BeginAccept(new AsyncCallback(AcceptCallback), _receiveSocket); }