Once again, I'm turning to the monks in hopes that somebody has sorted this mess out before. I have a class (let's call it Acme::Connection) which handles tcp connections and abstracts them a bit.
The question at the heart of this node is: What is the correct way to code for tcp sending and receiving in perl such that you handle all possible error conditions correctly, quickly, and efficiently, with gauranteed maximum timeouts of varying amounts throughout? (A maximum timeout for connecting, for recv()ing the first packet, perhaps a longer timeout for recv()ing the next packet, etc)...
And what that question really boils down to is - for a nonblocking tcp connection, what is the correct way to handle all the possible outcomes of calling $socket->recv($numbytes);.
There's the actual return value we can check for defined-ness, there's the definedness and/or length of the received data versus the $numbytes we asked for, and there's several different relevant errnos which require different sorts of action. it ends up being a matrix of many possibilities and few documented gaurantees... In the case of send() the possibilities are even more complex, but in practice it tends to never fail for reasonably-sized packets on an open connection.
Specifically, the expected behavior is that one can open a connection with:
my $conn = Acme::Connection->new({
ip_addr => '1.2.3.4',
port => 666,
def_timeout => 12
});
And that one can send or receive a chunk of data with the following calls:
my $data = $conn->recv(3124);
$conn->send($data);
The expected behavior is that $conn->recv(3124) will attempt to receive 3124 bytes of data over the network, and will fail with a timeout exception if that doesn't occur within 12 seconds (set with def_timeout in the constructor).
Similarly, $conn->send($data) should send the contents of $data, and fail with a timeout exception if that doesn't happen in 12 seconds.
I've truncated what used to be here, it was a big peice of ugly code, the current implementation at the time of the question.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.