(1)logic逻辑层:逻辑处理模块,异步的逻辑处理,登录、用户处理、匹配、选人、战斗的主要逻辑都在这里,Moba类游戏是典型的房间服务器架构,AbsOnceHandler用于单体消息发送的处理,AbsMulitHandler用于群发;
AbsOnceHandler代码如下:
using LOLServer.biz; using LOLServer.dao.model; using NetFrame; using NetFrame.auto; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LOLServer.logic { public class AbsOnceHandler { public IUserBiz userBiz = BizFactory.userBiz; private byte type; private int area; public void SetArea(int area) { this.area = area; } GetArea() { return area; } public void SetType(byte type) { this.type = type; } GetType() { return type; } 通过连接对象获取用户 USER getUser(UserToken token) { return userBiz.get(token); } 通过ID获取用户 USER getUser(int id) { return userBiz.get(id); } 通过连接对象 获取用户ID getUserId(UserToken token){ USER user = getUser(token); if(user==null)return -1; return user.id; } 通过用户ID获取连接 UserToken getToken(int id) { return userBiz.getToken(id); } #region 通过连接对象发送 public void write(UserToken token,int command) { write(token, command, null); } public void write(UserToken token, int command,object message) { write(token,GetArea(), command, message); } public void write(UserToken token,int area, int command, object message) { write(token,GetType(), GetArea(), command, message); } public void write(UserToken token,byte type, int area, int command, object message) { byte[] value = MessageEncoding.encode(CreateSocketModel(type,area,command,message)); value = LengthEncoding.encode(value); token.write(value); } #endregion #region 通过ID发送 public void write(int id, int command) { write(id, command, null); } public void write(int id, int command, object message) { write(id, GetArea(), command, message); } public void write(int id, int area, int command, object message) { write(id, GetType(), area, command, message); } public void write(int id, byte type, int area, int command, object message) { UserToken token= getToken(id); if(token==null)return; write(token, type, area, command, message); } public void writeToUsers(int[] users, byte type, int area, int command, object message) { byte[] value = MessageEncoding.encode(CreateSocketModel(type, area, command, message)); value = LengthEncoding.encode(value); foreach (int item in users) { UserToken token = userBiz.getToken(item); if (token == null) continue; byte[] bs = new byte[value.Length]; Array.Copy(value, 0, bs, 0, value.Length); token.write(bs); } } #endregion public SocketModel CreateSocketModel(byte type, int area, int command, object message) { return new SocketModel(type, area, command, message); } } }
AbsMulitHandler继承自AbsOnceHandler,实现代码如下: