微信登录

Server - Server.cs - 枢纽

中文

  1. class Server
  2. {
  3. private IPEndPoint ipEndPoint;//IP
  4. private Socket serverSocket;//监听、协议
  5. private List<Client> clientList = new List<Client>();//全部客户端
  6. private List<Room> roomList = new List<Room>();
  7. private ControllerManager controllerManager;//控制者分发
  8. //新建一个server
  9. public Server() {}//构造方法
  10. public Server(IP,PORT){}
  11. //设置IP和port
  12. public void SetIpAndPort(IP,PORT){}
  13. //启动监听
  14. public void Start(){}
  15. //异步接收
  16. private void AcceptCallBack(IAsyncResult ar ){}
  17. //_移除断开连线的客户端:被Client调用
  18. public void RemoveClient(客户端){}
  19. //_给客户端发起响应
  20. public void SendResponse(客户端,动作码,数据){}
  21. //传给ControllerManager
  22. public void HandleRequest(RequestCode requestCode, ActionCode actionCode, string data, 客户端){}
  23. //创建房间
  24. public void CreateRoom(客户端){}
  25. //移除房间
  26. public void RemoveRoom(房间){}
  27. //房间列表
  28. public List<Room> GetRoomList(){}
  29. //获得房间号
  30. public Room GetRoomById(用户id){}
  31. }

无代码

  1. class Server
  2. {
  3. private IPEndPoint ipEndPoint;//IP
  4. private Socket serverSocket;//监听、协议
  5. private List<Client> clientList = new List<Client>();//全部客户端
  6. private List<Room> roomList = new List<Room>();
  7. private ControllerManager controllerManager;//控制者分发
  8. //新建一个server
  9. public Server() {}//构造方法
  10. public Server(string ipStr,int port){}
  11. //设置IP和port
  12. public void SetIpAndPort(string ipStr, int port){}
  13. //启动监听
  14. public void Start(){}
  15. //异步接收
  16. private void AcceptCallBack(IAsyncResult ar ){}
  17. //_移除断开连线的客户端:被Client调用
  18. public void RemoveClient(Client client){}
  19. //_给客户端发起响应
  20. public void SendResponse(Client client,ActionCode actionCode,string data){}
  21. //传给ControllerManager
  22. public void HandleRequest(RequestCode requestCode, ActionCode actionCode, string data, Client client){}
  23. //创建房间
  24. public void CreateRoom(Client client){}
  25. //移除房间
  26. public void RemoveRoom(Room room){}
  27. //房间列表
  28. public List<Room> GetRoomList(){}
  29. //获得房间号
  30. public Room GetRoomById(int id){}
  31. }

完整代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Sockets;
  7. using System.Net;
  8. using GameServer.Controller;
  9. using Common;
  10. namespace GameServer.Servers
  11. {
  12. class Server
  13. {
  14. private IPEndPoint ipEndPoint;//IP
  15. private Socket serverSocket;//监听、协议
  16. private List<Client> clientList = new List<Client>();//全部客户端
  17. private List<Room> roomList = new List<Room>();
  18. private ControllerManager controllerManager;//控制者分发
  19. //新建一个server
  20. public Server() {}//构造方法
  21. public Server(string ipStr,int port)
  22. {
  23. controllerManager = new ControllerManager(this);//初始化开启控制器
  24. SetIpAndPort(ipStr, port);
  25. }
  26. //设置IP和port
  27. public void SetIpAndPort(string ipStr, int port)
  28. {
  29. ipEndPoint = new IPEndPoint(IPAddress.Parse(ipStr), port);
  30. }
  31. //启动监听
  32. public void Start()
  33. {
  34. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//启动协议
  35. serverSocket.Bind(ipEndPoint);//绑定IP
  36. serverSocket.Listen(0);//监听,无限个
  37. serverSocket.BeginAccept(AcceptCallBack, null);//异步接收
  38. }
  39. //异步接收
  40. private void AcceptCallBack(IAsyncResult ar )
  41. {
  42. Socket clientSocket = serverSocket.EndAccept(ar);//客户端的连接类
  43. Client client = new Client(clientSocket,this);//新建一个客户端
  44. client.Start();
  45. clientList.Add(client);//列表增加一个当前客户端
  46. serverSocket.BeginAccept(AcceptCallBack, null);
  47. }
  48. //_移除断开连线的客户端:被Client调用
  49. public void RemoveClient(Client client)
  50. {
  51. lock (clientList)//锁
  52. {
  53. clientList.Remove(client);
  54. }
  55. }
  56. //_给客户端发起响应
  57. public void SendResponse(Client client,ActionCode actionCode,string data)
  58. {
  59. client.Send(actionCode, data);
  60. }
  61. //传给ControllerManager
  62. public void HandleRequest(RequestCode requestCode, ActionCode actionCode, string data, Client client)
  63. {
  64. controllerManager.HandleRequest(requestCode, actionCode, data, client);
  65. }
  66. //创建房间
  67. public void CreateRoom(Client client)
  68. {
  69. Room room = new Room(this);
  70. room.AddClient(client);
  71. roomList.Add(room);
  72. }
  73. //移除房间
  74. public void RemoveRoom(Room room)
  75. {
  76. if (roomList != null && room != null)
  77. {
  78. roomList.Remove(room);
  79. }
  80. }
  81. //房间列表
  82. public List<Room> GetRoomList()
  83. {
  84. return roomList;
  85. }
  86. //获得房间号
  87. public Room GetRoomById(int id)
  88. {
  89. foreach(Room room in roomList)
  90. {
  91. if (room.GetId() == id) return room;
  92. }
  93. return null;
  94. }
  95. }
  96. }