18 #include "socket_tcp.h" 26 ConnectionWriter::WriterThread::
29 Thread(make_thread_name(thread_name, thread_index),
30 make_thread_name(thread_name, thread_index)),
32 _thread_index(thread_index)
39 void ConnectionWriter::WriterThread::
41 _writer->thread_run(_thread_index);
53 const std::string &thread_name) :
56 if (!Thread::is_threading_supported()) {
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);
149 copy.set_connection(connection);
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) {
310 void ConnectionWriter::
319 void ConnectionWriter::
320 thread_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);
void set_max_queue_size(int max_size)
Sets the maximum size the queue is allowed to grow to.
void set_max_queue_size(int max_size)
Limits the number of packets that may be pending on the outbound queue.
bool is_exact_type(TypeHandle handle) const
Returns true if the current object is the indicated type exactly.
A specific kind of Datagram, especially for sending across or receiving from a network.
bool send(const Datagram &datagram, const PT(Connection) &connection, bool block=false)
Enqueues a datagram for transmittal on the indicated socket.
void shutdown()
Stops all the threads and cleans them up.
bool insert(const NetDatagram &data, bool block=false)
Inserts the indicated datagram onto the end of the queue, and returns.
int get_max_queue_size() const
Returns the maximum size the queue is allowed to grow to.
ConnectionManager * get_manager() const
Returns a pointer to the ConnectionManager object that serves this ConnectionWriter.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The primary interface to the low-level networking layer in this package.
int get_current_queue_size() const
Returns the current number of things in the queue.
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
bool get_raw_mode() const
Returns the current setting of the raw mode flag.
Socket_IP * get_socket() const
Returns the internal Socket_IP that defines the connection.
bool extract(NetDatagram &result)
Extracts a datagram from the head of the queue, if one is available.
void set_raw_mode(bool mode)
Sets the ConnectionWriter into raw mode (or turns off raw mode).
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.
This class handles threaded delivery of datagrams to various TCP or UDP sockets.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_max_queue_size() const
Returns the maximum size the queue is allowed to grow to.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void shutdown()
Marks the queue as shutting down, which will eventually cause all threads blocking on extract() to re...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A thread; that is, a lightweight process.
void set_connection(const PT(Connection) &connection)
Specifies the socket to which the datagram should be written.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_num_threads() const
Returns the number of threads the ConnectionWriter has been created with.
bool is_immediate() const
Returns true if the writer is an immediate writer, i.e.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents a single TCP or UDP socket for input or output.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
void set_tcp_header_size(int tcp_header_size)
Sets the header size of TCP packets.
int get_tcp_header_size() const
Returns the current setting of TCP header size.
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.
int get_current_queue_size() const
Returns the current number of things in the queue.