简体   繁体   中英

How to split and join array in C++ for UDP?

I have a byte array like this:

lzo_bytep out; // my byte array
size_t uncompressedImageSize = 921600;

out = (lzo_bytep) malloc((uncompressedImageSize + 
          uncompressedImageSize / 16 + 64 + 3));
wrkmem = (lzo_voidp) malloc(LZO1X_1_MEM_COMPRESS);

// Now the byte array has 802270 bytes
r = lzo1x_1_compress(imageData, uncompressedImageSize,
        out, &out_len, wrkmem);

How can I split it into smaller parts under 65,535 bytes (the byte array is one large image which I want to sent over UDP which has upper limit 65,535 bytes) and then join those small chunks back into a continuous array?

The problem with doing this is that the UDP packets can arrive out or order, or be dropped. Use TCP for this; that's what it's for.

You don't have to "split" the array. You just have to point into different parts of it.

Assuming you're using a typical UDP write() function, it takes several arguments. One of them is a pointer to the buffer and the other is the length.

If you want to get the first 65535 bytes, your buffer is at wrkmem and the length is 65535.

For the second 65535 bytes, your buffer is at wrkmem + 65535 and your length is 65535.

The third 65535 bytes, your buffer is at wrkmem + 2 * 65535 and your length is 65535.

Get it?

(That said, the other posters are correct. You should be using TCP).

On the other side, when you want to re-join the array, you must allocate enough memory for the whole thing, then use a copy function like memcpy() to copy the arriving chunks into their correct position. Remember that UDP may not deliver the pieces in order and may not deliver all of them.

You might wish to try a message based middleware like ØMQ and feed the entire compressed image as one message and have the middleware run asynchronously and manage redelivery at the fastest speed possible. It provides a BSD socket compatible API and so can be easy to migrate code over and allows you to easily swap between various underlying transport protocols as required.

Other message systems are available.

void my_free (void *data, void *hint)
{
    free (data);
}

    /*  ...  */

size_t uncompressedImageSize = 921600, compressedImageSize = 0;
size_t out_len = (uncompressedImageSize + uncompressedImageSize / 16 + 64 + 3);
lzo_bytep out = (lzo_bytep)malloc (out_len);
lzo_voidp wkrmem = (lzo_voidp)malloc (LZO1X_1_MEM_COMPRESS);
zmq_msg_t msg;

rc = lzo1x_1_compress (imageData, uncompressedImageSize,
                       out, &compressedImageSize, wrkmem);
assert (compressedImageSize > 0);
rc = zmq_msg_init_data (&msg, out, compressedImageSize, my_free, NULL);
assert (rc == 0);
/* Send the message to the socket */
rc = zmq_send (socket, &msg, 0);
assert (rc == 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM