I'm trying to make gateway using Raspberry Pi which needs to push data periodically but some times internet connection lost. Is there a way to store received data and push it when connection available?
1 Answers
The good news is that sending data across a TCP/IP connection is not block-specific so socket.send ("Foo\n"); socket.send ("Bar\n"); is identical to socket.send ("Foo\nBar\n");
Therefore, you can avoid piecemeal memory allocation by just allocating a block of, say, 64K and appending your data to it. If it fills up, you can either allocate another one and link the two together or flush it out and note that you've dropped the data - something you have to code for anyway since eventually you would run out of space.
So, data arrives: put it at the end of the buffer and try to send the entire filled part of the buffer (the new data and anything that was already there). If the send succeeds, clear the buffer.
To keep things neat, when your internet connection is established flush the contents of the buffer (if any) across the connection.

- 2,721
- 1
- 15
- 20