public void PaserCommand(string cmdstring) { cmdstring = cmdstring.Trim(); string[] args = cmdstring.Split(new char[] { ' ' }); if (args.Length > 0) { if (string.Compare(args[0], "exit", true) == 0) { LogoutMessage lgoutMsg = new LogoutMessage(myName); byte[] buffer = FormatterHelper.Serialize(lgoutMsg); client.Send(buffer, buffer.Length, hostPoint); // do clear something here Dispose(); System.Environment.Exit(0); } else if (string.Compare(args[0], "send", true) == 0) { if (args.Length > 2) { string toUserName = args[1]; string message = ""; for (int i = 2; i < args.Length; i++) { if (args[i] == "") { message += " "; } else { message += args[i]; } } if (this.SendMessageTo(toUserName, message)) { Console.WriteLine("Send OK!"); } else { Console.WriteLine("Send to " + toUserName + " Failed!"); } } } else if (string.Compare(args[0], "getu", true) == 0) { ListUserMessage getUserMsg = new ListUserMessage(myName); byte[] buffer = FormatterHelper.Serialize(getUserMsg); client.Send(buffer, buffer.Length, hostPoint); } else { Console.WriteLine("Unknown command {0}", cmdstring); } } }
public void PaserCommand(string cmdstring) { cmdstring = cmdstring.Trim(); string[] args = cmdstring.Split(new char[] { ' ' }); if (args.Length > 0) { if (string.Compare(args[0], "exit", true) == 0) { LogoutMessage lgoutMsg = new LogoutMessage(myName); byte[] buffer = FormatterHelper.Serialize(lgoutMsg); client.Send(buffer, buffer.Length, hostPoint); // do clear something here Dispose(); System.Environment.Exit(0); } else if (string.Compare(args[0], "send", true) == 0) { if (args.Length > 2) { string toUserName = args[1]; string message = ""; for (int i = 2; i < args.Length; i++) { if (args[i] == "") message += " "; else message += args[i]; } if (this.SendMessageTo(toUserName, message)) { Console.WriteLine("Send OK!"); } else { Console.WriteLine("Send to " + toUserName + " Failed!"); } } } else if (string.Compare(args[0], "getu", true) == 0) { ListUserMessage getUserMsg = new ListUserMessage(myName); byte[] buffer = FormatterHelper.Serialize(getUserMsg); client.Send(buffer, buffer.Length, hostPoint); } else { Console.WriteLine("Unknown command {0}", cmdstring); } } }
private async void Run() { byte[] buffer = null; while (true) { byte[] msgBuffer = await server.Receive(remotePoint); try { object msgObj = FormatterHelper.Deserialize(msgBuffer); Type msgType = msgObj.GetType(); if (msgType == typeof(LoginMessage)) { // 转换接受的消息 LoginMessage lginMsg = (LoginMessage)msgObj; Console.WriteLine("{0}: user {1} sign in.", System.DateTime.Now.ToString(), lginMsg.UserName); // 添加用户到列表 IPEndPoint userEndPoint = new IPEndPoint(remotePoint.Address, remotePoint.Port); User user = new User(lginMsg.UserName, userEndPoint); userList.Add(user); // 发送应答消息 ListUserResponseMessage usersMsg = new ListUserResponseMessage(userList); buffer = FormatterHelper.Serialize(usersMsg); server.Send(buffer, buffer.Length, remotePoint); Console.WriteLine("Send:" + usersMsg); } else if (msgType == typeof(LogoutMessage)) { // 转换接受的消息 LogoutMessage lgoutMsg = (LogoutMessage)msgObj; Console.WriteLine("{0}: {1} sign out", System.DateTime.Now.ToString(), lgoutMsg.UserName); // 从列表中删除用户 User lgoutUser = userList.Find(lgoutMsg.UserName); if (lgoutUser != null) { userList.Remove(lgoutUser); } } else if (msgType == typeof(TranslateMessage)) { // 转换接受的消息 TranslateMessage transMsg = (TranslateMessage)msgObj; Console.WriteLine("{0}(1) wants to p2p {2}", remotePoint.Address.ToString(), transMsg.UserName, transMsg.ToUserName); // 获取目标用户 User toUser = userList.Find(transMsg.ToUserName); // 转发Purch Hole请求消息 if (toUser == null) { Console.WriteLine("Remote host {0} cannot be found at index server", transMsg.ToUserName); } else { PingMessage transMsg2 = new PingMessage(remotePoint); buffer = FormatterHelper.Serialize(transMsg); server.Send(buffer, buffer.Length, toUser.NetPoint); Console.WriteLine("Send:" + transMsg2); } } else if (msgType == typeof(ListUserMessage)) { // 发送当前用户信息到所有登录客户 ListUserResponseMessage srvResMsg = new ListUserResponseMessage(userList); buffer = FormatterHelper.Serialize(srvResMsg); foreach (User user in userList.InnerList) { server.Send(buffer, buffer.Length, user.NetPoint); Console.WriteLine("Send:" + srvResMsg); } } Thread.Sleep(500); } catch (Exception e) { System.Console.WriteLine(e.Message); System.Console.WriteLine(e.StackTrace); } } }