public static void Flush() { Servers.Clear(); NetData n = new NetData(NetMessageType.GainServer); NetUdp.SendBroad(n); }
public static void ConnectToServer(IPEndPoint ip, string password = null) { if (Status != NetStatus.Null) { Debug.LogError("请先退出Net状态:" + Status); return; } NetData n = new NetData(NetMessageType.Connecting); n.Write(PlayerName); if (password != null) { n.Write(password); } NetUdp.Send(n, ip); }
private void OnApplicationQuit() { switch (Net.Status) { case Corsair.NetStatus.Server: NetServer.ShutDown(); break; case Corsair.NetStatus.Client: NetClient.ShutDown(); break; case Corsair.NetStatus.Null: break; } NetUdp.ShutDown(); Net.NetDataMgr.Enable = false; }
public static void Send(NetData data, NetType t = NetType.TCP) { if (info != null) { switch (t) { case NetType.TCP: info.Send(data.ToBuffer()); break; case NetType.UDP: NetUdp.Send(data.ToBuffer(), info.ip); break; } } else { Debug.LogWarning("未连接tcp服务器"); } }
public static void SendTo(NetData data, IPEndPoint ip, NetType t = NetType.TCP) { if (clients.ContainsKey(ip)) { switch (t) { case NetType.TCP: clients[ip].Send(data.ToBuffer()); break; case NetType.UDP: NetUdp.Send(data.ToBuffer(), ip); break; } } else { Debug.LogError(ip + "未与本机建立连接"); } }
//public static void Send(NetData data, NetType t = NetType.TCP) //{ // if (socket != null) // { // byte[] b = data.ToBuffer(); // if (LogEnable) // Debug.Log("发送数据:" + b.Length); // { // switch (t) // { // case NetType.TCP: // foreach (var c in clients.Values) // c.Send(b); // break; // case NetType.UDP: // foreach (var c in clients) // NetUdp.Send(b, c.Key); // break; // } // } // } // else // { // Debug.LogError("未开启tcp服务器"); // } //} public static void Send(NetData data, NetType t = NetType.TCP, IPEndPoint ignore = null) { if (socket != null) { byte[] b = data.ToBuffer(); if (LogEnable) { Debug.Log("发送数据:" + b.Length); } { switch (t) { case NetType.TCP: foreach (var c in clients) { if (ignore == null || c.Key.ToString() != ignore.ToString()) { c.Value.Send(b); } } break; case NetType.UDP: foreach (var c in clients) { if (ignore == null || c.Key.ToString() != ignore.ToString()) { NetUdp.Send(b, c.Key); } } break; } } } else { Debug.LogError("未开启tcp服务器"); } }
private void Update() { while (NetDatas.Count > 0) { NetData n = NetDatas[0]; try { switch (n.MessageType) { // 1 下线警告 case NetMessageType.Warning: Debug.LogWarning(n.ReadString()); break; case NetMessageType.Error: Debug.LogError(n.ReadString()); break; case NetMessageType.Heartbeat: int d = DateTime.Now.Millisecond - n.ReadInt(); Debug.Log(n.RemoteIP + "延迟:" + d); break; case NetMessageType.Data: break; case NetMessageType.GainServer: if (Net.Status == NetStatus.Server) { NetData g = new NetData(NetMessageType.SendServer); g.Write(NetServer.ServerName); g.Write(NetServer.PlayerName); g.Write(NetServer.IsPassword); g.Write(NetServer.ClientNumber); g.Write(NetServer.ClientMax); NetUdp.Send(g, n.RemoteIP); } break; case NetMessageType.SendServer: string ssn = n.ReadString(); string spn = n.ReadString(); IPEndPoint sip = n.RemoteIP; bool spd = n.ReadBool(); int scn = n.ReadInt(); int scm = n.ReadInt(); NetClient.Servers.Add(new NetServer.Info(ssn, spn, sip, spd, scn, scm)); break; case NetMessageType.Connecting: if (Net.Status == NetStatus.Server) { if (NetServer.ClientNumber >= NetServer.ClientMax) { NetData cw0 = new NetData(NetMessageType.Warning); cw0.Write("该服务器玩家数已满!"); NetUdp.Send(cw0, n.RemoteIP); } string cn = n.ReadString(); if (NetServer.IsPassword) { if (!n.IsRead()) { NetData cw1 = new NetData(NetMessageType.Warning); cw1.Write("该服务器需要密码访问!"); NetUdp.Send(cw1, n.RemoteIP); break; } string cp = n.ReadString(); if (NetServer.Password != cp) { NetData cw2 = new NetData(NetMessageType.Warning); cw2.Write("该服务器访问密码错误!"); NetUdp.Send(cw2, n.RemoteIP); break; } } if (NetServer.Clients.ContainsIP(n.RemoteIP)) { NetData cw3 = new NetData(NetMessageType.Warning); cw3.Write("请勿重复登录!"); NetUdp.Send(cw3, n.RemoteIP); break; } NetServer.Clients.Add(new NetClient.Info(cn, n.RemoteIP)); NetData cw = new NetData(NetMessageType.ConnectPass); NetUdp.Send(cw, n.RemoteIP); } break; case NetMessageType.ConnectPass: NetClient.Connect(n.RemoteIP); break; default: Debug.Log("未知数据包,来自:" + n.RemoteIP); break; } n.Reset(); if (NetDataEvent != null) { NetDataEvent(n); } NetDatas.RemoveAt(0); } catch (Exception e) { Debug.LogError("数据包解析错误:" + e.Message.ToString()); NetDatas.RemoveAt(0); } } }
public static void AutoNet() { NetUdp.Listen(); Instance.StartCoroutine(AutoNetCor()); }