Exemplo n.º 1
0
def output_file_content(fp, msg, pkg_name, namespace):
    try:
        content = '''# -*- coding: utf-8 -*-

###############################################################################
# author: BrentHuang ([email protected])
###############################################################################

import os
import sys

sys.path.append('%s/../../../../py_tools' % os.path.split(os.path.realpath(__file__))[0])
# print(sys.path)

'''
        fp.write(content.encode('utf-8'))

        content = [
            'from util.%s_client import *' % namespace, os.linesep,
            'from test_action.%s.%s import *' %
            (namespace, name_style_util.camel_to_underline(msg).lower()),
            os.linesep * 3
        ]
        fp.write(''.join(content).encode('utf-8'))

        action_name = name_style_util.camel_to_underline(msg).lower()

        content = [
            'def %s():' % action_name[:-4], os.linesep,
            '    client = Client(conf.demo_server_addr, conf.demo_server_%s_port)'
            % namespace, os.linesep,
            '    %s = %s(client)' % (action_name, msg), os.linesep,
            '    ret = %s.%s()' % (action_name, action_name[:-4]), os.linesep,
            '    return ret', os.linesep * 3
        ]
        fp.write(''.join(content).encode('utf-8'))

        content = [
            'def test_001():', os.linesep,
            '    assert 0 == %s()' % action_name[:-4], os.linesep * 3
        ]
        fp.write(''.join(content).encode('utf-8'))

        content = [
            'if __name__ == \'__main__\':', os.linesep, '    test_001()',
            os.linesep
        ]
        fp.write(''.join(content).encode('utf-8'))

        return 0
    except Exception as e:
        print('exception: %s' % e)
        return -1
Exemplo n.º 2
0
def output_header_file_content(fp, msg, namespace):
    try:
        if 'proto_tcp' == namespace:
            namespace_begin = 'namespace tcp%s{%snamespace proto%s{' % (os.linesep, os.linesep, os.linesep)
            namespace_end = '}%s}' % os.linesep

        guard = 'DEMO_SERVER_%s_LOGICS_%s_HANDLER_H_' % (namespace.upper(),
                                                         name_style_util.camel_to_underline(msg).upper())
        class_name = '%sHandler' % msg

        content = ['#ifndef %s' % guard, os.linesep,
                   '#define %s' % guard, os.linesep * 2,
                   '#include "msg_handler.h"', os.linesep * 2,
                   '%s' % namespace_begin, os.linesep,
                   'class %s : public MsgHandler' % class_name, os.linesep,
                   '{', os.linesep, 'public:', os.linesep,
                   '    %sHandler();' % msg, os.linesep,
                   '    virtual ~%sHandler();' % msg, os.linesep * 2]
        fp.write(''.join(content).encode('utf-8'))

        if namespace == 'proto_tcp':
            content = '''    ///////////////////////// MsgHandlerInterface /////////////////////////
    ::proto::MsgID GetMsgID() override;
    void OnMsg(const ConnGUID* conn_guid, const ::proto::MsgHead& msg_head,
               const void* msg_body, size_t msg_body_len) override;

private:
    void SendErrRsp(const ConnGUID* conn_guid, const ::proto::MsgHead& req_msg_head, int err_code) const;'''
            fp.write(content.encode('utf-8'))
        elif namespace == 'http':
            content = '''    ///////////////////////// http::MsgHandlerInterface /////////////////////////
    const char* GetHttpReqPath() override;
    void OnHttpHeadReq(const ConnGuid* conn_guid, struct evhttp_request* evhttp_req, bool https,
                       const KeyValMap* http_header_map, const KeyValMap* http_query_map) override;
    void OnHttpGetReq(const ConnGuid* conn_guid, struct evhttp_request* evhttp_req, bool https,
                      const KeyValMap* http_header_map, const KeyValMap* http_query_map) override;
    void OnHttpPostReq(const ConnGuid* conn_guid, struct evhttp_request* evhttp_req, bool https,
                       const KeyValMap* http_header_map, const KeyValMap* http_query_map, const char* data,
                       int data_len) override;
    void OnHttpPutReq(const ConnGuid* conn_guid, struct evhttp_request* evhttp_req, bool https,
                      const KeyValMap* http_header_map, const KeyValMap* http_query_map, const char* data,
                      int data_len) override;'''
            fp.write(content.encode('utf-8'))
        else:
            return -1

        content = [os.linesep, '};', os.linesep, namespace_end, os.linesep * 2,
                   '#endif // %s' % guard, os.linesep]
        fp.write(''.join(content).encode('utf-8'))

        return 0
    except Exception as e:
        print('exception: %s' % e)
        return -1
Exemplo n.º 3
0
def output_file_content(fp, msg, pkg_name):
    try:
        msg_instance = msg[0].lower() + msg[1:]
        tab_blank = ' ' * 4

        content = [
            'using System;', os.linesep, 'using UnityEngine;', os.linesep,
            'using %s.cs;' % pkg_name.replace('::', '.'), os.linesep * 2,
            'public class %sHandler : MsgHandler' % msg, os.linesep, '{',
            os.linesep,
            '%spublic int GetMsgId()' % tab_blank, os.linesep,
            '%s{' % tab_blank, os.linesep,
            '%sreturn (int) MsgId.MSG_ID_%s;' %
            (tab_blank * 2, name_style_util.camel_to_underline(msg).upper()),
            os.linesep,
            '%s}' % tab_blank, os.linesep * 2,
            '%spublic void OnMsg(TcpMsg msg)' % tab_blank, os.linesep,
            '%s{' % tab_blank, os.linesep,
            '%stry' % (tab_blank * 2), os.linesep,
            '%s{' % (tab_blank * 2), os.linesep,
            '%s%s %s = TcpMsg.Deserialize<%s>(msg.MsgBody);' %
            (tab_blank * 3, msg, msg_instance, msg), os.linesep * 2
        ]
        fp.write(''.join(content).encode('utf-8'))

        if msg.endswith('Rsp'):
            content = [
                '%sif (%s.err_ctx.err_code != 0)' %
                (tab_blank * 3, msg_instance), os.linesep,
                '%s{' % (tab_blank * 3), os.linesep,
                '%sGameCtrl.Instance.TipsCtrl.AppendTips(ErrCode.Instance.GetErrDesc(%s.err_ctx.err_code));'
                % (tab_blank * 4, msg_instance), os.linesep,
                '%sreturn;' % (tab_blank * 4), os.linesep,
                '%s}' % (tab_blank * 3), os.linesep * 2
            ]
            fp.write(''.join(content).encode('utf-8'))

        content = [
            '%s}' % (tab_blank * 2), os.linesep,
            '%scatch (Exception e)' % (tab_blank * 2), os.linesep,
            '%s{' % (tab_blank * 2), os.linesep,
            '%sDebug.Log(e.ToString());' % (tab_blank * 3), os.linesep,
            '%s}' % (tab_blank * 2), os.linesep,
            '%s}' % tab_blank, os.linesep, '}', os.linesep
        ]
        fp.write(''.join(content).encode('utf-8'))

        return 0
    except Exception as e:
        print('exception: %s' % e)
        return -1
Exemplo n.º 4
0
def output_header_file(msg, output_dir, namespace_list):
    for namespace in namespace_list:
        file_path = os.path.join(output_dir, namespace, name_style_util.camel_to_underline(msg).lower() + '_handler.h')
        file_util.del_file(file_path)
        file_util.make_dir(file_util.file_dir(file_path))

        try:
            fp = open(file_path, 'wb')

            output_header_file_content(fp, msg, namespace)
            fp.close()

            print('=== generate %s done ===' % file_path)
        except Exception as e:
            print('exception: %s' % e)
            return -1

    return 0
def output_content(fp, msg_list_group, base_idx, gap, pkg_name):
    try:
        content = [
            'syntax = "proto3";', os.linesep,
            'package %s;' % pkg_name, os.linesep, os.linesep, 'enum MsgID {',
            os.linesep, '    PLACE_HOLDER = 0;', os.linesep
        ]
        fp.write(''.join(content).encode('utf-8'))

        group_base_idx = base_idx

        for msg_list in msg_list_group:
            if not msg_list or 0 == len(msg_list):
                continue

            if group_base_idx != base_idx:
                content = '    ////////////////////////////////////////////////////////////////////////////////%s' \
                          % os.linesep
                fp.write(content.encode('utf-8'))

            idx = group_base_idx
            for msg in msg_list:
                content = '    MSG_ID_%s = %d;%s' % (
                    name_style_util.camel_to_underline(msg).upper(), idx,
                    os.linesep)
                fp.write(content.encode('utf-8'))
                idx += 1

            group_base_idx += gap

        content = '}%s' % os.linesep
        fp.write(content.encode('utf-8'))

        return 0
    except Exception as e:
        print('exception: %s' % e)
        return -1
Exemplo n.º 6
0
def output_file(msg, output_dir, pkg_name, namespace_list):
    for namespace in namespace_list:
        if 'http' == namespace:
            continue

        file_path = os.path.join(
            output_dir, namespace,
            name_style_util.camel_to_underline(msg).lower().replace(
                '_req', '') + '_test.py')
        file_util.del_file(file_path)
        file_util.make_dir(file_util.file_dir(file_path))

        try:
            fp = open(file_path, 'wb')

            output_file_content(fp, msg, pkg_name, namespace)
            fp.close()

            print('=== generate %s done ===' % file_path)
        except Exception as e:
            print('exception: %s' % e)
            return -1

    return 0
Exemplo n.º 7
0
def output_cpp_file_content(fp, msg, pkg_name, namespace):
    try:
        if 'proto_tcp' == namespace:
            namespace_begin = 'namespace tcp%s{%snamespace proto%s{' % (os.linesep, os.linesep, os.linesep)
            namespace_end = '}%s}' % os.linesep

        class_name = '%sHandler' % msg

        content = ['#include "%s_handler.h"' % name_style_util.camel_to_underline(msg).lower(),
                   '''
#include "cs_msg.pb.h"
#include "cs_msg_id.pb.h"
#include "err_code.h"''', os.linesep,
                   '#include "%s_protobuf_util.h"' % namespace, os.linesep * 2,
                   'using namespace %s;' % pkg_name, os.linesep * 2,
                   '%s' % namespace_begin, os.linesep,
                   '%s::%s()' % (class_name, class_name), os.linesep, '{', os.linesep, '}', os.linesep * 2,
                   '%s::~%s()' % (class_name, class_name), os.linesep, '{', os.linesep, '}', os.linesep * 2]

        if namespace == 'http':
            content.insert(1, '''
#include "evhttp.h"
#include "json/json.h"''')
            content.insert(6, '#include "http_conn_center_interface.h"%s' % (os.linesep * 2))

        fp.write(''.join(content).encode('utf-8'))

        if namespace == 'proto_tcp':
            req_msg_name = name_style_util.camel_to_underline(msg).lower()
            rsp_msg_name = name_style_util.camel_to_underline(msg).lower().replace('req', 'rsp')

            content = ['::proto::MsgID %s::GetMsgID()' % class_name, os.linesep, '{', os.linesep,
                       '    return cs::MSG_ID_%s;' % (req_msg_name.upper()), os.linesep,
                       '}', os.linesep * 2,
                       'void %s::OnMsg(const ConnGUID* conn_guid, const ::proto::MsgHead& msg_head, const void* msg_body, size_t msg_body_len)' % class_name,
                       os.linesep,
                       '{', os.linesep,
                       '    LOG_TRACE("%s::OnMsg");' % class_name, os.linesep * 2,
                       '    cs::%s %s;' % (msg, req_msg_name), os.linesep,
                       '    if (ParseProtobufMsg(&%s, msg_body, msg_body_len) != 0)' % req_msg_name, os.linesep,
                       '    {', os.linesep,
                       '        LOG_ERROR("failed to parse msg, msg id: " << msg_head.msg_id << ", msg body len: " << msg_body_len);',
                       os.linesep,
                       '        SendErrRsp(conn_guid, msg_head, ERR_INVALID_PARAM);', os.linesep,
                       '        return;', os.linesep,
                       '    }', os.linesep * 2,
                       '    // TODO', os.linesep * 2,
                       '    ////////////////////////////////////////////////////////////////////////////////',
                       os.linesep,
                       '    ::proto::MsgHead rsp_msg_head = msg_head;', os.linesep,
                       '    rsp_msg_head.msg_id = cs::MSG_ID_%s;' % rsp_msg_name.upper(), os.linesep * 2,
                       '    cs::%s %s;' % (msg.replace('Req', 'Rsp'), rsp_msg_name), os.linesep,
                       '    %s.mutable_err_ctx()->set_err_code(ERR_OK);' % rsp_msg_name, os.linesep * 2,
                       '    if (SendToClient(logic_ctx_->scheduler, conn_guid, rsp_msg_head, &%s) != 0)' % rsp_msg_name,
                       os.linesep,
                       '    {', os.linesep,
                       '        LOG_ERROR("failed to send to client, " << conn_guid << ", msg id: " << rsp_msg_head.msg_id);',
                       os.linesep,
                       '        return;', os.linesep,
                       '    }', os.linesep,
                       '}', os.linesep * 2,
                       'void %sHandler::SendErrRsp(const ConnGUID* conn_guid, const ::proto::MsgHead& req_msg_head, int err_code) const' % msg,
                       os.linesep, '{', os.linesep,
                       '    ::proto::MsgHead rsp_msg_head = req_msg_head;', os.linesep,
                       '    rsp_msg_head.msg_id = cs::MSG_ID_%s;' % rsp_msg_name.upper(), os.linesep * 2,
                       '    cs::%s %s;' % (msg.replace('Req', 'Rsp'), rsp_msg_name), os.linesep,
                       '    %s.mutable_err_ctx()->set_err_code(err_code);' % rsp_msg_name, os.linesep * 2,
                       '    SendToClient(logic_ctx_->scheduler, conn_guid, rsp_msg_head, &%s);' % rsp_msg_name,
                       os.linesep,
                       '}', os.linesep]
            fp.write(''.join(content).encode('utf-8'))
        elif namespace == 'http':
            content = ['const char* %s::GetHttpReqPath()' % class_name, os.linesep, '{', os.linesep,
                       '    return "/%s"' % name_style_util.camel_to_underline(msg).lower(), os.linesep,
                       '}', os.linesep * 2,
                       'void %s::OnHttpHeadReq(const ConnGuid* conn_guid, struct evhttp_request* evhttp_req, bool https, const KeyValMap* http_header_map, const KeyValMap* http_query_map)' % class_name,
                       os.linesep, '{', os.linesep, '}', os.linesep * 2,
                       'void %s::OnHttpGetReq(const ConnGuid* conn_guid, struct evhttp_request* evhttp_req, bool https, const KeyValMap* http_header_map, const KeyValMap* http_query_map)' % class_name,
                       os.linesep, '{', os.linesep, '}', os.linesep * 2,
                       'void %s::OnHttpPostReq(const ConnGuid* conn_guid, struct evhttp_request* evhttp_req, bool https, const KeyValMap* http_header_map, const KeyValMap* http_query_map, const char* data, int data_len)' % class_name,
                       os.linesep, '{', os.linesep, '}', os.linesep * 2,
                       'void %s::OnHttpPutReq(const ConnGuid* conn_guid, struct evhttp_request* evhttp_req, bool https, const KeyValMap* http_header_map, const KeyValMap* http_query_map, const char* data, int data_len)' % class_name,
                       os.linesep, '{', os.linesep, '}', os.linesep]
            fp.write(''.join(content).encode('utf-8'))
        else:
            return -1

        content = [namespace_end, os.linesep]
        fp.write(''.join(content).encode('utf-8'))

        return 0
    except Exception as e:
        print('exception: %s' % e)
        return -1
def output_file_content(fp, msg, pkg_name, namespace):
    try:
        content = '''# -*- coding: utf-8 -*-

###############################################################################
# author: BrentHuang ([email protected])
###############################################################################

import os
import sys

sys.path.append('%s/../../../../py_tools' % os.path.split(os.path.realpath(__file__))[0])
# print(sys.path)

from util.proto_msg_codec import *
import conf
from data.err_code import *

'''
        fp.write(content.encode('utf-8'))

        content = [
            'from %s import cs_msg_id_pb2' % pkg_name.replace('::', '.'),
            os.linesep,
            'from %s import cs_msg_pb2' % pkg_name.replace('::', '.'),
            os.linesep * 3
        ]
        fp.write(''.join(content).encode('utf-8'))

        content = [
            'class %s(object):' % msg, os.linesep,
            '    def __init__(self, client):', os.linesep,
            '        self.client = client', os.linesep * 2
        ]
        fp.write(''.join(content).encode('utf-8'))

        if msg.endswith('Req'):
            content = [
                '    # TODO', os.linesep, '    def set_xx(self, xx):',
                os.linesep, '        self.xx = xx', os.linesep * 2
            ]
            fp.write(''.join(content).encode('utf-8'))

            req_instance = name_style_util.camel_to_underline(msg).lower()
            content = [
                '    def __make_%s(self):' % req_instance, os.linesep,
                '        %s = cs_msg_pb2.%s()' % (req_instance, msg),
                os.linesep, '        # TODO', os.linesep * 2,
                '        LOG_DEBUG(\'%%s%%s\' %% (os.linesep, %s))' %
                req_instance, os.linesep,
                '        return %s' % req_instance, os.linesep * 2
            ]
            fp.write(''.join(content).encode('utf-8'))

            rsp_instance = name_style_util.camel_to_underline(
                msg).lower().replace('req', 'rsp')
            content = [
                '    def __make_%s(self, rsp_msg_body):' % rsp_instance,
                os.linesep,
                '        %s = cs_msg_pb2.%s()' %
                (rsp_instance, msg.replace('Req', 'Rsp')), os.linesep,
                '        %s.ParseFromString(rsp_msg_body)' % rsp_instance,
                os.linesep * 2,
                '        LOG_DEBUG(\'%%s%%s\' %% (os.linesep, %s))' %
                rsp_instance, os.linesep,
                '        return %s' % rsp_instance, os.linesep * 2
            ]
            fp.write(''.join(content).encode('utf-8'))

            content = [
                '    def %s(self):' % req_instance.replace('_req', ''),
                os.linesep,
                '        LOG_DEBUG(\'----- %s -----\')' % req_instance,
                os.linesep * 2,
                '        %s_msg_head = MsgHead()' % req_instance, os.linesep,
                '        %s_msg_head.msg_id = cs_msg_id_pb2.MSG_ID_%s' %
                (req_instance,
                 name_style_util.camel_to_underline(msg).upper()),
                os.linesep * 2,
                '        %s = self.__make_%s()' % (req_instance, req_instance),
                os.linesep * 2
            ]
            fp.write(''.join(content).encode('utf-8'))

            if 'proto_tcp' == namespace:
                content = [
                    '        ret = self.client.send(%s_msg_head, %s.SerializeToString(), %s.ByteSize(), conf.proto_tcp_do_checksum)'
                    % (req_instance, req_instance, req_instance), os.linesep
                ]
            fp.write(''.join(content).encode('utf-8'))

            content = [
                '        if ret != 0:', os.linesep,
                '            LOG_ERROR(\'failed to send to server %s\' % self.client.server())',
                os.linesep, '            return -1', os.linesep * 2
            ]
            fp.write(''.join(content).encode('utf-8'))

            if 'proto_tcp' == namespace:
                content = [
                    '        %s_msg_head = MsgHead()' % rsp_instance,
                    os.linesep * 2,
                    '        ret, %s_msg_head, %s_msg_body = self.client.recv(conf.proto_tcp_do_checksum)'
                    % (rsp_instance, rsp_instance), os.linesep
                ]
            fp.write(''.join(content).encode('utf-8'))

            rsp_msg_head = '%s_msg_head' % rsp_instance
            content = [
                '        if ret != 0:', os.linesep,
                '            LOG_ERROR(\'ret: %d\' % ret)', os.linesep,
                '            return -1', os.linesep * 2,
                '        if %s.msg_id != cs_msg_id_pb2.MSG_ID_%s:' %
                (rsp_msg_head,
                 name_style_util.camel_to_underline(msg).upper().replace(
                     'REQ', 'RSP')), os.linesep,
                '            LOG_ERROR(\'error rsp msg id: %d\' % ' +
                rsp_msg_head + '.msg_id)', os.linesep, '            return -1',
                os.linesep * 2,
                '        %s = self.__make_%s(%s_msg_body)' %
                (rsp_instance, rsp_instance, rsp_instance), os.linesep * 2,
                '        err_code = %s.err_ctx.err_code' % rsp_instance,
                os.linesep, '        if err_code != 0:', os.linesep,
                '            LOG_ERROR(\'failed err code: %d\' % err_code)',
                os.linesep, '            return err_code', os.linesep * 2,
                '        return 0', os.linesep
            ]
            fp.write(''.join(content).encode('utf-8'))
        else:
            nfy_instance = name_style_util.camel_to_underline(msg).lower()
            content = [
                '    def __make_%s(self, nfy_msg_body):' % nfy_instance,
                os.linesep,
                '        %s = cs_msg_pb2.%s()' % (nfy_instance, msg),
                os.linesep,
                '        %s.ParseFromString(nfy_msg_body)' % nfy_instance,
                os.linesep * 2,
                '        LOG_DEBUG(\'%%s%%s\' %% (os.linesep, %s))' %
                nfy_instance, os.linesep,
                '        return %s' % nfy_instance, os.linesep * 2
            ]
            fp.write(''.join(content).encode('utf-8'))

            content = [
                '    def %s(self):' % nfy_instance.replace('_nfy', ''),
                os.linesep,
                '        LOG_DEBUG(\'----- %s -----\')' % nfy_instance,
                os.linesep * 2
            ]
            fp.write(''.join(content).encode('utf-8'))

            nfy_msg_head = '%s_msg_head' % nfy_instance

            if 'proto_tcp' == namespace:
                content = [
                    '        %s = MsgHead()' % nfy_msg_head, os.linesep * 2,
                    '        ret, %s, %s_msg_body = self.client.recv(conf.proto_tcp_do_checksum)'
                    % (nfy_msg_head, nfy_instance), os.linesep
                ]
            fp.write(''.join(content).encode('utf-8'))

            content = [
                '        if ret != 0:', os.linesep,
                '            LOG_ERROR(\'ret: %d\' % ret)', os.linesep,
                '            return -1', os.linesep * 2,
                '        if %s.msg_id != cs_msg_id_pb2.MSG_ID_%s:' %
                (nfy_msg_head,
                 name_style_util.camel_to_underline(msg).upper()), os.linesep,
                '            LOG_ERROR(\'error nfy msg id: %d\' % ' +
                nfy_msg_head + '.msg_id)', os.linesep, '            return -1',
                os.linesep * 2,
                '        %s = self.__make_%s(%s_msg_body)' %
                (nfy_instance, nfy_instance, nfy_instance), os.linesep * 2,
                '        return 0', os.linesep
            ]
            fp.write(''.join(content).encode('utf-8'))

        return 0
    except Exception as e:
        print('exception: %s' % e)
        return -1