public static void Init() { string myIp = "127.0.0.1"; ushort myProt = 40000; m_bDestory = false; InitBytePool(); m_Net = new KCP.Server.KCPServer(myIp, myProt); m_Net.NewClientSession += server_NewClientSession; m_Net.CloseClientSession += server_CloseClientSession; m_Net.RecvData += server_RecvData; for (int i = 1; i < 10000; i++) { m_Net.AddClientKey((uint)i, i); } PacketRegister.Init(null); m_Net.Start(); Log.print("启动监听{0}:{1}成功", myIp, myProt); while (true) { m_Net.Update(); } }
/// <summary> /// 监听客户端连接 /// </summary> private static void ListenClientConnect() { while (m_ListenClientConnectThread.IsAlive) { Socket clientSocket = serverSocket.Accept(); Log.print("有客户端{0}连接进来了", clientSocket.RemoteEndPoint.ToString()); //初始化新建立连接的数据 DP_Msg clientData = new DP_Msg(); clientData.clientSocket = clientSocket; clientData.m_LastConnTime = Environment.TickCount; Log.print("连接的TickCount:" + Environment.TickCount); PacketRegister.Init(clientData); Thread m_ReceiveMessageThread = new Thread(ReceiveMessage); Thread m_SendMessageThread = new Thread(SendMessage); Thread m_ProcessMessageThread = new Thread(ProecssMessage); clientData.m_ProcessMessageThread = m_ProcessMessageThread; clientData.m_SendMessageThread = m_SendMessageThread; clientData.m_ReceiveMessageThread = m_ReceiveMessageThread; Thread m_TestConnectionThread = new Thread(TestConnection); m_TestConnectionThread.Start(clientData); m_SendMessageThread.Start(clientData); m_ProcessMessageThread.Start(clientData); m_ReceiveMessageThread.Start(clientData); //向客户端发送一个HelloWorld if (Program.m_MainLoop.m_UseTcp) { TestTcp.SendMsg(clientData); } else { TestUdp.SendMsg(clientData); } } }