public static void OnSendMessageReq(Player player, MemoryStream stream) { CSSendMessageReq req = NetworkManager.Deserialize <CSSendMessageReq>(stream); //Console.WriteLine($"OnSendMessageReq,content={req.Content}"); int retCode = player.inRoom ? 0 : -1; if (player.inRoom) { Ultilities.Print(req.Content, player.name); } CSSendMessageRes res = new CSSendMessageRes { RetCode = retCode }; NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_SEND_MESSAGE_RES, res); MessageNotify(player.name, req.Content); }
public static void Start() { string ip = Ultilities.GetIpv4Address(); //string ip = new WebClient().DownloadString("http://icanhazip.com"); //ip = ip.Substring(0,ip.Length-1);//remove the \n character Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port); try { _socket.Bind(endPoint); } catch (Exception ex) { Ultilities.Print("bind socket failed"); Ultilities.Print(ex.ToString()); return; } _socket.Listen(0); Ultilities.Print($"server started, ip = {ip}, port = {port}"); Thread sendThread = new Thread(Send); sendThread.Start(); while (true) { Socket client = _socket.Accept(); Player player = new Player(); player.socket = client; Ultilities.Print($"{player.socket.RemoteEndPoint} connected"); Thread receiveThread = new Thread(Receive); receiveThread.Start(player); } }
public static void OnLoginReq(Player player, MemoryStream stream) { CSLoginReq req = NetworkManager.Deserialize <CSLoginReq>(stream); Ultilities.Print($"CSLoginReq,account={req.Account}"); //检测是否已注册 bool bAccountExist = Redis.GetAccountData(req.Account, out AccountData accountData); //Ultilities.Print($"bAccountExist={bAccountExist}"); if (!bAccountExist) { CSLoginRes res = new CSLoginRes { RetCode = 6 }; NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res); } else { //检测是否已登录,防止顶号 bool hasLoggedIn = players.ContainsKey(accountData.playerID); //Ultilities.Print($"hasLoggedIn={hasLoggedIn}"); if (hasLoggedIn) { CSLoginRes res = new CSLoginRes { RetCode = 5 }; NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res); } else { //检查密码是否正确 bool bCorrect = req.Password.Equals(accountData.password); //Ultilities.Print($"req.Password={req.Password},accountData.password={accountData.password},bCorrect={bCorrect}"); if (!bCorrect) { CSLoginRes res = new CSLoginRes { RetCode = 7 }; NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res); } else { bool hasData = Redis.GetPlayerData(accountData.playerID, out CSPlayer playerData); //Ultilities.Print($"hasData={hasData}"); if (!hasData) { //如果是第一次登陆,则写数据库 playerData = new CSPlayer { PlayerID = accountData.playerID, Name = accountData.name, Position = new CSVector3 { x = 0, y = 30, z = 0 }, Rotation = new CSVector3 { x = 0, y = 0, z = 0 } }; Redis.SetPlayerData(playerData.PlayerID, playerData); } AddPlayer(player, playerData); //Ultilities.Print($"playerData,playerID={accountData.playerID},account={accountData.account},password={accountData.password},name={accountData.name}"); //Ultilities.Print($"player,id={player.id},name={player.name}"); CSLoginRes res = new CSLoginRes { RetCode = 0, PlayerData = playerData }; NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res); MessageNotify("system", "player " + player.name + " has logged in, current number of player : " + players.Count); } } } }
static void PlayerDisconnect(Player player) { Ultilities.Print($"player {player.name} disconnected"); player.socket.Close(); GameLogic.RemovePlayer(player); }