๐Ÿ™‡โ€โ™€๏ธPacketGenerator

ํŒจํ‚ท ์ƒ์„ฑ๊ธฐ

๐Ÿช๋ถ„์„

  • Packet Generator 1
    • Packet class
      • Size packetId๋Š” ํ—ค๋”์— ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„์ด์ง€๋งŒ ์‹ค์งˆ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
      • Packet class๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ƒ์†์„ ์ œ๊ฑฐํ•จ
      • ์ƒ์„ฑ์ž๋กœ ๊ฐ’์„ ๋„ฃ๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ง‰๋ฐ”๋กœ ๋„ฃ์Œ
    • PacketGenerator
      • ์›๋ณธ์— ๋Œ€ํ•œ ์ •์˜๊ฐ€ ํ•„์š”
      • ํŒจํ‚ท ์ •์˜๋ฅผ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ํ• ์ง€ ์ •์˜ ํ•ด์•ผ๋œ๋‹ค (json, xml)
    • PDL(PacketDefinitionList) - xml
      • ํ—ค๋”์— xml์—๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ
      • ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค - <PDL> </PDL>
      • <long name="playerId"/> ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ”๋กœ ๋‹ซ์„์ˆ˜ ์žˆ๋‹ค
      • packet long string ๋“ฑ๋“ฑ์€ ์ •ํ•ด์ง„ ์ด๋ฆ„์ด ์•„๋‹ˆ๋ผ ํŒŒ์‹ฑ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งž์ถฐ์ฃผ๋Š” ์ด๋ฆ„์ด๋‹ค
      • ๊ธฐ๋ณธ์ ์ธ๊ฒƒ๋“ค์„ ์•Œ๋ฉด ์ „์ฒด์ ์ธ ๊ตฌ์กฐ๋ฅผ ์•Œ์ˆ˜ ์ž‡๊ณ  ์ด๋ฅผ ์ด์šฉํ•ด ์ž๋™ํ™” ํˆด์„ ๋งŒ๋“ ๋Š” ๊ฒƒ!
      • xml์€ ๋Œ€์นญ์ ์ธ ๊ตฌ์กฐ์—์„œ ํŽธํ•˜๊ณ  ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค
      • cpp์—์„œ๋Š” ๊ณต์‹๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒ์— xml์„ ํŒŒ์‹ฑํ•˜๋Š” ๊ฒƒ์ด ์—†์ง€๋งŒ cs์—๋Š” ์žˆ์Œ (XmlReader)
<?xml version="1.0" encoding="utf-8" ?>
<PDL>
	<packet name="C_PlayerInfoReq">
		<byte name="testByte"/>
		<long name="playerId"/>
		<string name="name"/>
		<list name="skill">
			<int name="id"/>
			<short name="level"/>
			<float name="duration"/>
			<list name="attribute">
				<int name="att"/>
			</list>
		</list>
	</packet>
	<packet name ="S_Text">
		<int name="testInt"/>
	</packet>
</PDL>
  • XmlReader
    • PacketGenerator Program
      • XmlReader๋กœ Xml๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๊ณ  Create๋กœ ํŒŒ์ผ๊ณผ ์˜ต์…˜์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค
      • XmlReader r = XmlReader.Create("PDL.xml", settings);
      • ์ด๋•Œ settings๋Š” XmlReaderSettings๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ ์˜จ๊ฐ– ์„ค์ •์ด ๊ฐ€๋Šฅ!
      • IgnoreComments = true - ์ฃผ์„์„ ๋ฌด์‹œ
      • IgnoreWhitespace = true - ์ŠคํŽ˜์ด์Šค๋ฅผ ๋ฌด์‹œ
       XmlReaderSettings settings = new XmlReaderSettings()
      {
          IgnoreComments = true,
          IgnoreWhitespace = true
      };
      
      • XmlReader๊ฐ€ ํŒŒ์‹ฑ์„ ์™„๋ฃŒํ•œ ๋’ค์—๋Š” r.Dispose(); ๋กœ ๋‹ซ์•„์ฃผ๋Š” ์ž‘์—…์„ ํ•ด์•ผ๋จ
      • using() {}์„ ์‚ฌ์šฉํ•˜๋ฉด ๋งˆ์น  ๋•Œ ์ž๋™์œผ๋กœ Dispose()๋ฅผ ํ•ด์คŒ
      • r.MoveToContent();๋ฅผ ํ•˜๋ฉด PDL์—์„œ ํ—ค๋”๊ฐ™์€ ๋ถ€๋ถ„์€ ๊ฑด๋„ˆ๋›ฐ๊ณ  ํ•ต์‹ฌ์ ์ด ๋‚ด์šฉ์œผ๋กœ ๋ฐ”๋กœ ๊ฐ
      • r.Read()๋Š” ํŒŒ์‹ฑ์„ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ Stream๋ฐฉ์‹ ์ฆ‰, ํ•œ์ค„ํ•œ์ค„ ์ฝ์Œ
      using (XmlReader r = XmlReader.Create(pdlPath, settings))
      {
          r.MoveToContent();
      
          while (r.Read())
          {
              Console.WriteLine(r.Name + " " + r["name"]);
          }
      }
      
      • Console.WriteLine(r.Name + " " + r["name"]); - `r[โ€œnameโ€]์€ name์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฝ๋Š” ๊ฒƒ
      • r.Name - packet (ํƒ€์ž…) , r["name"] - PlayerInfoReq (๋‚ด์šฉ๋ฌผ)
      • </packet> </PDL>๋„ ํŒŒ์‹ฑ์„ํ•จ - pakcet PDL

      • ๋ฐ”๋กœ PacektGenerator๋ฅผ ๋นŒ๋“œํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚จ
      • ์‹คํ–‰ํŒŒ์ผ์ด ์žˆ๋Š” ์œ„์น˜์—์„œ ์ฝ๊ธฐ ๋•Œ๋ฌธ์— PDLํŒŒ์ผ์„ ์‹คํ–‰ํŒŒ์ผ์ด ์žˆ๋Š” ํด๋”์— ๋„ฃ์–ด์ฃผ์–ด์•ผ ๋จ!
static void Main(string[] args)
{
    XmlReaderSettings settings = new XmlReaderSettings()
    {
        IgnoreComments = true,
        IgnoreWhitespace = true
    };

    using (XmlReader r = XmlReader.Create(settings))
    {
        r.MoveToContent();

        while (r.Read())
        {
            if (r.Depth == 1 && r.NodeType == XmlNodeType.Element)
            {
                ParsePacket(r);
            }
            //Console.WriteLine(r.Name + " " + r["name"]);
        }
    }
}
  • packet์ฝ๊ธฐ

if(r.Depth == 1 && r.NodeType == XmlNodeType.Element)๋ฉด ParsePacket(r);์„ ์‹คํ–‰ r.Depth๋Š” PDL์— packet์˜ ๊นŠ์ด๊ณ  ` r.NodeType == XmlNodeType.Element๋ถ€๋ถ„์€ </packet> </PDL>`์„ ํŒŒ์‹ฑํ•˜์ง€ ์•Š๊ฒŒ ์‹œ์ž‘ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์ฝ์Œ

  • ParsePacket
public static void ParsePacket(XmlReader r)
{
    if (r.NodeType == XmlNodeType.EndElement)
        return;

    if (r.Name.ToLower() != "packet")
    {
        Console.WriteLine("Invalid packet node");
        return;
    }

    string packetName = r["name"];
    if (string.IsNullOrEmpty(packetName))
    {
        Console.WriteLine("Packet without name");
        return;
    }
    
    ParseMembers(r);
}
  • if (r.NodeType == XmlNodeType.EndElement) return;์œผ๋กœ ๋‹ซํžˆ๋Š” ๋ถ€๋ถ„์ด๋ฉด return
  • if (r.Name.ToLower() != "packet") { Console.WriteLine("Invalid packet node"); return; }์œผ๋กœ packet๊ณผ ์ด๋ฆ„์ด ๋‹ค๋ฅด๋ฉด return, r.Name.ToLower()์€ r.Name์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟˆ
  • string packetName = r["name"];๋กœ packet์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ ธ์˜ด
  • if (string.IsNullOrEmpty(packetName)) { Console.WriteLine("Packet without name"); return; }๋กœ ๋น„์–ด์žˆ๋‹ค๋ฉด return

์—ฌ๊ธฐ๊นŒ์ง€ ์˜ค๋ฉด packet์ธ๊ฒƒ์ด ๋ถ„๋ช…ํ•˜๋‹ˆ packet๋‚ด์˜ Member๋“ค์„ ์ฝ์–ด์•ผํ•˜๋ฏ€๋กœ ParseMember(r)๋กœ ์ด๋™

  • ParseMembers
public static void ParseMembers(XmlReader r)
{
    string packetName = r["name"];

    int depth = r.Depth + 1;
    while (r.Read())
    {
        if (r.Depth != depth)
            break;

        string memberName = r["name"];
        if (string.IsNullOrEmpty(memberName))
        {
            Console.WriteLine("Member without name");
            return;
        }

        string memberType = r.Name.ToLower();
        switch (memberType)
        {
            case "byte":
            case "sbyte":
            case "bool":
            case "short":
            case "ushort":
            case "int":
            case "long":
            case "float":
            case "double":
            case "string":
            case "list":
            default:
                break;
        }
    }
}

int depth = r.Depth + 1;

์ฆ‰ depth๊ฐ€ +1 ์ผ ๋•Œ๊นŒ์ง€ ๋ฃจํ”„๋ฅผ ๋„๋‹ˆ๊นŒ

<int name="id"/>
<short name="level"/>
<float name="duration"/>

๋ฅผ ๋‹ค ํŒŒ์‹ฑํ•˜๊ณ  ๋น ์ ธ๋‚˜์˜ด

string memberType = r.Name.ToLower();๋กœ ๋งด๋ฒ„๋“ค์˜ ํƒ€์ž…์„ ์ฒดํฌ

์Šค์œ„์น˜๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ฐ ํƒ€์ž…๋งˆ๋‹ค ์–ด๋–ค ๊ฐ’์„ ๋„ฃ์„์ง€๋ฅผ ์ •ํ•จ

๐ŸชPacketFormat

public static string packetFormat = @""; ํ˜•์‹

@โ€โ€œ;์€ ์—ฌ๋Ÿฌ์ค„์˜ ๋ฌธ์ž์—ด์„ ๋„ฃ์„ ๋•Œ ์‚ฌ์šฉํ•จ

๊ณ ์ •์ ์ธ๊ฒƒ์€ ๊ทธ๋Œ€๋กœ ๋†”๋‘๊ณ  ๊ฐ€๋ณ€์ ์ธ ๊ฒƒ๋“ค์€ {0} {1} {2} โ€ฆ ์ˆœ์œผ๋กœ ๊ต์ฒดํ•œ๋‹ค

  • ๊ฐ€๋ณ€์ ์ธ ๊ฒƒ๋“ค
    • ํŒŒ์‹ฑํ•œ xml์—์„œ ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค - ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋“ค
    • ํŒจํ‚ท ์ด๋ฆ„๋“ฑ PDL์— ์ •์˜๋œ ๊ฒƒ๋“ค - ํŒจํ‚ท ์ด๋ฆ„
    • ์‹ค์งˆ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„ - ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ Read
    • ๋ณ€์ˆ˜ ํ˜•์‹, ๋ณ€์ˆ˜ ์ด๋ฆ„ ๋“ฑ๋“ฑ

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: