public virtual void AddPacketInfo(Packet packet) { packet.AddUInt32(Id); packet.AddByte((byte)ClassType); packet.AddUInt16(Type); packet.AddByte(Lvl); packet.AddFloat((float)Hp); packet.AddFloat((float)Stats.MaxHp); packet.AddUInt16(Count); }
private void PointUpdate(ICity city) { if (!ShouldUpdate(city)) { return; } channel.Post(GetChannelName(city), () => { var packet = new Packet(Command.CityPointUpdate); packet.AddUInt32(city.Id); packet.AddInt32(city.AttackPoint); packet.AddInt32(city.DefensePoint); packet.AddUInt16(city.Value); packet.AddFloat((float)city.AlignmentPoint); return(packet); }); }
private void GetTroopInfo(Session session, Packet packet) { ICity city; ITroopObject troop; uint cityId; uint objectId; try { cityId = packet.GetUInt32(); objectId = packet.GetUInt32(); } catch (Exception) { ReplyError(session, packet, Error.Unexpected); return; } locker.Lock(cityId, objectId, out city, out troop).Do(() => { if (city == null || troop == null || troop.Stub == null) { ReplyError(session, packet, Error.ObjectNotFound); return; } var reply = new Packet(packet); reply.AddUInt16(troop.Stub.TroopId); if (city.Owner == session.Player) { reply.AddByte(troop.Stats.AttackRadius); reply.AddFloat((float)troop.Stats.Speed); reply.AddUInt32(troop.TargetX); reply.AddUInt32(troop.TargetY); var template = new Dictionary <ushort, IBaseUnitStats>(); reply.AddByte(troop.Stub.FormationCount); foreach (var formation in troop.Stub) { reply.AddByte((byte)formation.Type); reply.AddByte((byte)formation.Count); foreach (var kvp in formation) { reply.AddUInt16(kvp.Key); reply.AddUInt16(kvp.Value); template[kvp.Key] = city.Template[kvp.Key]; } } reply.AddUInt16((ushort)template.Count); IEnumerator <KeyValuePair <ushort, IBaseUnitStats> > templateIter = template.GetEnumerator(); while (templateIter.MoveNext()) { KeyValuePair <ushort, IBaseUnitStats> kvp = templateIter.Current; reply.AddUInt16(kvp.Key); reply.AddByte(kvp.Value.Lvl); } } session.Write(reply); }); }
private void Test_Share_Net_Packet_Custom() { byte[] buffer = new byte[Packet.DEFAULT_PACKET_BUF_SIZE]; Packet pkt = new Packet(buffer); pkt.Initialize(); CAssert.AreEqual(buffer, pkt.Buf); CAssert.AreEqual((int)pkt.Size, Packet.PACKET_HEAD_LENGTH); pkt.SetPacketID(Protocol.CLI_GW_ENTER_TEST); CAssert.AreEqual(pkt.GetPacketID(), Protocol.CLI_GW_ENTER_TEST); int int_test = 10000001; uint uint_test = 88776655; short short_test = 10001; ushort ushort_test = 65530; long long_test = 9223372036854775805; double double_test = 123456789.987654321; float float_test = 12345.12345f; string str_test = "This is a testing string."; pkt.AddInt(int_test); pkt.AddUint(uint_test); pkt.AddShort(short_test); pkt.AddUshort(ushort_test); pkt.AddLong(long_test); pkt.AddDouble(double_test); pkt.AddFloat(float_test); pkt.AddString(str_test); int total_size = Packet.PACKET_HEAD_LENGTH + sizeof(int) + sizeof(uint) + sizeof(short) + sizeof(ushort) + sizeof(long) + sizeof(double) + sizeof(float) + sizeof(short) + str_test.Length; CAssert.AreEqual(total_size, (int)pkt.Size); pkt.ResetBufferIndex(); int int_get = pkt.GetInt(); CAssert.AreEqual(int_get, int_test); uint uint_get = pkt.GetUint(); CAssert.AreEqual(uint_get, uint_test); short short_get = pkt.GetShort(); CAssert.AreEqual(short_get, short_test); ushort ushort_get = pkt.GetUshort(); CAssert.AreEqual(ushort_get, ushort_test); long long_get = pkt.GetLong(); CAssert.AreEqual(long_get, long_test); double double_get = pkt.GetDouble(); CAssert.AreEqual(double_get, double_test); float float_get = pkt.GetFloat(); CAssert.AreEqual(float_get, float_test); string str_get = pkt.GetString(); CAssert.AreEqual(str_get, str_test); pkt.Release(); CAssert.IsNull(pkt.Buf); buffer = null; }
private void Test_Share_Net_ReadWriteBuffer() { Packet pkt_orig = PacketManager.Instance.AllocatePacket(); CAssert.IsNotNull(pkt_orig); pkt_orig.SetPacketID(Protocol.CLI_GW_ENTER_TEST); CAssert.AreEqual(pkt_orig.GetPacketID(), Protocol.CLI_GW_ENTER_TEST); int int_test = 10000001; short short_test = 10001; double double_test = 123456789.987654321; float float_test = 12345.12345f; string str_test = "This is a testing string."; pkt_orig.AddInt(int_test); pkt_orig.AddShort(short_test); pkt_orig.AddDouble(double_test); pkt_orig.AddFloat(float_test); pkt_orig.AddString(str_test); byte[] buffer = new byte[ReadWriteBuffer.BUFFER_MAX_SIZE]; ReadWriteBuffer rw_buf = new ReadWriteBuffer(buffer); CAssert.IsNotNull(rw_buf); CAssert.AreEqual(0, rw_buf.GetCanReadSize()); CAssert.AreEqual(ReadWriteBuffer.BUFFER_MAX_SIZE, rw_buf.GetCanWriteSize()); rw_buf.WriteBytes(pkt_orig.Buf, pkt_orig.Size); CAssert.AreEqual(rw_buf.GetCanReadSize(), (int)pkt_orig.Size); Packet pkt_read = PacketManager.Instance.AllocatePacket(); CAssert.IsNotNull(pkt_read); rw_buf.ReadBytes(pkt_read.Buf, rw_buf.GetCanReadSize()); pkt_read.SetSize(); CAssert.AreEqual(0, rw_buf.GetCanReadSize()); CAssert.AreEqual(ReadWriteBuffer.BUFFER_MAX_SIZE, rw_buf.GetCanWriteSize()); CAssert.AreEqual(pkt_orig.Size, pkt_read.Size); CAssert.AreEqual(pkt_orig.Buf, pkt_read.Buf); int w_count = 0; while (rw_buf.GetCanWriteSize() >= pkt_orig.Size) { rw_buf.WriteBytes(pkt_orig.Buf, pkt_orig.Size); ++w_count; } CAssert.AreEqual(pkt_orig.Size * w_count, rw_buf.GetCanReadSize()); int r_count = 0; while (rw_buf.GetCanReadSize() > 0) { int pkt_size = rw_buf.PeekPacketSize(); CAssert.IsTrue(rw_buf.GetCanReadSize() >= pkt_size); Packet pkt_tmp = PacketManager.Instance.AllocatePacket(); CAssert.IsNotNull(pkt_tmp); pkt_tmp.Initialize(); rw_buf.ReadBytes(pkt_tmp.Buf, pkt_size); pkt_tmp.SetSize(); ++r_count; CAssert.AreEqual(pkt_tmp.Size, pkt_orig.Size); CAssert.AreEqual(pkt_tmp.Buf, pkt_orig.Buf); } CAssert.AreEqual(r_count, w_count); CAssert.AreEqual(0, rw_buf.GetCanReadSize()); CAssert.AreEqual(ReadWriteBuffer.BUFFER_MAX_SIZE, rw_buf.GetCanWriteSize()); rw_buf.Release(); //CAssert.IsNull(rw_buf.Buffer); }
private void StructureInfo(Session session, Packet packet) { ICity city; IStructure structure; uint cityId; uint objectId; try { cityId = packet.GetUInt32(); objectId = packet.GetUInt32(); } catch (Exception) { ReplyError(session, packet, Error.Unexpected); return; } locker.Lock(cityId, objectId, out city, out structure).Do(() => { if (city == null || structure == null) { ReplyError(session, packet, Error.ObjectNotFound); return; } var reply = new Packet(packet); reply.AddUInt16(structure.Stats.Base.Type); reply.AddByte(structure.Stats.Base.Lvl); if (session.Player == structure.City.Owner) { reply.AddUInt16(structure.Stats.Labor); reply.AddUInt16((ushort)structure.Stats.Hp); foreach (var prop in propertyFactory.GetProperties(structure.Type)) { switch (prop.Type) { case DataType.Byte: reply.AddByte((byte)prop.GetValue(structure)); break; case DataType.UShort: reply.AddUInt16((ushort)prop.GetValue(structure)); break; case DataType.UInt: reply.AddUInt32((uint)prop.GetValue(structure)); break; case DataType.String: reply.AddString((string)prop.GetValue(structure)); break; case DataType.Int: reply.AddInt32((int)prop.GetValue(structure)); break; case DataType.Float: reply.AddFloat((float)prop.GetValue(structure)); break; } } } else { foreach (var prop in propertyFactory.GetProperties(structure.Type, Visibility.Public)) { switch (prop.Type) { case DataType.Byte: reply.AddByte((byte)prop.GetValue(structure)); break; case DataType.UShort: reply.AddUInt16((ushort)prop.GetValue(structure)); break; case DataType.UInt: reply.AddUInt32((uint)prop.GetValue(structure)); break; case DataType.String: reply.AddString((string)prop.GetValue(structure)); break; case DataType.Int: reply.AddInt32((int)prop.GetValue(structure)); break; case DataType.Float: reply.AddFloat((float)prop.GetValue(structure)); break; } } } session.Write(reply); }); }