public void StartConnect() { IPAddress ipAddress = IPAddress.Parse(IP); remoteEP = new IPEndPoint(ipAddress, Port); if (connection != null && !connection.m_Disposed) { connection.Dispose(); connection = null; } connection = new JHSConnection(); connection.Init(true); if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager Connecting to Server reconnect attmpt:" + m_RecconectTry); } connection.m_socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { Blocking = false }; connection.m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 1048576); connection.m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1048576); connection.m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, 1); connection.m_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 0); connection.m_socket.BeginConnect(remoteEP, ConnectCallback, connection.m_socket); Connecting = true; PermaDisconnected = 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); }