public static PlayerCmd Instance(NetNodeInfo player) { PlayerCmd cmd = new PlayerCmd(); cmd.player = player; return(cmd); }
private void HandleSearchMessage(Message message) { Message messageResponse = new Message(MessageTypes.SearchResponse, NetNodeInfo.GetCurrentIP().ToString(), port); Socket socketSetAdress = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(message.ipAddress), message.port); Console.WriteLine(messageResponse.ipAddress); socketSetAdress.SendTo(messageSerializer.Serialize(messageResponse), endPoint); }
public PlayerCmd(byte[] value = null) : base(value) { if (TryValid(value)) { int ID = value[Used] + (value[Used + 1] << 8) + (value[Used + 2] << 16) + (value[Used + 3] << 24); //尝试获取玩家 player = new NetNodeInfo(); player.id = ID; //if (!PlayerManager.Instance.PlayerVerityMapDic.TryGetValue(ID, out player)) //{ // player = PlayerManager.Instance.UnknowPlayer; //} } }
public void UdpBroadcastRequest() { Message message = new Message(MessageTypes.SearchRequest); message.port = ((IPEndPoint)socketUdpHandler.LocalEndPoint).Port; message.ipAddress = NetNodeInfo.GetCurrentIP().ToString(); IPEndPoint IPendPoint = new IPEndPoint(IPAddress.Parse(broadcastIP), 8005); Socket sendRequest = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sendRequest.SendTo(messageSerializer.Serialize(message), IPendPoint); Thread threadReceiveUdp = new Thread(ReceiveMessagesUdp); threadReceiveUdp.Start(); }
public void StartServer() { IPEndPoint endPoint = new IPEndPoint(NetNodeInfo.GetCurrentIP(), port); server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(endPoint); server.Listen(MaxConnectionAmount); Thread handleUDP = new Thread(ListenUDPBroadcast); handleUDP.Start(); while (true) { client = server.Accept(); Connections.Add(id, client); var connectionInfo = new ConnectionInfo(client, id++); Thread thread = new Thread(new ParameterizedThreadStart(RecieveMessage)); thread.Start(connectionInfo); } }
private static byte[] OnConnectInfo(UdpPoint point, IPEndPoint remote, RequestDataHandle handle) { if (handle.data == null || handle.data.Length == 0) { return(null); } byte[] value; //if(ServerCtrl.UniquenessNetNodeRandDic.TryGetValue()) NetNodeInfo info = new NetNodeInfo(); //获取玩家的识别码h int id = handle.data.ToInt(0); //获取玩家名称 string name = handle.data.ToStr(4); //获取延迟 int delay = handle.data.ToInt(name.Length + 8); //获取一个验证值 int license = NodeControl.Instance.NodeLicenseRandom.Next(); //获取一个密码值 int password = ServerCtrl.random.Next(); //为进行连接的玩家创建映射记录 //Console.WriteLine("[名称]" + name + "[ID]" + id + "[许可]" + license + "[延迟]" + delay); info.id = id; info.isValid = false; info.license = license; info.remote = remote; info.name = name; info.password = password; info.delay = delay; info.lastTime = ServerCtrl.TickTime; if (info.message == null) { info.message = new ConcurrentDictionary <NetNodeInfo, List <string> >(); } else { info.message.Clear(); } if (info.reply == null) { info.reply = new ConcurrentDictionary <int, byte[]>(); } else { info.reply.Clear(); } info.removes = new ConcurrentStack <int>(); //添加许可如果许可已经存在则直接覆盖 NodeControl.Instance.NodeLicenseMapDic[license] = info; //回复验证值结构[玩家验证值][连接密码][主机验证值] List <byte> list = new List <byte>(); list.AddRange(license.ToBytes()); list.AddRange(password.ToBytes()); list.AddRange(ServerCtrl.LocalNetNode.license.ToBytes()); value = list.ToArray(); Console.WriteLine("[客户端连接信息]" + remote + "[名称]" + name + "[许可]" + license); return(value); }