QUIC Protocol Implementation 1.0
A Python implementation of the QUIC (Quick UDP Internet Connections) protocol.
Loading...
Searching...
No Matches
quic.QuicConnection Class Reference
Collaboration diagram for quic.QuicConnection:

Public Member Functions

 __init__ (self, int connection_id, tuple local_addr, tuple remote_addr)
 
'Streamget_stream (self, int initiated_by, int direction)
 
 add_file_to_stream (self, int stream_id, str path)
 
 send_packets (self)
 
 receive_packets (self)
 

Protected Member Functions

int _stream_id_generator (self, int initiated_by, int direction)
 
 _add_stream (self, int stream_id, bool initiated_by, bool direction)
 
 _add_stream_to_stats_dict (self, int stream_id)
 
'Stream_get_stream_by_id (self, int stream_id)
 
'Stream_remove_stream (self, int stream_id)
 
 _add_data_to_stream (self, int stream_id, bytes data)
 
 _add_active_stream_id (self, int stream_id)
 
bool _is_stream_id_in_dict (self, int stream_id)
 
 _set_start_time (self)
 
 _send_packet_size (self)
 
Packet _create_packet (self)
 
 _generate_streams_frames (self)
 
Stream|None _get_stream_from_active_streams (self)
 
 _send_packet (self, bytes packet)
 
 _receive_packet (self)
 
 _increment_received_packets_counter (self)
 
 _handle_received_packet_size (self, bytes packet_size)
 
 _handle_received_packet (self, bytes packet)
 
bool _write_stream (self, int stream_id)
 
 _close_connection (self)
 
 _print_stats (self)
 

Protected Attributes

 _connection_id
 
 _local_addr
 
 _remote_addr
 
 _socket
 
 _stats_dict
 
 _streams_counter
 
 _sent_packets_counter
 
 _received_packets_counter
 
 _idle
 
 _packet_size
 
 _active_streams_ids
 
 _total_time
 

Detailed Description

@brief Manages a QUIC connection.

@details Handles stream creation, packet assembly, sending/receiving data,
         and tracking connection statistics.

Definition at line 22 of file quic.py.

Constructor & Destructor Documentation

◆ __init__()

quic.QuicConnection.__init__ (   self,
int  connection_id,
tuple  local_addr,
tuple  remote_addr 
)
@brief Initialize a QuicConnection instance.

@param connection_id The ID of the connection (0 for client, 1 for server).
@param local_addr The local address for the connection (IP, port).
@param remote_addr The remote address for the connection (IP, port).

Definition at line 30 of file quic.py.

Member Function Documentation

◆ _add_active_stream_id()

quic.QuicConnection._add_active_stream_id (   self,
int  stream_id 
)
protected
@brief Mark a stream as active.

@details Adds stream ID to the active streams list.

@param stream_id The ID of the stream to mark as active.

Definition at line 149 of file quic.py.

References quic.QuicConnection._active_streams_ids.

Referenced by quic.QuicConnection._add_data_to_stream(), and quic.QuicConnection._handle_received_packet().

◆ _add_data_to_stream()

quic.QuicConnection._add_data_to_stream (   self,
int  stream_id,
bytes  data 
)
protected
@brief Add data to a specific stream's buffer.

@param stream_id The ID of the stream.
@param data The data to add.

Definition at line 138 of file quic.py.

References quic.QuicConnection._add_active_stream_id(), and quic.QuicConnection._get_stream_by_id().

Referenced by quic.QuicConnection.add_file_to_stream().

◆ _add_stream()

quic.QuicConnection._add_stream (   self,
int  stream_id,
bool  initiated_by,
bool  direction 
)
protected
@brief Add a new stream to the connection.

@details Initializes the stream's statistics.

@param stream_id The ID of the stream to add.
@param initiated_by Indicates if the stream was initiated by the server.
@param direction Indicates if the stream is unidirectional.

Definition at line 82 of file quic.py.

References quic.QuicConnection._add_stream_to_stats_dict(), and quic.QuicConnection._streams_counter.

Referenced by quic.QuicConnection._get_stream_by_id(), and quic.QuicConnection.get_stream().

◆ _add_stream_to_stats_dict()

quic.QuicConnection._add_stream_to_stats_dict (   self,
int  stream_id 
)
protected
@brief Initialize statistics for a new stream.

@param stream_id The ID of the stream.

Definition at line 96 of file quic.py.

References quic.QuicConnection._stats_dict.

Referenced by quic.QuicConnection._add_stream().

◆ _close_connection()

quic.QuicConnection._close_connection (   self)
protected
@brief Close the connection, socket, and print the statistics.

Definition at line 353 of file quic.py.

References quic.QuicConnection._idle, quic.QuicConnection._print_stats(), quic.QuicConnection._socket, and quic.QuicConnection._total_time.

Referenced by quic.QuicConnection.send_packets().

◆ _create_packet()

Packet quic.QuicConnection._create_packet (   self)
protected
@brief Create a packet containing frames from the streams.

@details 1. Generate frames for each stream
         2. Assemble SOME of them and add to packet payload
         3. Add packet to pending packets

@return The created packet with frames from different streams.

Definition at line 200 of file quic.py.

References quic.QuicConnection._connection_id, quic.QuicConnection._generate_streams_frames(), quic.QuicConnection._get_stream_from_active_streams(), quic.QuicConnection._packet_size, quic.QuicConnection._remove_stream(), and quic.QuicConnection._sent_packets_counter.

Referenced by quic.QuicConnection.send_packets().

◆ _generate_streams_frames()

quic.QuicConnection._generate_streams_frames (   self)
protected
@brief Generate frames for each active stream.

Definition at line 235 of file quic.py.

References quic.QuicConnection._active_streams_ids, and quic.QuicConnection._get_stream_by_id().

Referenced by quic.QuicConnection._create_packet().

◆ _get_stream_by_id()

'Stream' quic.QuicConnection._get_stream_by_id (   self,
int  stream_id 
)
protected
@brief Retrieve a stream by its ID.

@details Creates the stream if necessary.

@param stream_id The ID of the stream to retrieve.
@return The retrieved or newly created stream.

Definition at line 104 of file quic.py.

References quic.QuicConnection._add_stream(), and quic.QuicConnection._is_stream_id_in_dict().

Referenced by quic.QuicConnection._add_data_to_stream(), quic.QuicConnection._generate_streams_frames(), quic.QuicConnection._handle_received_packet(), and quic.QuicConnection.get_stream().

◆ _get_stream_from_active_streams()

Stream | None quic.QuicConnection._get_stream_from_active_streams (   self)
protected
@brief Retrieve a stream from the list of active streams.

@return The retrieved stream, or None if no active streams.

Definition at line 242 of file quic.py.

References quic.QuicConnection._active_streams_ids, and quic.QuicConnection._idle.

Referenced by quic.QuicConnection._create_packet().

◆ _handle_received_packet()

quic.QuicConnection._handle_received_packet (   self,
bytes  packet 
)
protected
@brief Handle the reception of a packet and its frames.

@param packet The received packet in bytes.

Definition at line 314 of file quic.py.

References quic.QuicConnection._add_active_stream_id(), quic.QuicConnection._get_stream_by_id(), quic.QuicConnection._stats_dict, and quic.QuicConnection._write_stream().

◆ _handle_received_packet_size()

quic.QuicConnection._handle_received_packet_size (   self,
bytes  packet_size 
)
protected
@brief Handle the reception of the packet size from the peer.

@param packet_size The received packet size in bytes.

Definition at line 305 of file quic.py.

References quic.QuicConnection._packet_size.

◆ _increment_received_packets_counter()

quic.QuicConnection._increment_received_packets_counter (   self)
protected
@brief Increment the counter for received packets.

Definition at line 299 of file quic.py.

References quic.QuicConnection._received_packets_counter.

◆ _is_stream_id_in_dict()

bool quic.QuicConnection._is_stream_id_in_dict (   self,
int  stream_id 
)
protected
@brief Check if a stream ID exists in the streams dict.

@param stream_id The ID of the stream to check.
@return True if the stream ID exists, False otherwise.

Definition at line 160 of file quic.py.

Referenced by quic.QuicConnection._get_stream_by_id(), and quic.QuicConnection.get_stream().

◆ _print_stats()

quic.QuicConnection._print_stats (   self)
protected
@brief Print the statistics for all active streams in the connection.

Definition at line 362 of file quic.py.

References quic.QuicConnection._stats_dict, and quic.QuicConnection._total_time.

Referenced by quic.QuicConnection._close_connection().

◆ _receive_packet()

quic.QuicConnection._receive_packet (   self)
protected
@brief Receive a packet and process it.

@details If socket times out, the connection will be closed.

Definition at line 278 of file quic.py.

References quic.QuicConnection._packet_size, and quic.QuicConnection._socket.

Referenced by quic.QuicConnection.receive_packets().

◆ _remove_stream()

'Stream' quic.QuicConnection._remove_stream (   self,
int  stream_id 
)
protected
@brief Remove a stream from the active streams list and the streams dictionary.

@param stream_id The ID of the stream to remove.
@return The removed stream.

Definition at line 117 of file quic.py.

References quic.QuicConnection._active_streams_ids.

Referenced by quic.QuicConnection._create_packet(), and quic.QuicConnection._write_stream().

◆ _send_packet()

quic.QuicConnection._send_packet (   self,
bytes  packet 
)
protected
@brief Send a packet to the remote address.

@param packet The packet to send.
@return True if the packet was sent successfully, False otherwise.

Definition at line 257 of file quic.py.

References quic.QuicConnection._remote_addr, and quic.QuicConnection._socket.

Referenced by quic.QuicConnection.send_packets().

◆ _send_packet_size()

quic.QuicConnection._send_packet_size (   self)
protected
@brief Send the packet size to the remote peer.

@return True if the packet size was sent successfully, False otherwise.

Definition at line 190 of file quic.py.

Referenced by quic.QuicConnection.send_packets().

◆ _set_start_time()

quic.QuicConnection._set_start_time (   self)
protected
@brief Set the start time for all streams in the connection.

Definition at line 169 of file quic.py.

References quic.QuicConnection._stats_dict.

◆ _stream_id_generator()

int quic.QuicConnection._stream_id_generator (   self,
int  initiated_by,
int  direction 
)
protected
@brief Generate a unique stream ID.

@details Generates ID based on stream counter, initiator, and direction.

@param initiated_by Indicates whether the stream was initiated by 'client'(0) or 'server'(1).
@param direction Indicates whether the stream is bidirectional(0) or unidirectional(1).
@return The generated stream ID.

Definition at line 67 of file quic.py.

References quic.QuicConnection._streams_counter.

Referenced by quic.QuicConnection.get_stream().

◆ _write_stream()

bool quic.QuicConnection._write_stream (   self,
int  stream_id 
)
protected
@brief Write the received data of a stream to a file.

@param stream_id The ID of the stream whose data should be written.
@return True if the data was written successfully, False otherwise.

Definition at line 334 of file quic.py.

References quic.QuicConnection._remove_stream(), and quic.QuicConnection._stats_dict.

Referenced by quic.QuicConnection._handle_received_packet().

◆ add_file_to_stream()

quic.QuicConnection.add_file_to_stream (   self,
int  stream_id,
str  path 
)
@brief Add a file's content to a stream.

@param stream_id The ID of the stream to add the file to.
@param path The file path.

Definition at line 127 of file quic.py.

References quic.QuicConnection._add_data_to_stream().

◆ get_stream()

'Stream' quic.QuicConnection.get_stream (   self,
int  initiated_by,
int  direction 
)
@brief Retrieve or create a new stream for the connection.

@param initiated_by Indicates whether the stream was initiated by 'client'(0) or 'server'(1).
@param direction Indicates if the stream is bidirectional(0) or unidirectional(1).
@return The created or retrieved stream.

Definition at line 54 of file quic.py.

References quic.QuicConnection._add_stream(), quic.QuicConnection._get_stream_by_id(), quic.QuicConnection._is_stream_id_in_dict(), and quic.QuicConnection._stream_id_generator().

◆ receive_packets()

quic.QuicConnection.receive_packets (   self)
@brief Continuously receive packets until the connection is closed or a timeout occurs.

Definition at line 266 of file quic.py.

References quic.QuicConnection._idle, quic.QuicConnection._receive_packet(), and quic.QuicConnection._socket.

◆ send_packets()

quic.QuicConnection.send_packets (   self)

Member Data Documentation

◆ _active_streams_ids

◆ _connection_id

quic.QuicConnection._connection_id
protected

Definition at line 38 of file quic.py.

Referenced by quic.QuicConnection._create_packet().

◆ _idle

◆ _local_addr

quic.QuicConnection._local_addr
protected

Definition at line 39 of file quic.py.

◆ _packet_size

quic.QuicConnection._packet_size
protected

◆ _received_packets_counter

quic.QuicConnection._received_packets_counter
protected

Definition at line 48 of file quic.py.

Referenced by quic.QuicConnection._increment_received_packets_counter().

◆ _remote_addr

quic.QuicConnection._remote_addr
protected

Definition at line 40 of file quic.py.

Referenced by quic.QuicConnection._send_packet().

◆ _sent_packets_counter

quic.QuicConnection._sent_packets_counter
protected

Definition at line 47 of file quic.py.

Referenced by quic.QuicConnection._create_packet().

◆ _socket

◆ _stats_dict

◆ _streams_counter

quic.QuicConnection._streams_counter
protected

◆ _total_time

quic.QuicConnection._total_time
protected

The documentation for this class was generated from the following file: