18#include "socket_tcp.h"
26ConnectionWriter::WriterThread::
29 Thread(make_thread_name(thread_name, thread_index),
30 make_thread_name(thread_name, thread_index)),
32 _thread_index(thread_index)
39void ConnectionWriter::WriterThread::
41 _writer->thread_run(_thread_index);
53 const std::string &thread_name) :
58 if (num_threads != 0) {
59 if (net_cat.is_debug()) {
61 <<
"Threading support is not available.\n";
69 _tcp_header_size = tcp_header_size;
70 _immediate = (num_threads <= 0);
73 std::string writer_thread_name = thread_name;
74 if (thread_name.empty()) {
75 writer_thread_name =
"WriterThread";
78 for (i = 0; i < num_threads; i++) {
79 PT(WriterThread) thread =
new WriterThread(
this, writer_thread_name, i);
80 _threads.push_back(thread);
82 for (i = 0; i < num_threads; i++) {
83 _threads[i]->start(net_thread_priority,
true);
86 _manager->add_writer(
this);
94 if (_manager !=
nullptr) {
95 _manager->remove_writer(
this);
144 nassertr(!_shutdown,
false);
145 nassertr(connection !=
nullptr,
false);
153 return connection->send_raw_datagram(copy);
155 return connection->send_datagram(copy, _tcp_header_size);
158 return _queue.
insert(copy, block);
179 nassertr(!_shutdown,
false);
180 nassertr(connection !=
nullptr,
false);
183 if ((
int)datagram.
get_length() > maximum_udp_datagram) {
185 <<
"Attempt to send UDP datagram of " << datagram.
get_length()
186 <<
" bytes, more than the\n"
187 <<
"currently defined maximum of " << maximum_udp_datagram
193 copy.set_address(address);
197 return connection->send_raw_datagram(copy);
199 return connection->send_datagram(copy, _tcp_header_size);
202 return _queue.
insert(copy, block);
212 return (
int)datagram.
get_length() <= maximum_udp_datagram;
237 return _threads.size();
271 _tcp_header_size = tcp_header_size;
279 return _tcp_header_size;
297 Threads::iterator ti;
298 for (ti = _threads.begin(); ti != _threads.end(); ++ti) {
310void ConnectionWriter::
319void ConnectionWriter::
320thread_run(
int thread_index) {
321 nassertv(!_immediate);
324 while (_queue.
extract(datagram)) {
326 datagram.get_connection()->send_raw_datagram(datagram);
328 datagram.get_connection()->send_datagram(datagram, _tcp_header_size);
The primary interface to the low-level networking layer in this package.
This class handles threaded delivery of datagrams to various TCP or UDP sockets.
void set_max_queue_size(int max_size)
Limits the number of packets that may be pending on the outbound queue.
int get_num_threads() const
Returns the number of threads the ConnectionWriter has been created with.
bool send(const Datagram &datagram, const PT(Connection) &connection, bool block=false)
Enqueues a datagram for transmittal on the indicated socket.
bool is_valid_for_udp(const Datagram &datagram) const
Returns true if the datagram is small enough to be sent over a UDP packet, false otherwise.
void set_raw_mode(bool mode)
Sets the ConnectionWriter into raw mode (or turns off raw mode).
void shutdown()
Stops all the threads and cleans them up.
int get_max_queue_size() const
Returns the maximum size the queue is allowed to grow to.
bool is_immediate() const
Returns true if the writer is an immediate writer, i.e.
ConnectionManager * get_manager() const
Returns a pointer to the ConnectionManager object that serves this ConnectionWriter.
bool get_raw_mode() const
Returns the current setting of the raw mode flag.
ConnectionWriter(ConnectionManager *manager, int num_threads, const std::string &thread_name=std::string())
Creates a new ConnectionWriter with the indicated number of threads to handle output.
int get_current_queue_size() const
Returns the current number of things in the queue.
int get_tcp_header_size() const
Returns the current setting of TCP header size.
void set_tcp_header_size(int tcp_header_size)
Sets the header size of TCP packets.
Represents a single TCP or UDP socket for input or output.
Socket_IP * get_socket() const
Returns the internal Socket_IP that defines the connection.
int get_max_queue_size() const
Returns the maximum size the queue is allowed to grow to.
void shutdown()
Marks the queue as shutting down, which will eventually cause all threads blocking on extract() to re...
void set_max_queue_size(int max_size)
Sets the maximum size the queue is allowed to grow to.
bool extract(NetDatagram &result)
Extracts a datagram from the head of the queue, if one is available.
bool insert(const NetDatagram &data, bool block=false)
Inserts the indicated datagram onto the end of the queue, and returns.
int get_current_queue_size() const
Returns the current number of things in the queue.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
size_t get_length() const
Returns the number of bytes in the datagram.
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound.
A specific kind of Datagram, especially for sending across or receiving from a network.
void set_connection(const PT(Connection) &connection)
Specifies the socket to which the datagram should be written.
A thread; that is, a lightweight process.
is_threading_supported
Returns true if threading support has been compiled in and enabled, or false if no threading is avail...
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
bool is_exact_type(TypeHandle handle) const
Returns true if the current object is the indicated type exactly.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.