Search code examples
crubysocketsposix-select

is it possible to pause/resume part of a full-duplex connection?


Yes, no?

How can I achieve something like this:

In Client side

loop do
    socket.read(MAX_LEN)
    socket.pause_read   # the client is busy, do not send any more data, similar to: C system call shutdown(sock, SHUT_RD)
    # ... working

    socket.resume_read  # the client is ready to work again. 
end

In Server side

loop do
    rd, wr, _ = select(rdsockets, wrsockets)
    # wr is an array fill of clients ready to work
    wr.each {}
end

PS: Help can be provided either with C or Ruby. Thank you in advance.

EDIT: The incoming data has a predefined format and the length of the incoming data varies.

EDIT2: OS: Linux


Solution

  • That's how it already works if you don't do anything. The "do work" will take some time, so the client won't call read again until it finishes. The server will keep sending data for a little while, until the client's receive buffer (part of the system TCP implementation) fills up; then the server will be throttled and select won't return that client as writable anymore. The overall effect is that for a long-lived connection, the server will only send data as fast as the client can process it.

    If you need fine-grained control over the size of the receive buffer on the client, you can use setsockopt(SO_RCVBUF, size).