public GameServer(bool autoConnect, int port, int broadCastPort, string netGroup) { maxPlayer = AppCfg.expose.MaxPlayer; waitConnectTime = AppCfg.expose.WaitConnectTime; maxEnterSceneTime = ServerCfg.MaxEnterSceneTime; host = new TcpHost(); curPlayer = 1; host.SetHost(port, maxPlayer); this.autoConnect = autoConnect; if (autoConnect) { IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ipa in IpEntry.AddressList) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { localIP = ipa.ToString(); break; } } syncIpConnection = new UdpBase(broadCastPort, "SyncIP_Host", false); broadcastSelfData = Encoding.UTF8.GetBytes(Application.version + "|" + localIP.ToString() + "|" + netGroup); broadcastSelf = true; } gameStatus = GameStatus.WaitConnect; startUpTime = Time.time; Debug.LogWarning("部署服务器成功"); }
//同步IP void UpdateSyncIpConnect() { byte[] ipData = null; if (syncIpConnection != null) { ipData = syncIpConnection.GetMsg(); } //接受同步IP if (recvBroadCastIP == true) { if (ipData != null) { string recvIpString = Encoding.UTF8.GetString(ipData); string[] strSplit = recvIpString.Split('|'); if (strSplit.Length == 3) { string version = strSplit[0]; string ip = strSplit[1]; string group = strSplit[2]; if (version == Application.version && netGroup == group) { recvBroadCastIP = false; syncIpConnection.Dispose(); syncIpConnection = null; ConenctTcp(ip); } } } } }
public void Init() { #if UNITY_EDITOR sendSampler = UnityEngine.Profiling.CustomSampler.Create("sendSampler"); recvSampler = UnityEngine.Profiling.CustomSampler.Create("recvSampler"); #endif //*******************获取配置START********************************** if (int.TryParse(AppCfg.expose.UdpPort, out udpPort) == false) { Debug.LogError("UDP端口配置错误"); } if (int.TryParse(AppCfg.expose.TcpPort, out tcpPort) == false) { Debug.LogError("Tcp端口配置错误"); } autoConnect = AppCfg.expose.AutoConnect; netGroup = AppCfg.expose.NetGroup; isHost = AppCfg.expose.IsHost; differentUdpPort = AppCfg.expose.differentUdpPort; Debug.Log("isHost:" + isHost + " autoConnect:" + autoConnect + " netGroup:" + netGroup + " tcpPort:" + tcpPort + " udpPort:" + udpPort); //*******************获取配置END********************************** if (differentUdpPort) { broadCastList = new Dictionary <int, IPEndPoint>(); } //通知逻辑层的事件 eventLib = new NetNotiLib <byte>(); //作为主机 if (isHost) { server = new GameServer(autoConnect, tcpPort, broadCastPort, netGroup); } //通过主机UDP广播自动连接 if (autoConnect) { if (isHost) { ConenctTcp("127.0.0.1"); } else { //建立UDP获取主机广播IP recvBroadCastIP = true; syncIpConnection = new UdpBase(broadCastPort, "SyncIP_Client"); Debug.Log("等待主机发送IP"); } } else { //不自动连接会直接连接配置里的主机IP ConenctTcp(AppCfg.expose.IpAddress); } }
/// <summary> ///同步量大的数据用UDP收发 /// </summary> public void SetUpMutiDataUdp(int id) { if (multidataConnection == null) { int port = udpPort; port = differentUdpPort? udpPort + id:udpPort; multidataConnection = new UdpBase(port, "MultiData", true); Debug.Log("部署同步多数据Udp " + port); } }
public void Init() { if (sendUdp == null) { sendUdp = new UdpBase(33002, "ChairSDKSend", false); recverID = sendUdp.GetRecverID("127.0.0.1"); } if (recvUdp == null) { recvUdp = new UdpBase(33001, "ChairSDKRecv", true); } }
void OnDestroy() { onBtnChange = null; if (sendUdp != null) { sendUdp.Dispose(); sendUdp = null; } if (recvUdp != null) { recvUdp.Dispose(); recvUdp = null; } }
static void Main(string[] args) { //Read config file Config config = ConfigParser.Parse("Config.xml", log); var calculator = new MomentsCalculator(); UdpBase client = new UdpBase(config.Port, AddressFamily.InterNetwork); bool exit = false; Task.Factory.StartNew(() => { while (true) { ConsoleKey key = Console.ReadKey().Key; if (key == ConsoleKey.Q) { exit = true; } if (key == ConsoleKey.Enter) { double[] moments = calculator.GetValues(); Console.WriteLine("Current moments:"); Console.WriteLine(" Mean = " + moments[0]); Console.WriteLine(" SD = " + moments[1]); Console.WriteLine(" Median = " + moments[2]); Console.WriteLine(" Mode = " + moments[3]); } } }); Task.Factory.StartNew(async() => { while (true) { string message = await client.ReceiveAsync(log); calculator.Add(Convert.ToDouble(message)); } }); while (!exit) { ; } }
static void Main(string[] args) { //Read config file Config config = ConfigParser.Parse("Config.xml", log); //Create values generator var valuesGenerator = new ValuesGenerator(config.MinValue, config.MaxValue, 1000); bool exit = false; Task.Factory.StartNew(() => { while (Console.ReadKey().Key != ConsoleKey.Q) { ; } exit = true; }); //create a new server UdpBase server = new UdpBase("localhost", config.Port); log.Info("Start generating numbers"); Console.WriteLine("Start generating numbers"); while (!exit) { double value = valuesGenerator.Next(); Console.WriteLine(value); log.Trace("Generated number: " + value); server.Send(value.ToString(), config.Port, log); //Thread.Sleep(500); } log.Info("Stopped"); Console.WriteLine("Server stopped"); server.Send("Server stopped", config.Port, log); }
public MessageHandlers(UdpBase udpBase) { _mUdpBase = udpBase; }
public void SetUdpBase(UdpBase udpBase) { _udpBase = udpBase; }