Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent client from deadlocking/halting on large transfer by throwing error #61

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ use crate::recv::ReceiveBuffer;
use crate::send::SendBuffer;
use crate::sent::SentPackets;
use crate::seq::CircularRangeInclusive;
use crate::stream::BUFFER_CAPACITY;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Error {
BufferCapacityExceeded,
EmptyDataPayload,
InvalidAckNum,
InvalidFin,
Expand All @@ -32,6 +34,7 @@ enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::BufferCapacityExceeded => "data written exceeds buffer capacity",
Self::EmptyDataPayload => "missing payload in DATA packet",
Self::InvalidAckNum => "received ACK for unsent packet",
Self::InvalidFin => "received multiple FIN packets with distinct sequence numbers",
Expand All @@ -52,8 +55,8 @@ impl From<Error> for io::ErrorKind {
fn from(value: Error) -> Self {
use Error::*;
match value {
EmptyDataPayload | InvalidAckNum | InvalidFin | InvalidSeqNum | InvalidSyn
| SynFromAcceptor => io::ErrorKind::InvalidData,
BufferCapacityExceeded | EmptyDataPayload | InvalidAckNum | InvalidFin
| InvalidSeqNum | InvalidSyn | SynFromAcceptor => io::ErrorKind::InvalidData,
Reset => io::ErrorKind::ConnectionReset,
TimedOut => io::ErrorKind::TimedOut,
}
Expand Down Expand Up @@ -461,6 +464,12 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
send_buf.write(&data).unwrap();
let _ = tx.send(Ok(data.len()));
self.writable.notify_one();
} else if data.len() > BUFFER_CAPACITY {
// TODO: refactor SendBuffer so we have 1 send buffer instead of 2
let (_, tx) = self.pending_writes.pop_front().unwrap();
let _ = tx.send(Err(io::Error::from(io::ErrorKind::from(
Error::BufferCapacityExceeded,
))));
} else {
break;
}
Expand Down
11 changes: 8 additions & 3 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::packet::Packet;

/// The size of the send and receive buffers.
// TODO: Make the buffer size configurable.
const BUF: usize = 1024 * 1024;
pub const BUFFER_CAPACITY: usize = 1024 * 1024;

pub struct UtpStream<P> {
cid: ConnectionId<P>,
Expand All @@ -31,8 +31,13 @@ where
stream_events: mpsc::UnboundedReceiver<StreamEvent>,
connected: oneshot::Sender<io::Result<()>>,
) -> Self {
let mut conn =
conn::Connection::<BUF, P>::new(cid.clone(), config, syn, connected, socket_events);
let mut conn = conn::Connection::<BUFFER_CAPACITY, P>::new(
cid.clone(),
config,
syn,
connected,
socket_events,
);

let (shutdown_tx, shutdown_rx) = oneshot::channel();
let (reads_tx, reads_rx) = mpsc::unbounded_channel();
Expand Down