public int res_role_list(Bit_Buffer buffer) { int length = (int)buffer.read_uint(3); for (int i = 0; i < length; ++i) { Role_Info role_info; role_info.role_id = buffer.read_int64(); role_info.role_name = buffer.read_string(); role_info.gender = (byte)buffer.read_uint(1); role_info.career = (byte)buffer.read_uint(2); role_info.level = (byte)buffer.read_uint(8); role_info.combat = buffer.read_uint(32); role_list_.Add(role_info); } if (length > 0) { Log.debug_log("账号名:" + account_ + "角色数量:" + length.ToString()); Random random = new Random(); int index = random.Next(0, length - 1); long role_id = role_list_[index].role_id; req_enter_game(role_id); } else { req_create_role(); } return(0); }
public void req_client_msg(int seq) { try { if (!end_point.connect_status) { return; } JObject obj = Msg_Parse.get_cmd_jsonobj(seq); uint msg_id = uint.Parse(obj["msg_id"].ToString()); if (msg_id < 5) { player_log("小于5的命令号是系统命令号"); return; } Bit_Buffer buffer = new Bit_Buffer(); Msg_Struct msg = Struct_Manager.instance.get_send_msg_struct((int)msg_id); msg.scan(buffer, obj); if (-1 == msg.scan(buffer, obj) || msg == null) { throw new Exception("命令参数错误"); } end_point_.send_to_server(msg_id, buffer); } catch (Exception ex) { Log.debug_log(ex.Message); } }
public void req_select_gate() { Bit_Buffer buffer = new Bit_Buffer(); buffer.write_string(account_); end_point_.send_to_server(Msg.REQ_SELECT_GATE, buffer); }
public int req_role_list() { Bit_Buffer buffer = new Bit_Buffer(); buffer.write_string(account_); end_point_.send_to_server(Msg.REQ_ROLE_LIST, buffer); return(0); }
int req_enter_game(long role_id) { Bit_Buffer buffer = new Bit_Buffer(); buffer.write_int64(role_id); end_point_.send_to_server(Msg.REQ_ENTER_GAME, buffer); return(0); }
public int req_connect_gate() { Bit_Buffer buffer = new Bit_Buffer(); buffer.write_string(account_); buffer.write_string(token_); end_point_.send_to_server(Msg.REQ_CONNECT_GATE, buffer); return(0); }
public void send_to_server(uint cmd_id, Bit_Buffer buffer) { Byte_Buffer buf = new Byte_Buffer(); buf.make_tcp_begin(); buf.write_uint8(cmd_id); buf.copy(buffer); buf.make_tcp_end(); send(buf); }
public string print_msg(Bit_Buffer buffer) { string msg = get_print_msg(field_list, buffer); if (msg != "") { msg = msg.Substring(0, msg.Length - 2); } return("\r\n" + struct_name + ":{\r\n" + msg + "\r\n}"); }
private int set_struct_buffer(Field_Info info, Bit_Buffer buffer, JObject jobject) { Msg_Struct msg = Struct_Manager.instance.get_msg_struct(info.field_type); if (msg == null) { return(-1); } set_msg_buffer(msg.field_list, buffer, jobject); return(0); }
public int res_server_msg(uint msg_id, Bit_Buffer buffer) { Msg_Struct msg = Struct_Manager.instance.get_recv_msg_struct((int)msg_id); if (msg == null) { return(0); } player_log(msg.print_msg(buffer)); return(0); }
public int req_create_role() { Random ran = new Random(); Bit_Buffer buffer = new Bit_Buffer(); buffer.write_string(account_); buffer.write_string(account_); buffer.write_uint((uint)ran.Next(0, 2), 1); buffer.write_uint((uint)ran.Next(0, 3), 2); end_point_.send_to_server(Msg.REQ_CREATE_ROLE, buffer); return(0); }
public int scan(Bit_Buffer buffer, JObject jobject) { try { return(set_msg_buffer(field_list, buffer, jobject)); } catch (Exception ex) { Log.debug_log(ex.Message); return(-1); } }
/// <summary> /// 客户端请求消息 /// </summary> public void req_heartbeat(long tick) { if (robot_status_ != 2 || !end_point_.connect_status) { return; } Bit_Buffer buffer = new Bit_Buffer(); buffer.write_int((int)(tick / 1000), 32); end_point_.send_to_server(Msg.REQ_HEARTBEAT, buffer); player_log("同步心跳至服务器"); }
private int set_msg_buffer(List <Field_Info> info_list, Bit_Buffer buffer, JObject jobject) { foreach (Field_Info info in info_list) { if (info.field_label == "arg") { JValue obj = (JValue)jobject[info.field_name]; set_arg_buffer(info, buffer, obj); } else if (info.field_label == "vector") { JArray jarray = (JArray)jobject[info.field_name]; set_vector_buffer(info, buffer, jarray); } else if (info.field_label == "struct") { JObject obj = (JObject)jobject[info.field_name]; set_struct_buffer(info, buffer, obj); } else if (info.field_label == "if") { buffer.write_bool(true); int ret = set_msg_buffer(info.field_list, buffer, jobject); if (ret < 0) { return(ret); } } else if (info.field_label == "switch") { uint case_val = uint.Parse(jobject[info.field_name].ToString()); buffer.write_uint(case_val, info.field_bit); foreach (Field_Info sub_info in info.field_list) { if (sub_info.field_label == "case" && sub_info.case_val == case_val) { //找到对应的case标签,对case标签内的child数组进行build int ret = set_msg_buffer(sub_info.field_list, buffer, jobject); if (ret < 0) { return(ret); } break; } } } } return(0); }
public int res_error_code(Bit_Buffer buffer) { uint code = buffer.read_uint(16); string err_msg = Error_Code.message(code); if (err_msg != "") { player_log("服务器返回错误消息:" + err_msg); } else { player_log("服务器返回错误代码:" + code.ToString()); } return(0); }
static public void parse_cmd_and_send_buffer(string cmd) { Bit_Buffer buffer = new Bit_Buffer(); try { JObject jsonobj = null; Match match = Regex.Match(cmd, cmd_input_pattern_); string cmd_string = match.Groups[0].ToString(); if (cmd_string != "") { string[] cmd_str = cmd_string.Split('@'); int cmd_seq = int.Parse(cmd_str[1]); Log.debug_log("脚本命令序列:" + cmd_seq.ToString() + " => " + Msg_Parse.parse_cmd_string(cmd_list_[cmd_seq])); Game_Manager.instance.send_to_server(cmd_seq); return; } else if (Regex.IsMatch(cmd, cmd_input_pattern2)) { int cmd_seq = int.Parse(cmd); Log.debug_log("脚本命令序列:" + cmd_seq.ToString() + " => " + Msg_Parse.parse_cmd_string(cmd_list_[cmd_seq])); Game_Manager.instance.send_to_server(cmd_seq); return; } else { string real_cmd = Msg_Parse.parse_cmd_string(cmd); jsonobj = (JObject)JsonConvert.DeserializeObject(real_cmd); } uint msg_id = uint.Parse(jsonobj["msg_id"].ToString()); if (msg_id < 5) { Log.debug_log("小于5的命令号是系统命令号"); return; } Msg_Struct msg = Struct_Manager.instance.get_send_msg_struct((int)msg_id); if (-1 == msg.scan(buffer, jsonobj) || msg == null) { throw new Exception("命令参数错误"); } Game_Manager.instance.send_to_server(msg_id, buffer); } catch (Exception ex) { Log.debug_log(ex.Message); } }
private string get_print_msg(List <Field_Info> info_list, Bit_Buffer buffer) { string ret = ""; foreach (Field_Info info in info_list) { if (info.field_label == "arg") { string value = get_arg_string(info, buffer); ret += value; } else if (info.field_label == "vector") { string value = get_vector_string(info, buffer); ret += value; } else if (info.field_label == "struct") { string value = get_struct_string(info, buffer); ret += value; } else if (info.field_label == "if") { if (buffer.read_bits_available() >= 1 && buffer.read_bool()) { ret += get_print_msg(info.field_list, buffer); } } else if (info.field_label == "switch") { if (buffer.read_bits_available() >= info.field_bit) { uint case_val = buffer.read_uint(info.field_bit); foreach (Field_Info swinfo in info.field_list) { if (swinfo.field_label == "case" && swinfo.case_val == case_val) { //找到对应的case标签,对case标签内的child数组进行build ret += get_print_msg(swinfo.field_list, buffer); break; } } } } } return(ret); }
private string get_arg_string(Field_Info info, Bit_Buffer buffer, bool from_vector = false) { string ret = ""; if (!from_vector) { ret = (info.field_name + ":"); } if (info.field_type == "int") { int val = buffer.read_int(info.field_bit); ret += val.ToString(); } else if (info.field_type == "uint") { uint val = buffer.read_uint(info.field_bit); ret += val.ToString(); } else if (info.field_type == "int64") { long val = buffer.read_int64(); ret += val.ToString(); } else if (info.field_type == "uint64") { ulong val = buffer.read_uint64(); ret += val.ToString(); } else if (info.field_type == "float") { float val = buffer.read_decimal(32); ret += val.ToString(); } else if (info.field_type == "bool") { bool val = buffer.read_bool(); ret += val.ToString(); } else if (info.field_type == "string") { string val = buffer.read_string(); ret += "\"" + val + "\""; } return(ret + ", "); }
public int res_select_gate(Bit_Buffer buffer) { string gate_ip = buffer.read_string(); uint gate_port = buffer.read_uint(16); token_ = buffer.read_string(); player_log("\r\n" + "{" + "\r\n" + " gate_ip:" + gate_ip + "\r\n" + " gate_port:" + gate_port.ToString() + "\r\n" + " token:" + token_ + "\r\n" + "}"); robot_status_ = 1;//准备连接gate服务器 end_point_.disconnect(); end_point_.connect(gate_ip, (int)gate_port); return(1); }
private int set_vector_buffer(Field_Info info, Bit_Buffer buffer, JArray jarray) { int ret = 0; uint length = (uint)jarray.Count; buffer.write_uint(length, info.field_vbit); for (int i = 0; i < (int)length; ++i) { if (is_struct(info.field_type)) { ret = set_struct_buffer(info, buffer, (JObject)jarray[i]); } else { ret = set_arg_buffer(info, buffer, (JValue)jarray[i]); } } return(0); }
private string get_struct_string(Field_Info info, Bit_Buffer buffer, bool from_vector = false) { string ret = "{"; if (!from_vector) { ret = (info.field_name + ":{"); } Msg_Struct msg = Struct_Manager.instance.get_msg_struct(info.field_type); string msg_str = msg.get_print_msg(msg.field_list, buffer); if (msg_str != "") { msg_str = msg_str.Substring(0, msg_str.Length - 2); } ret += msg_str; return(ret + "}, "); }
private string get_vector_string(Field_Info info, Bit_Buffer buffer) { string ret = info.field_name + ":["; uint length = buffer.read_uint(info.field_vbit); for (uint i = 0; i < length; ++i) { if (is_struct(info.field_type)) { ret += get_struct_string(info, buffer, true); } else { ret += get_arg_string(info, buffer, true); } } if (length > 0) { ret = ret.Substring(0, ret.Length - 2); } return(ret + "], "); }
private int set_arg_buffer(Field_Info info, Bit_Buffer buffer, JValue value) { if (info.field_type == "int") { int val = int.Parse(value.ToString()); buffer.write_int(val, info.field_bit); } else if (info.field_type == "uint") { uint val = uint.Parse(value.ToString()); buffer.write_uint(val, info.field_bit); } else if (info.field_type == "int64") { long val = long.Parse(value.ToString()); buffer.write_int64(val); } else if (info.field_type == "uint64") { ulong val = ulong.Parse(value.ToString()); buffer.write_uint64(val); } else if (info.field_type == "float") { float val = float.Parse(value.ToString()); buffer.write_decimal(val, 32); } else if (info.field_type == "bool") { bool val = bool.Parse(value.ToString()); buffer.write_bool(val); } else if (info.field_type == "string") { string val = value.ToString(); buffer.write_string(val); } return(0); }
private int process_packet(Player p, uint cmd, Bit_Buffer buf) { int ret = 0; switch (cmd) { case Msg.RES_HEARTBEAT: ret = p.res_heartbeat(buf); break; case Msg.RES_SELECT_GATE: ret = p.res_select_gate(buf); break; case Msg.RES_CONNECT_GATE: ret = p.res_connect_gate(buf); break; case Msg.RES_ROLE_LIST: ret = p.res_role_list(buf); break; case Msg.RES_ENTER_GAME: ret = p.res_enter_game(buf); break; case Msg.RES_ERROR_CODE: ret = p.res_error_code(buf); break; default: ret = p.res_server_msg(cmd, buf); break; } return(ret); }
public void send_to_server(uint cmd_id, Bit_Buffer buffer) { player_.end_point.send_to_server(cmd_id, buffer); }
public void process_buffer(Endpoint ep, Byte_Buffer buffer) { Player p = get_player(ep); while (buffer.readable_length() > 0) { if (p.end_point.merge_state == 0) { uint read_len = 0; if (p.end_point.len_data == null) { if (buffer.readable_length() < 2) { p.end_point.len_data = new byte[2]; p.end_point.len_data[0] = (byte)buffer.read_int8(); p.end_point.merge_state = 1; return; } read_len = buffer.read_uint16(); } else { read_len = BitConverter.ToUInt16(p.end_point.len_data, 0); } uint len = (((read_len) & 0x1f) << 8) | (((read_len) & 0xff00) >> 8); if (p.end_point.buffer_data == null) { if (buffer.readable_length() < (int)len) { p.end_point.buffer_data = new Byte_Buffer(); p.end_point.buffer_data.copy(buffer); p.end_point.remain = (int)len - buffer.readable_length(); buffer.read_complete(); p.end_point.merge_state = 2; return; } uint cmd = buffer.read_uint8(); Bit_Buffer buf = new Bit_Buffer(buffer.rdata(), (int)len - 1); buffer.rpos += ((int)len - 1); process_packet(p, cmd, buf); } else { uint cmd = p.end_point.buffer_data.read_uint8(); Bit_Buffer buf = new Bit_Buffer(p.end_point.buffer_data.rdata(), (int)len - 1); process_packet(p, cmd, buf); p.end_point.buffer_data = null; } } else { if (p.end_point.merge_state == 1) { p.end_point.len_data[1] = (byte)buffer.read_int8(); p.end_point.merge_state = 0; } else if (p.end_point.merge_state == 2) { if (buffer.readable_length() < p.end_point.remain) { p.end_point.buffer_data.copy(buffer); p.end_point.remain -= buffer.readable_length(); buffer.read_complete(); return; } else { p.end_point.buffer_data.copy(buffer.rdata(), p.end_point.remain); buffer.rpos += p.end_point.remain; p.end_point.merge_state = 0; } } } } }
public int res_connect_gate(Bit_Buffer buffer) { player_log("连接gate成功"); req_role_list(); return(0); }
/// <summary> /// 服务器返回消息 /// </summary> public int res_heartbeat(Bit_Buffer buffer) { server_tick_ = buffer.read_int(32); player_log("服务器心跳时间:" + server_tick_.ToString()); return(0); }
public void copy(Bit_Buffer buffer) { copy(buffer.rdata(), buffer.readable_length()); }
public int res_enter_game(Bit_Buffer buffer) { robot_status_ = 2; //机器人成功连接到game,可以发消息 return(0); }