public bool Start() { lock (this) { if (Interlocked.Exchange(ref m_Started, 1) == 1) { return(true); } if (!connectServer()) { Interlocked.Exchange(ref m_Started, 0); return(false); } if (m_Comm == null) { m_ClientID = CommManager.AllocateClientID(); m_Comm = new AsyncCommunicator(this, m_ClientID, m_Client, null, m_Extractor, m_Encoder, m_bCompress); m_Comm.onLog += new LogHandler(m_Comm_onLog); m_Comm.onConnectionBroken += new ConnectionBrokenHandler(m_Comm_onConnectionBroken); } m_Comm.startWork(); if (m_bKeepAlive) { m_NotAliveCounter = 0; this.m_TimerKeepAlive.Start(); } //CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.RESPONSE, this, new Dictionary<Constants.TK_CommandType, byte>()); } return(true); }
private void _accepter() { try { m_AccepterStopEvent.Reset(); while (Interlocked.Read(ref m_Run) == 1) { Socket newclient = m_Listener.Accept();//.AcceptTcpClient(); lock (m_ClientCommunicators) { if (m_ClientCommunicators.Count == m_MaxClients) { byte[] buf = System.Text.Encoding.Default.GetBytes("Client connection exceeds. New request is closing."); try { newclient.Send(buf); newclient.Shutdown(SocketShutdown.Both); newclient.Close(); } catch (Exception ex) { SendLog(ex.ToString()); } continue; } else { try { //IPEndPoint ep = newclient.Client.RemoteEndPoint as IPEndPoint; //byte[] addr = ep.Address.GetAddressBytes(); //long id = addr[3] * 0x10000000000L + // addr[2] * 0x100000000L + // addr[1] * 0x1000000L + // addr[0] * 0x10000L + ep.Port; long id = CommManager.AllocateClientID(); ICommunicator comm = new AsyncCommunicator(this, id, newclient, new INTERPRETER(), new EXTRACTOR(), new ENCODER(), m_bCompress); lock (m_ClientCommunicators) m_ClientCommunicators[id] = comm; lock (m_ClientActiveCounter) m_ClientActiveCounter[id] = 0; //newclient.NoDelay = true; //newclient.LingerState = new LingerOption(false, 0); //newclient.SendBufferSize = 2048; //newclient.SendTimeout = 1000; comm.onLog += new LogHandler(comm_onLog); comm.onConnectionBroken += new ConnectionBrokenHandler(_HandleConnectionBroken); comm.startWork(); // 发送报文注册客户端 MSGTYPE msg = new MSGTYPE(); msg.SeqID = CommandProcessor.AllocateID(); msg.TK_CommandType = Constants.TK_CommandType.REGISTERCLIENT; msg.SetValue("ClientID", id); msg.SetValue("SERVERNAME", Name); msg.SetValue(Constants.MSG_PARANAME_AUTHORIZED, false); CommandProcessor.instance().DispatchCommand(msg); SendLog("客户端 " + newclient.RemoteEndPoint.ToString() + " 连接到:" + Name); SendLog(Name + "当前客户端的数量:" + m_ClientCommunicators.Count); //int wt, iot; //ThreadPool.GetAvailableThreads(out wt, out iot); //SendLog("可用工作线程数: " + wt + " I/O线程数: " + iot); } catch (Exception ex) { try { //NetworkStream ns = newclient.GetStream(); //if (ns != null) // ns.Close(); newclient.Close(); SendLog(ex.ToString()); } catch { } } } } // endlock } } catch (Exception ex) { SendLog(ex.ToString()); } finally { m_AccepterStopEvent.Set(); } }
private void _accepterCallback(IAsyncResult ar) { Socket newclient = m_Listener.EndAccept(ar); lock (m_ClientCommunicators) { if (m_ClientCommunicators.Count == m_MaxClients) { byte[] buf = System.Text.Encoding.Default.GetBytes("Client connection exceeds. New request is closing."); try { newclient.Send(buf); newclient.Shutdown(SocketShutdown.Both); newclient.Close(); } catch (Exception ex) { SendLog(ex.ToString()); } } else { try { long id = CommManager.AllocateClientID(); ICommunicator comm = new AsyncCommunicator(this, id, newclient, new INTERPRETER(), new EXTRACTOR(), new ENCODER(), m_bCompress); lock (m_ClientCommunicators) m_ClientCommunicators[id] = comm; lock (m_ClientActiveCounter) m_ClientActiveCounter[id] = 0; comm.onLog += new LogHandler(comm_onLog); comm.onConnectionBroken += new ConnectionBrokenHandler(_HandleConnectionBroken); comm.startWork(); // 发送报文注册客户端 MSGTYPE msg = new MSGTYPE(); msg.SeqID = CommandProcessor.AllocateID(); msg.TK_CommandType = Constants.TK_CommandType.REGISTERCLIENT; msg.SetValue("ClientID", id); msg.SetValue("SERVERNAME", Name); msg.SetValue(Constants.MSG_PARANAME_AUTHORIZED, false); CommandProcessor.instance().DispatchCommand(msg); SendLog("客户端 " + newclient.RemoteEndPoint.ToString() + " 连接到:" + Name); SendLog(Name + "当前客户端的数量:" + m_ClientCommunicators.Count); //int wt, iot; //ThreadPool.GetAvailableThreads(out wt, out iot); //SendLog("可用工作线程数: " + wt + " I/O线程数: " + iot); } catch (Exception ex) { try { newclient.Close(); SendLog(ex.ToString()); } catch { } } } } // endlock if (Interlocked.Read(ref m_Run) == 1) { _asyncAccepter(); } }