【问题标题】:Structure size so big, need optimization结构尺寸这么大,需要优化
【发布时间】:2023-03-12 23:11:01
【问题描述】:

我的一个功能的代码有一个很大的问题我为一个游戏编写了这个代码,将数据包从服务器发送到客户端,一切正常,但问题在于服务器尺寸太大.. 尺寸更小,如 100-300效果很好,但我的源代码有问题,因为它们有保护来检查数据包中的缓冲区,如果太棒会阻止发送功能,所以我需要其他我认为的功能或很多优化或其他结构.. 嗯

这里有问题 - TPacket list[1000];

typedef struct testa
{
    char    t_A[10 + 1];
    char    t_B[12 + 1];
    char    t_C[32 + 1];
    char    t_D[512 + 1];
    int     t_E;
    char    t_F[19 + 1];
    int     t_G;
} TPacket;

typedef struct testb
{
    BYTE header;
    TPacket list[1000]; // (If i put example 200 etc work) but when is so big = buffer mem_size overflow. memsize(131072) write_pos(32) iSize(598001)
} Test;

// FUNCTION TO SEND:
    Test p;
    p.header = HEADER_GC_T;

    SQLMsg *pMsg = DBManager::instance().DirectQuery("SELECT * FROM table.list ORDER BY date DESC LIMIT 1000");
    MYSQL_ROW row;
    int i = 0;

    if(pMsg->uiSQLErrno != 0)
        return;

    while ((row = mysql_fetch_row(pMsg->Get()->pSQLResult)))
    {
        p.list[i] = TPacket();
        strncpy(p.list[i].t_A, row[1], sizeof(p.list[i].t_A));
        strncpy(p.list[i].t_B, row[2], sizeof(p.list[i].t_B));
        strncpy(p.list[i].t_C, row[3], sizeof(p.list[i].t_C));
        strncpy(p.list[i].t_D, row[4], sizeof(p.list[i].t_D));
        str_to_number(p.list[i].t_E, row[5]);
        strncpy(p.list[i].t_F, row[6], sizeof(p.list[i].t_F) - 1);
        str_to_number(p.list[i].t_G, row[7]);
        i++;
    }

    if(pMsg->Get()->uiNumRows < 1000)
    {
        while (i < 1000)
        {
            p.list[i] = TPacket();
            strncpy(p.list[i].t_A, "", sizeof(p.list[i].t_A));
            strncpy(p.list[i].t_B, "", sizeof(p.list[i].t_B));
            strncpy(p.list[i].t_C, "", sizeof(p.list[i].t_C));
            strncpy(p.list[i].t_D, "", sizeof(p.list[i].t_D));          
            p.list[i].t_E = 0;          
            strncpy(p.list[i].t_F, "", sizeof(p.list[i].t_F) - 1);          
            p.list[i].t_G = 0;              
            i++;
        }
    }
    ch->GetDesc()->Packet(&p, sizeof(p));

【问题讨论】:

  • 听起来真正的问题是您需要使用非阻塞发送。
  • 您使用的是 C 还是 C++?如果是 C++,我建议 std::string 用于 cstring 和 std::vector 替换其他数组。
  • 这对你的堆栈来说太大了。将TPacket list[1000]; 替换为std::vector&lt;TPacket&gt; list(1000);
  • 正如@πάνταῥεῖ 所说,您正在达到堆栈帧大小限制。您可以使用全局变量或动态分配来实现。
  • “他们有保护从数据包中检查缓冲区”——“他们”是谁,你在说什么数据包?

标签: c++ c gcc struct packet


【解决方案1】:

您的Test 结构非常大,588,000 字节,对于自动存储来说可能太大了。设置为static 应该可以解决问题,但会使您的代码不可重入且绝对不是线程安全的。

如果问题出在最大数据包大小上,您必须将传输分成更小的数据包。在结构和 SQL SELECT 语句中使用较少数量的项目。

strncpy 如果字符串比目标数组长,则不会终止字符串。你永远不应该使用这个功能。阅读为什么你应该stop using strncpy already!。您可以改为使用其他函数,该函数使用截断进行复制,但会以 null 终止目标。

清除循环可以大大简化,假设 TPacket 的默认构造函数生成一个初始化为所有位 0 的 TPacket。如果没有,只需使用 memset 来执行此操作。

typedef struct testa {
    char t_A[10 + 1];
    char t_B[12 + 1];
    char t_C[32 + 1];
    char t_D[512 + 1];
    int  t_E;
    char t_F[19 + 1];
    int  t_G;
} TPacket;

typedef struct testb {
    BYTE header;
    TPacket list[200];
} Test;

// Utility function: copy with truncation, return source string length
// truncation occurred if return value >= size argument
size_t bstrcpy(char *dest, size_t size, const char *src) {
    size_t i;
    /* copy the portion that fits */
    for (i = 0; i + 1 < size && src[i] != '\0'; i++) {
         dest[i] = src[i];
    }
    /* null terminate destination if possible */
    if (i < size) {
        dest[i] = '\0';
    }
    /* compute necessary length to allow truncation detection */
    while (src[i] != '\0') {
        i++;
    }
    return i;
}

// FUNCTION TO SEND:
void myfunction() {
    Test p;
    p.header = HEADER_GC_T;

    SQLMsg *pMsg = DBManager::instance().DirectQuery("SELECT * FROM table.list ORDER BY date DESC LIMIT 200");
    MYSQL_ROW row;
    int i = 0;

    if (pMsg->uiSQLErrno != 0)
        return;

    while ((row = mysql_fetch_row(pMsg->Get()->pSQLResult))) {
        p.list[i] = TPacket();
        bstrcpy(p.list[i].t_A, sizeof(p.list[i].t_A), row[1]);
        bstrcpy(p.list[i].t_B, sizeof(p.list[i].t_B), row[2]);
        bstrcpy(p.list[i].t_C, sizeof(p.list[i].t_C), row[3]);
        bstrcpy(p.list[i].t_D, sizeof(p.list[i].t_D), row[4]);
        str_to_number(p.list[i].t_E, row[5]);
        bstrcpy(p.list[i].t_F, sizeof(p.list[i].t_F), row[6]);
        str_to_number(p.list[i].t_G, row[7]);
        i++;
    }

    if (i < 1000) {
        memset(&p.list[i], 0, (1000 - i) * sizeof(p.list[i]));
        //while (i < 1000) {
        //    p.list[i] = TPacket();
        //    i++;
        //}
    }
    ch->GetDesc()->Packet(&p, sizeof(p));

【讨论】:

  • @Dr.Vendetta:你为什么不再认为这个答案可以接受?
猜你喜欢
  • 2010-12-28
  • 1970-01-01
  • 2012-06-16
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-02
相关资源
最近更新 更多