public void PlayerMove(Int32 x, Int32 z, Int32 ry, Int32 uid, Int32 state) { // 客户端发送move信息 msgtemp = new MSG_PLAYER_MOVE(); Byte[] temp = new byte[1024]; using (CodedOutputStream cos = new CodedOutputStream(temp)) { //Debug.LogError("Send mov: x = " + x + ", z = " + z + "."); msgtemp.Ry = ry; msgtemp.X = x; msgtemp.Z = z; msgtemp.Playerid = uid; msgtemp.State = state; msgtemp.WriteTo(cos); //Debug.LogError("-------------------------state " + state); //data = cos.to.ToArray(); } Buffers.Encode(data, 1006, uid, msgtemp.CalculateSize()); // 1006 -> player move for (int i = 0; i < msgtemp.CalculateSize(); i++) { data[sizeof(Int32) * 3 + i] = temp[i]; } net_manager.Send(data, 0, sizeof(Int32) * 3 + msgtemp.CalculateSize(), out se); }
public void RegistePlayerSelf(Player player, Int32 uid, Int32 ry, Int32 x, Int32 y) { if (player == null) { return; } m_PlayerSelf = player; // 客户端发送register信息 msgRegister = new MSG_PLAYER_REGISTER(); msgtemp_register = new MSG_PLAYER_REGISTER(); Byte[] temp = new byte[1024]; using (CodedOutputStream cos = new CodedOutputStream(temp)) { msgRegister.Playerid = uid; msgRegister.Ry = ry; msgRegister.Professsional = 0; msgRegister.X = x; msgRegister.Z = y; msgRegister.Speed = 0; msgRegister.WriteTo(cos); //data = cos.to.ToArray(); } Buffers.Encode(data, 1008, uid, msgRegister.CalculateSize()); for (int i = 0; i < msgRegister.CalculateSize(); i++) { data[sizeof(Int32) * 3 + i] = temp[i]; } net_manager.Send(data, 0, sizeof(Int32) * 3 + msgRegister.CalculateSize(), out se); }
private void LoginButton_Click(object sender, EventArgs e) { string name = Username.Text.Trim(); string password = Password.Text.Trim(); if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(password)) { RemindLable.Text = "请输入合法的用户名密码!"; RemindLable.ForeColor = Color.Red; return; } CtlMsgLoginReq login = new CtlMsgLoginReq(); login.Name = name; login.Password = password; Byte[] temp = new byte[1024]; byte[] data = new byte[1024]; using (CodedOutputStream cos = new CodedOutputStream(temp)) { login.Name = name; login.Password = password; login.WriteTo(cos); } Buffers.Encode(data, 9999, 1, login.CalculateSize()); for (int i = 0; i < login.CalculateSize(); i++) { data[sizeof(Int32) * 3 + i] = temp[i]; } if (tcpSocket.Connected()) { tcpSocket.Send(data, 0, sizeof(Int32) * 3 + login.CalculateSize(), out socketError); } MsgInfo msg = null; while (true) { int ret = tcpSocket.Receive(buffers, out socketError); if (buffers.IsHeaderReadable(sizeof(Int32) * 3)) { msg = buffers.Decode(); if (buffers.IsHeaderAndPayloadReadable(msg.GetPacLen() + sizeof(Int32) * 3)) { buffers.GetPayload(msg); buffers.SetStartByLen(msg.GetPacLen() + sizeof(Int32) * 3); break; } } } CtlMsgLoginRsp loginRsp = CtlMsgLoginRsp.Parser.ParseFrom(msg.GetPayload()); if (loginRsp.ErrCode != ErrorCode.ErrorNoError) { RemindLable.Text = "登录失败!"; RemindLable.ForeColor = Color.Red; return; } RemindLable.Text = "登录成功!"; RemindLable.ForeColor = Color.Green; isLogined = true; player = loginRsp.Player; LoginButton.Visible = false; checkBox.Visible = false; InitBagInfo(); foreach (var curitem in player.Bags.Currency) { if (curitem.ItemID == -1) { continue; } switch (curitem.TypeID) { case 0: label7.Text = "金币:" + curitem.Count; break; case 1: label10.Text = "银币:" + curitem.Count; break; case 2: label11.Text = "钻石:" + curitem.Count; break; default: break; } } }
private void button1_Click(object sender, EventArgs e) { if (isLogined == false) { RemindLable.Text = "请正确登陆后操作!"; RemindLable.ForeColor = Color.Red; return; } int id; int count; if (!Int32.TryParse(RemoveItemText.Text.Trim(), out id) || !Int32.TryParse(RemoveItemCount.Text.Trim(), out count)) { RemindLable.Text = "请输入正确的id和count!"; RemindLable.ForeColor = Color.Red; return; } RemindLable.Text = "删除成功!"; RemindLable.ForeColor = Color.Green; Byte[] temp = new byte[1024]; byte[] data = new byte[1024]; AddItemReq item = new AddItemReq(); item.ItemID = id; item.Count = count; using (CodedOutputStream cos = new CodedOutputStream(temp)) { item.WriteTo(cos); } Buffers.Encode(data, 10001, player.Id, item.CalculateSize()); for (int i = 0; i < item.CalculateSize(); i++) { data[sizeof(Int32) * 3 + i] = temp[i]; } if (tcpSocket.Connected()) { tcpSocket.Send(data, 0, sizeof(Int32) * 3 + item.CalculateSize(), out socketError); } MsgInfo msg = null; while (true) { int ret = tcpSocket.Receive(buffers, out socketError); if (buffers.IsHeaderReadable(sizeof(Int32) * 3)) { msg = buffers.Decode(); if (buffers.IsHeaderAndPayloadReadable(msg.GetPacLen() + sizeof(Int32) * 3)) { buffers.GetPayload(msg); buffers.SetStartByLen(msg.GetPacLen() + sizeof(Int32) * 3); break; } } } RemoveItemRsp addRsp = RemoveItemRsp.Parser.ParseFrom(msg.GetPayload()); if (addRsp.ErrCode != ErrorCode.ErrorNoError) { RemindLable.Text = "删除失败!"; RemindLable.ForeColor = Color.Red; return; } BagInfo bag = player.Bags; for (int i = 0; i < bag.Bag.Count; i++) { if (bag.Bag[i] == null) { continue; } if (bag.Bag[i].ItemID == item.ItemID) { bag.Bag[i].Count -= item.Count; // 我发送的请求生效,则在客户端同步 if (bag.Bag[i].Count <= 0) { bag.Bag.RemoveAt(i); } break; } } InitBagInfo(); }