uint16_t uint8_t还是size_t ?

时间:2021-07-12 19:58:11

I would like to use a function which is charged to send both "data size" and "data" to a specific file descriptor by using write(). It works when the record length is equal to 2 bytes. However, I would like to use the same function to send also record length equal to 1 byte.

我想使用一个函数,它通过write()将“数据大小”和“数据”发送到特定的文件描述符。当记录长度等于2字节时,它可以工作。但是,我想使用相同的函数发送也记录长度为1字节。

send_func(int port)
{
    void *fd;
    uint64_t fsize = 2517283;
    uint64_t nbrBytes = 0;  
    uint16_t rsize;
    int count;
    ssize_t ret;
    uint8_t Bsent = 0;

    for (count = 1; nbrBytes < fsize; count++) 
    {
        rsize = ((uint8_t*)fp)[nbrBytes];
        rsize += ((((uint8_t*)fp)[nbrBytes + 1]) << 8) & 0xFF00;
        nbrBytes += 2;

        // send size
        ret = write(port, rsize, 2);
        if (ret != 2) {
            return -1;
        }
        // send data
        ret = write(port, ((unsigned char*)fp) + Bsent, rsize - Bsent);
        if (ret < 0) {
            return -1;
        }
        Bsent += ret;
    }
}

send_func(int port)
{
    void *fd;
    uint64_t fsize = 2517283;
    uint64_t nbrBytes = 0;  
    size_t rsize;
    int count;
    ssize_t ret;
    uint8_t Bsent = 0;

    for (count = 1; nbrBytes < fsize; count++) 
    {
        if (mode == ONLY_1_BYTE) {
            rsize = ((uint8_t*)fp)[nbrBytes];
            rsize += ((((uint8_t*)fp)[nbrBytes + 1])); 
            nbrBytes += 1;

            do {
                // send data
                ret = write(port, ((unsigned char*)fp) + Bsent, rsize - Bsent);
                if (ret < 0) {
                    return -1;
                }  
                Bsent += ret;
            } while(Bsent < rsize)     
        }
        else
        {
            rsize = ((uint8_t*)fp)[nbrBytes];
            rsize += ((((uint8_t*)fp)[nbrBytes + 1]) << 8) & 0xFF00;
            nbrBytes += 2;   

            // send size
            ret = write(port, rsize, sizeof(uint16_t));
            if (ret != 2) {
                return -1;
            }        
        }

        do {
            // send data
            ret = write(port, ((unsigned char*)fp) + Bsent, rsize - Bsent);
            if (ret < 0) {
                return -1;
            }  
            Bsent += ret;
        } while(Bsent < rsize)
    }
}

Because there is only 1 length byte in the second case, I voluntarily removed the endianity operation, which is compulsory in case of 2 bytes.

因为在第二种情况下只有1个字节,所以我自动删除了endieness操作,这在2个字节的情况下是必须的。

Is it the best way to practice ?

这是最好的练习方式吗?

1 个解决方案

#1


1  

Some best practices that you can apply to improve the code you posted:

您可以应用一些最佳实践来改进您发布的代码:

  • Don't worry about optimizing a single byte out of the stream. It doesn't matter. Did you know that every Ethernet frame takes about 60 bytes of overhead aside from your payload?
  • 不要担心从流中优化一个字节。没关系。你知道吗,除了有效负载外,每个以太网帧都要占用60字节的开销。
  • Don't hand-roll endian swaps. Use built in functions like htons().
  • 不要使用字节存储次序互换。使用像htons()这样的函数。
  • You need to account for "short writes" where the return value from write() is less than you tried to send. When that happens you need to loop and call write() again without re-sending a length prefix.
  • 您需要说明“短写”,其中write()的返回值小于您尝试发送的值。当发生这种情况时,您需要再次循环并调用write(),而无需重新发送长度前缀。

#1


1  

Some best practices that you can apply to improve the code you posted:

您可以应用一些最佳实践来改进您发布的代码:

  • Don't worry about optimizing a single byte out of the stream. It doesn't matter. Did you know that every Ethernet frame takes about 60 bytes of overhead aside from your payload?
  • 不要担心从流中优化一个字节。没关系。你知道吗,除了有效负载外,每个以太网帧都要占用60字节的开销。
  • Don't hand-roll endian swaps. Use built in functions like htons().
  • 不要使用字节存储次序互换。使用像htons()这样的函数。
  • You need to account for "short writes" where the return value from write() is less than you tried to send. When that happens you need to loop and call write() again without re-sending a length prefix.
  • 您需要说明“短写”,其中write()的返回值小于您尝试发送的值。当发生这种情况时,您需要再次循环并调用write(),而无需重新发送长度前缀。