/// <summary> /// 处理请求 /// </summary> /// <param name="request">请求类型</param> /// <param name="action">执行该请求所用的方法</param> /// <param name="pre">该方法的参数</param> /// <param name="client">负责接受数据的客户端</param> public void HeadleRequest(RequestCode request, ActionCode action, string pre, Client client) { BaseController con; if (!controllerDic.TryGetValue(request, out con)) { this.LogError($"获取:{con}错误"); return; } //var merodName = Enum.GetName(typeof(ActionCode), action); //var methodInfo = con.GetType().GetMethod(merodName); //if (methodInfo == null) //{ // this.LogError($"{methodInfo}为 null "); // return; //} //var res = methodInfo.Invoke(con, new object[]{ pre, client, server}); var res = con.ExecuteAction(action, pre, client, server); if (res != null || !string.IsNullOrEmpty(res)) { server.SendRequest(client, action, res); this.Log($"请求: {action.ToString()} 客户端: {client.Name} 数据: {res}"); } }
public void HandleRequest(RequestCode requestCode, ActionCode actionCode, string data, Client client) { BaseController controller; if (controllerDict.TryGetValue(requestCode, out controller) == false) { Console.WriteLine("无法得到" + requestCode + "所对应的Controller,无法处理请求"); return; } string methodName = Enum.GetName(typeof(ActionCode), actionCode); MethodInfo mi = controller.GetType().GetMethod(methodName); if (mi == null) { Console.WriteLine("[警告]在Controller[" + controller.GetType() + "]中没有对应的处理方法:[" + methodName + "]"); return; } object[] parameters = { data, client, server }; object o = mi.Invoke(controller, parameters); Console.WriteLine(requestCode.ToString() + " " + actionCode.ToString()); if (o == null) { return; } server.SendResponse(client, actionCode, o as string); }
/// <summary> /// 解析数据 /// </summary> public void ReadMessage(int newDataAmount, Action <RequestCode, ActionCode, string> processDataCallBack) { startIndex += newDataAmount; while (true) { if (startIndex <= 4)//当存储的数据小于四时说明里面的数据不足,等待下一次补充存储 { return; } int count = BitConverter.ToInt32(data, 0); //将byte数组中的指定位置的四个字节转为int 转出的数字表示收到的消息字节长度 if ((startIndex - 4) >= count) //字节数组中剩余字节大于一次消息传送的字节。也就是字节数组中的数据必须要大于要解析的数据长度 { //string s = Encoding.UTF8.GetString(data, 4, count);//从第四位字节以后开始解析,前四位代表数据长度 //Console.WriteLine("解析出一条数据" + s); RequestCode requestCode = (RequestCode)BitConverter.ToInt32(data, 4); Console.WriteLine(requestCode.ToString()); ActionCode actionCode = (ActionCode)BitConverter.ToInt32(data, 8); Console.WriteLine(actionCode.ToString()); string s = Encoding.UTF8.GetString(data, 12, count - 8); Console.WriteLine(s); processDataCallBack(requestCode, actionCode, s); //Message只负责解析消息,解析后的消息发送给服务端处理,由于Message中么有Server的引用,并且为了降低耦合度,发送消息交给客户端链接去做,所以这里回调调用方法。 Array.Copy(data, count + 4, data, 0, startIndex - 4 - count); //将已经解析过的位置之后的数据移动到首位,移动的数据长度是减去已经解析过的数据。相当于覆盖已经解析过的数据,直到当前读取客户端消息的字节少于等于4也就是字节数组中没有有效数据了。 startIndex -= (count + 4); } else { break; } } }
/// 处理客户端接收到的消息 public void HandleRequest(Client client, RequestCode requestCode, ActionCode actionCode, string data) { //1、取得指定的Controller BaseController baseController; bool isGet = controllerDict.TryGetValue(requestCode, out baseController); if (isGet == false) { Debug.Log(string.Format("无法得到【{0}】所对应的Controller,无法处理请求", requestCode)); return; } //2、利用反射,执行指定名称的方法 string methodName = actionCode.ToString(); MethodInfo methodInfo = baseController.GetType().GetMethod(methodName); if (methodInfo == null) { Debug.Log(string.Format("【警告】在Controller【{0}】没有对应的处理方法【{1}】", baseController.GetType(), methodName)); return; } object[] parameters = { server, client, data }; //构造方法参数 object result = methodInfo.Invoke(baseController, parameters); //执行方法 if (string.IsNullOrEmpty(result.ToString())) { return; } //3、通过server向客户端返回Response server.SendResponse(client, actionCode, result.ToString()); }
private void LoadSprite() { var skillName = m_Skill.ToString(); var image = Resources.Load <Sprite>(skillName); GetComponent <Image>().sprite = image; gameObject.name = skillName; }
public void AppendTableToDataset(DataSet ds2) { DataTable dt1; iTableCount = (iTableCount + 1); dt1 = ds2.Tables[0].Copy(); dt1.TableName = (ActionCode.ToString() + "_" + iTableCount.ToString()); DatasetReturned.Tables.Add(dt1); }
private void CastSkill(ActionCode skill, float durationInSeconds) { m_CastBar.enabled = true; m_CastBar.fillAmount = 0; m_StartCastTime = Time.time; m_CastText.enabled = true; m_CastText.text = skill.ToString(); }
internal void CreateSkillEntity(string caster, string id, ActionCode actionCode, Vector startPosition) { log.InfoFormat("Factory received skill entity creation request with code {0}", actionCode); var name = actionCode.ToString() + id; var stringType = aiEntityNameSpace + actionCode.ToString() + "AI"; var actionType = Type.GetType(stringType); if (actionType == null) { log.ErrorFormat("Type {0} was not found.", stringType); return; } var skillEntity = new SkillEntity(caster, name, startPosition, TeamByEntity(caster), actionCode); World.Instance.AddEntity(skillEntity); Activator.CreateInstance(actionType, skillEntity); }
public void Send(ActionCode actionCode, string data) { try { Console.WriteLine("发出消息" + "," + actionCode.ToString() + "," + data); byte[] bytes = Message.PackData(actionCode, data); clientSocket.Send(bytes); } catch (Exception e) { Console.WriteLine("无法发送消息:" + e); } }
public void SetActionState(ActionCode newState) { m_ActionState = newState; log.DebugFormat("{0}'s actionState changed to {1}", m_Entity.Name, m_ActionState.ToString()); var ev = new ActionStateChangedEvent() { Username = m_Entity.Name, ActionState = (int)m_ActionState }; IEventData evData = new EventData((byte)EventCode.ActionStateUpdate, ev); PublishChange(evData, BroadcastOptions.IgnoreOwner); }
public static byte[] PackData(RequestCode requestData, ActionCode actionCode, string data) { // Debug.LogWarning("Message:"+requestData); // Debug.LogWarning("Message:"+actionCode); // Debug.LogWarning("Message:"+data); // byte[] requestCodeBytes = BitConverter.GetBytes((int)requestData); // Debug.LogWarning(BitConverter.ToString(requestCodeBytes)); // Debug.LogWarning(Encoding.UTF8.GetBytes(BitConverter.ToString(requestCodeBytes)).Length); // byte[] actionCodeBytes = BitConverter.GetBytes((int)actionCode); // byte[] dataBytes = Encoding.UTF8.GetBytes(data); // int dataAmount = requestCodeBytes.Length + dataBytes.Length + actionCodeBytes.Length; // Debug.LogWarning("Length:"+dataAmount); // byte[] dataAmountBytes = BitConverter.GetBytes(dataAmount); //byte[] newBytes = dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>();//Concat(dataBytes); //return newBytes.Concat(dataBytes).ToArray<byte>(); // return dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>() // .Concat(actionCodeBytes).ToArray<byte>() // .Concat(dataBytes).ToArray<byte>(); // string test = "django,12345"; // byte[] datas = Encoding.UTF8.GetBytes(test); // // return datas; // Debug.LogWarning("packData"); // Debug.LogWarning("actionCode--------->"+(int)actionCode); JsonData jsonData = new JsonData(); jsonData["requestCode"] = requestData.ToString(); jsonData["actionCode"] = actionCode.ToString(); jsonData["data"] = data; string datas = jsonData.ToJson(); // Debug.LogWarning("PackedData------>"+datas); byte[] Json = Encoding.UTF8.GetBytes(datas); return(Json); // byte[] test = dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>() // .Concat(actionCodeBytes).ToArray<byte>(); // Debug.LogWarning("---------"+Encoding.UTF8.GetBytes(BitConverter.ToString(test)).Length); // return Encoding.UTF8.GetBytes(BitConverter.ToString(test)); }
/// <summary> /// 解析数据或者叫做读取数据 /// </summary> public void ReadMessage(int dataLength, Action <RequestCode, ActionCode, string> processDataCallback) { tail += dataLength; while (true) { if (tail <= head) { return; //接受数据太小 } //headContent解析出来的大小指的是去除整个协议头部的数据大小 int headContent = BitConverter.ToInt32(data, 0); Console.WriteLine("headContent:" + headContent); int re = BitConverter.ToInt32(data, 4); Console.WriteLine("re:" + re); int ac = BitConverter.ToInt32(data, 8); Console.WriteLine("ac:" + ac); //判断接收到的数据去掉头部是否满足headContent if ((tail - head) >= headContent) { RequestCode request = (RequestCode)re; Console.WriteLine("request:" + request.ToString()); ActionCode action = (ActionCode)ac; Console.WriteLine("action:" + action.ToString()); string s = Encoding.UTF8.GetString(data, head, headContent); Console.WriteLine("s:" + s); //执行回调函数 processDataCallback(request, action, s); Console.WriteLine("回调函数已执行!"); Array.Copy(data, headContent + head, data, 0, tail - head - headContent); tail -= (headContent + head); } //此处条件为发生了分包 else { break; } } }
public ProtocalData(ActionCode code, object @object) : this(code.ToString(), "", @object) { }
private void OnProcessMessage(RequestCode requestCode, ActionCode actionCode, string data) { Console.WriteLine("收到消息" + "," + requestCode.ToString() + "," + actionCode.ToString() + "," + data); server.HandleRequest(requestCode, actionCode, data, this); }
private void ExecuteTheAPI() { switch (ActionCode) { case c.API01_GET_TEETIMES: API01 A1 = new API01(m, this); A1.API01_GET_TEETIMES(); break; case c.API01_GET_SINGLE_TEETIME: API01 A2 = new API01(m, this); A2.API01_GET_SINGLE_TEETIME(); break; case c.API02_ADD_TEETIME: API02 A3 = new API02(m, this); A3.API02_ADD_TEETIME(); break; case c.API03_UPDATE_TT_PLAYER: API03 A4 = new API03(m, this); A4.API03_UPDATE_TT_PLAYER(); break; case c.API03_UPDATE_TT_INFO: API03 A5 = new API03(m, this); A5.API03_UPDATE_TT_INFO(); break; case c.API03_MARK_TT_AS_DELETED: API03 A6 = new API03(m, this); A6.API03_MARK_TT_AS_DELETED(); break; case c.API04_GET_GROUPS: API04 A8 = new API04(m, this); A8.API04_GET_GROUPS(); break; case c.API04_GET_SINGLE_GROUP: API04 A9 = new API04(m, this); A9.API04_GET_SINGLE_GROUP(); break; case c.API04_UPDATE_GROUP: API04 A10 = new API04(m, this); A10.API04_UPDATE_GROUP(); break; case c.API04_DELETE_GROUP: API04 A11 = new API04(m, this); A11.API04_DELETE_GROUP(); break; case c.API04_ADD_GROUP: API04 A12 = new API04(m, this); A12.API04_ADD_GROUP(); break; case c.API05_UPDATE_SETTINGS: API05 A13 = new API05(m, this); A13.API05_UPDATE_SETTINGS(); break; case c.API05_READ_SETTINGS: API05 A14 = new API05(m, this); A14.API05_READ_SETTINGS(); break; case c.API06_GET_MESSAGES_LIST: API06 A15 = new API06(m, this); A15.API06_GET_MESSAGES_LIST(); break; case c.API06_GET_SINGLE_MESSAGE: API06 A16 = new API06(m, this); A16.API06_GET_SINGLE_MESSAGE(); break; case c.API06_ADD_MESSAGE: API06 A17 = new API06(m, this); A17.API06_ADD_MESSAGE(); break; case c.API06_DELETE_MESSAGE: API06 A18 = new API06(m, this); A18.API06_DELETE_MESSAGE(); break; case c.API07_GET_AUDITLOG: API07 A19 = new API07(m, this); A19.API07_GET_AUDITLOG(); break; default: APIErrorMessage = "An invalid API code was called. (" + ActionCode.ToString() + ")"; break; } }