예제 #1
0
 public void Generate(Protocol protocol,
     string outputPath)
 {
     GenerateMessageCode(protocol, outputPath);
     GenerateProxyCode(protocol, outputPath);
     GenerateStubCode(protocol, outputPath);
 }
예제 #2
0
        private void GenerateMessageCode(Protocol protocol, string path)
        {
            string outputFile = String.Format("{0}/{1}.message.js",
                path, protocol.name.ToLower());
            using (StreamWriter sw = new StreamWriter(outputFile))
            {
                sw.WriteLine("var message = require('./odin.message.js');");
                foreach (Import import in protocol.importList)
                {
                    if (import.lang != "js") continue;
                    sw.WriteLine("{0}", import.name);
                }
                sw.WriteLine("");
                sw.WriteLine("");
                foreach (Message message in protocol.messageList)
                {
                    sw.WriteLine("var {0}Message = function() {{", message.name);
                    sw.WriteLine("\tmessage.Message.apply(this, arguments);");
                    foreach (Data data in message.dataList)
                    {
                        sw.WriteLine("\tthis.{0} = undefined;", data.name);
                    }

                    sw.WriteLine("};");
                    sw.WriteLine("{0}Message.prototype = new message.Message();", message.name);
                    sw.WriteLine("{0}Message.prototype.constructor = {0}Message;", message.name);
                    sw.WriteLine("");
                    sw.WriteLine("exports.{0}Message = {0}Message;", message.name);
                    sw.WriteLine("");
                }
            }
        }
예제 #3
0
        private void GenerateProxyCode(Protocol protocol, string path)
        {
            string outputFile = String.Format("{0}/{1}.proxy.js",
                path, protocol.name.ToLower());
            using (StreamWriter sw = new StreamWriter(outputFile))
            {
                sw.WriteLine("var {0}message = require('./{0}.message.js');",
                    protocol.name.ToLower());
                foreach (Import import in protocol.importList)
                {
                    if (import.lang != "js") continue;
                    sw.WriteLine("{0}", import.name);
                }
                sw.WriteLine();
                sw.WriteLine("var proxy = function() {");
                sw.WriteLine("\tthis.protocol = undefined;");
                sw.WriteLine("};");
                sw.WriteLine();
                sw.WriteLine("proxy.prototype = {");
                foreach (Message message in protocol.messageList)
                {
                    string paramList = "socket, ";
                    foreach (Data data in message.dataList)
                    {
                        paramList += string.Format("{0}, ", data.name);
                    }
                    if (paramList.Length > 2)
                    {
                        paramList = paramList.Substring(0, paramList.Length - 2);
                    }

                    sw.WriteLine("\t{0} : function({1}) {{", message.name, paramList);
                    sw.WriteLine("\t\ttry {");
                    sw.WriteLine("\t\t\tvar sendMessage = new {0}message.{1}Message();",
                        protocol.name.ToLower(), message.name);

                    sw.WriteLine("\t\t\tsendMessage.id = \"{0}\";", message.id);
                    foreach (Data data in message.dataList)
                    {
                        sw.WriteLine("\t\t\tsendMessage.{0} = {0};", data.name);
                    }
                    sw.WriteLine();
                    sw.WriteLine("\t\t\tvar sendData = new Buffer(JSON.stringify(sendMessage));");
                    sw.WriteLine("\t\t\tthis.protocol.sendPacket(socket, sendData);");
                    sw.WriteLine("\t\t} catch(e) {");
                    sw.WriteLine("\t\t\tconsole.log(e);");
                    sw.WriteLine("\t\t}");
                    sw.WriteLine("\t},");
                }

                sw.WriteLine("};");
                sw.WriteLine();
                sw.WriteLine("exports.proxy = proxy;");
            }
        }
예제 #4
0
        private void GenerateMessageCode(Protocol protocol, string path)
        {
            string outputFile = String.Format("{0}/{1}.Message.cs",
                path, protocol.name);
            using (StreamWriter sw = new StreamWriter(outputFile))
            {
                foreach (Import import in protocol.importList)
                {
                    if (import.lang != "cs") continue;
                    sw.WriteLine("using {0};", import.name);
                }

                sw.WriteLine("namespace {0}", protocol.name);
                sw.WriteLine("{");
                sw.WriteLine("\tpublic class Message");
                sw.WriteLine("\t{");

                sw.WriteLine("\t\tpublic const int Version = {0};",
                    protocol.version);
                sw.WriteLine();

                /// FLAG
                sw.WriteLine("\t\tpublic enum Flag");
                sw.WriteLine("\t\t{");
                foreach (Flag flag in protocol.flagList)
                {
                    sw.WriteLine("\t\t\tkFlag{0} = {1},", flag.name, flag.value);
                }
                sw.WriteLine("\t\t};");
                sw.WriteLine("");

                foreach (Message message in protocol.messageList)
                {
                    sw.WriteLine("\t\tpublic struct {0}", message.name);
                    sw.WriteLine("\t\t{");
                    sw.WriteLine("\t\t\tpublic string id;");
                    foreach (Data data in message.dataList)
                    {
                        if (data.generic != "")
                            sw.WriteLine("\t\t\tpublic {0}<{1}> {2};\t{3}",
                                data.type, data.generic, data.name,
                                data.desc == string.Empty ? "" : "//" + data.desc);
                        else
                            sw.WriteLine("\t\t\tpublic {0} {1};\t{2}", data.type,
                                data.name,
                                data.desc == string.Empty ? "" : "//" + data.desc);
                    }
                    sw.WriteLine("\t\t}");
                }

                sw.WriteLine("\t}");
                sw.WriteLine("}");
            }
        }
예제 #5
0
        private void GenerateProxyCode(Protocol protocol, string path)
        {
            string outputFile = String.Format("{0}/{1}.Proxy.cs",
                path, protocol.name);
            using (StreamWriter sw = new StreamWriter(outputFile))
            {
                foreach (Import import in protocol.importList)
                {
                    if (import.lang != "cs") continue;
                    sw.WriteLine("using {0};", import.name);
                }

                sw.WriteLine("namespace {0}", protocol.name);
                sw.WriteLine("{");
                sw.WriteLine("\tpublic class Proxy : MessageProxy");
                sw.WriteLine("\t{");

                sw.WriteLine("\t\tpublic const int Version = {0};",
                    protocol.version);
                sw.WriteLine();

                foreach (Message message in protocol.messageList)
                {
                    string paramList = "Socket client, ";
                    foreach (Data data in message.dataList)
                    {
                        if (data.generic != "")
                        {
                            paramList += string.Format("{0}<{1}> {2}, ",
                                data.type, data.generic, data.name);
                        }
                        else
                        {
                            paramList += string.Format("{0} {1}, ",
                                data.type, data.name);
                        }
                    }
                    if (paramList.Length > 2)
                    {
                        paramList = paramList.Substring(0, paramList.Length - 2);
                    }

                    sw.WriteLine("\t\tpublic bool {0}({1})", message.name, paramList);
                    sw.WriteLine("\t\t{");

                    sw.WriteLine("\t\t\tif (client == null) return false;");

                    sw.WriteLine("\t\t\tMessage.{0} msg = new Message.{0}();", message.name);
                    sw.WriteLine("\t\t\tmsg.id = \"{0}\";", message.id);

                    foreach (Data data in message.dataList)
                    {
                        sw.WriteLine("\t\t\tmsg.{0} = {0};", data.name);
                    }

                    sw.WriteLine("\t\t\tstring jsonmsg = JsonConvert.SerializeObject(msg);");
                    sw.WriteLine("\t\t\tbyte[] data = UTF8Encoding.UTF8.GetBytes(jsonmsg);");
                    sw.WriteLine("\t\t\tvar dataArray = new List<byte>();");
                    sw.WriteLine("\t\t\tdataArray.AddRange(data);");
                    sw.WriteLine("\t\t\tdataArray.InsertRange(0, BitConverter.GetBytes((int)(data.Length + 4)));");
                    sw.WriteLine("\t\t\tif (!client.Connected) return false;");
                    sw.WriteLine("\t\t\tclient.Send(dataArray.ToArray());");
                    //sw.WriteLine("\t\t\tclient.Send(data);");
                    sw.WriteLine("\t\t\treturn true;");

                    sw.WriteLine("\t\t}");
                }

                sw.WriteLine("\t}");
                sw.WriteLine("}");
            }
        }
예제 #6
0
        private void GenerateStubCode(Protocol protocol, string path)
        {
            string outputFile = String.Format("{0}/{1}.Stub.cs",
                path, protocol.name);
            using (StreamWriter sw = new StreamWriter(outputFile))
            {
                foreach (Import import in protocol.importList)
                {
                    if (import.lang != "cs") continue;
                    sw.WriteLine("using {0};", import.name);
                }

                sw.WriteLine("namespace {0}", protocol.name);
                sw.WriteLine("{");
                sw.WriteLine("\tpublic class Stub : MessageStub");
                sw.WriteLine("\t{");

                sw.WriteLine("\t\tpublic const int Version = {0};",
                    protocol.version);
                sw.WriteLine();

                foreach (Message message in protocol.messageList)
                {
                    sw.WriteLine("\t\tpublic delegate void {1}Delegate(string message, {0}.Message.{1} data);",
                        protocol.name, message.name);
                    sw.WriteLine("\t\tpublic event {0}Delegate On{0};", message.name);

                    sw.WriteLine("\t\t[RpcStubAttribute(\"{0}\")]", message.id);
                    sw.WriteLine("\t\tpublic virtual {0}.Message.{1} {1}(string message)",
                        protocol.name, message.name);
                    sw.WriteLine("\t\t{");

                    sw.WriteLine("\t\t\tMessage.{0} data = JsonConvert.DeserializeObject<Message.{0}>(message);", message.name);

                    sw.WriteLine("\t\t\tif(On{0} != null) On{0}(message, data);", message.name);
                    sw.WriteLine();
                    sw.WriteLine("\t\t\treturn data;");

                    sw.WriteLine("\t\t}");
                }

                sw.WriteLine("\t}");
                sw.WriteLine("}");
            }
        }
예제 #7
0
        public Protocol Parse(string packetFile)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(packetFile);

            Protocol protocol = new Protocol();
            XmlNode protocolNode = doc.SelectSingleNode("/Protocol");
            protocol.name = protocolNode.Attributes["name"].InnerText;
            protocol.number = int.Parse(protocolNode.Attributes["number"].InnerText);
            protocol.version = int.Parse(protocolNode.Attributes["version"].InnerText);

            /// Using
            XmlNodeList usingList = protocolNode.SelectNodes("Import");
            foreach (XmlNode node in usingList)
            {
                Import import = new Import();
                import.name = node.Attributes["name"].InnerText;
                import.lang = node.Attributes["lang"].InnerText;

                protocol.importList.Add(import);
            }

            /// FLAG
            XmlNodeList flagList = protocolNode.SelectNodes("Flag");
            foreach (XmlNode node in flagList)
            {
                Flag flag = new Flag();
                flag.name = node.Attributes["name"].InnerText;
                flag.value = int.Parse(node.Attributes["value"].InnerText);
                if (node.Attributes["desc"] != null)
                    flag.desc = node.Attributes["desc"].InnerText;
                else
                    flag.desc = "";

                protocol.flagList.Add(flag);
            }

            /// Message
            XmlNodeList messageList = protocolNode.SelectNodes("Message");
            UInt32 lastId = 0;
            foreach (XmlNode node in messageList)
            {
                Message message = new Message();
                message.name = node.Attributes["name"].InnerText;
                if (node.Attributes["id"] == null)
                {
                    message.id = ++lastId;
                }
                else
                {
                    message.id = UInt32.Parse(node.Attributes["id"].InnerText);
                    lastId = message.id;
                }

                XmlNodeList dataList = node.SelectNodes("Data");
                foreach (XmlNode dataNode in dataList)
                {
                    Data data = new Data();
                    data.type = dataNode.Attributes["type"].InnerText;
                    data.name = dataNode.Attributes["name"].InnerText;
                    if (dataNode.Attributes["generic"] != null)
                        data.generic = dataNode.Attributes["generic"].InnerText;
                    else
                        data.generic = "";
                    if (dataNode.Attributes["desc"] != null)
                        data.desc = dataNode.Attributes["desc"].InnerText;
                    else
                        data.desc = "";

                    message.dataList.Add(data);
                }

                protocol.messageList.Add(message);
            }

            return protocol;
        }
예제 #8
0
        private void GenerateStubCode(Protocol protocol, string path)
        {
            string outputFile = String.Format("{0}/{1}.stub.js",
                path, protocol.name.ToLower());
            using (StreamWriter sw = new StreamWriter(outputFile))
            {
                //sw.WriteLine("var {0}message = require('./{0}.message.js');",
                //    protocol.name.ToLower());
                foreach (Import import in protocol.importList)
                {
                    if (import.lang != "js") continue;
                    sw.WriteLine("{0}", import.name);
                }
                sw.WriteLine();
                sw.WriteLine("var stub = function() {");
                foreach (Message message in protocol.messageList)
                {
                    sw.WriteLine("\tthis.On{0} = undefined;", message.name);
                }
                sw.WriteLine("};");
                sw.WriteLine();
                sw.WriteLine("stub.prototype = {");
                sw.WriteLine("\tprocess : function(message, socket, sessions) {");
                sw.WriteLine("\t\ttry{");
                int i = 0;
                foreach (Message message in protocol.messageList)
                {
                    if (i++ == 0)
                    {
                        sw.Write("\t\t\tif");
                    }
                    else
                    {
                        sw.Write("\t\t\telse if");
                    }
                    sw.WriteLine("(message.id === '{0}' && this.On{1} !== undefined && this.On{1} !== null) {{",
                        message.id, message.name);
                    sw.WriteLine("\t\t\t\tthis.On{0}(message, socket, sessions);", message.name);
                    sw.WriteLine("\t\t\t}");
                    sw.WriteLine();
                }
                sw.WriteLine("\t\t} catch(e) {");
                sw.WriteLine("\t\t\tconsole.log(e);");
                sw.WriteLine("\t\t}");
                sw.WriteLine("\t},");

                sw.WriteLine("};");
                sw.WriteLine();
                sw.WriteLine("exports.stub = stub;");
            }
        }