开发者

Buffering of stream data

开发者 https://www.devze.com 2022-12-30 12:49 出处:网络
I\'m trying to develop a simple IRC bot. First I want to think out a proper design for this project开发者_如何学Python. One of the things I\'m wondering about right now is the read mechanism. I develo

I'm trying to develop a simple IRC bot. First I want to think out a proper design for this project开发者_如何学Python. One of the things I'm wondering about right now is the read mechanism. I develop this bot on a Linux system. (Fedora 12) To read from a socket I use the system call "read()". I plan to use the reading functionality in the following way (code just an example. Not something from the final product):

while (uBytesRead = read(iServerSocket, caBuffer, MAX_MESSAGE_SIZE))
{
   //1. Parse the buffer and place it into a Message structure.
   //2. Add the message structure to a linked list that will act as a queue of message that are to be processed.
}

This code will be run in it's own thread. I choose for this option because I wanted there to be as small of a delay between reads as possible. (writes will be implemented in the same way) This is all slightly based on assumptions, that I would like to clear up. My question is: what if you receive so much data at such a quick rate, that the reading and processing the data (in this case just parsing it) goes slower than the rate at which data comes in. I made the assumption that this data will be buffered by the system. is this a right assumption? And if so:

  1. How big is this buffer?
  2. What happens with incomming data when this buffer gets full?
  3. To make my application protected against spam, how could I best deal with it?

I hope I've explained my issue clear enough.

Thanks in advance.


IRC uses TCP sockets for networking. Linux/Posix TCP sockets have a data buffer for sending and another one for receiving. You can resize the buffers with setsockopt() and SO_SNDBUF/SO_RCVBUF.

TCP has flow control so when a receive buffer is getting full the OS will send a congestion notice. Received packets that didn't fit in the buffer will not be acknowledged by the receiver and will be eventually retransmitted by the sender.

So that's not to worry. What matters is what does the sender program when its socket's send buffer gets full. Some programs will close the socket, others would just discard written data and try again, while others might buffer internally.

0

精彩评论

暂无评论...
验证码 换一张
取 消