15 #include "connectionWriter.h" 16 #include "connectionManager.h" 17 #include "datagramTCPHeader.h" 18 #include "config_net.h" 19 #include "socket_tcp.h" 20 #include "socket_udp.h" 22 #include "config_downloader.h" 29 ConnectionWriter::WriterThread::
32 Thread(make_thread_name(thread_name, thread_index),
33 make_thread_name(thread_name, thread_index)),
35 _thread_index(thread_index)
44 void ConnectionWriter::WriterThread::
46 _writer->thread_run(_thread_index);
61 const string &thread_name) :
66 if (num_threads != 0) {
67 if (net_cat.is_debug()) {
69 <<
"Threading support is not available.\n";
77 _tcp_header_size = tcp_header_size;
78 _immediate = (num_threads <= 0);
81 string writer_thread_name = thread_name;
82 if (thread_name.empty()) {
83 writer_thread_name =
"WriterThread";
86 for (i = 0; i < num_threads; i++) {
87 PT(WriterThread) thread =
new WriterThread(
this, writer_thread_name, i);
88 _threads.push_back(thread);
90 for (i = 0; i < num_threads; i++) {
91 _threads[i]->start(net_thread_priority,
true);
94 _manager->add_writer(
this);
103 ~ConnectionWriter() {
105 _manager->remove_writer(
this);
167 nassertr(!_shutdown,
false);
168 nassertr(connection != (
Connection *)NULL,
false);
172 copy.set_connection(connection);
176 return connection->send_raw_datagram(copy);
178 return connection->send_datagram(copy, _tcp_header_size);
181 return _queue.
insert(copy, block);
208 nassertr(!_shutdown,
false);
209 nassertr(connection != (
Connection *)NULL,
false);
212 if ((
int)datagram.
get_length() > maximum_udp_datagram) {
214 <<
"Attempt to send UDP datagram of " << datagram.
get_length()
215 <<
" bytes, more than the\n" 216 <<
"currently defined maximum of " << maximum_udp_datagram
222 copy.set_address(address);
226 return connection->send_raw_datagram(copy);
228 return connection->send_datagram(copy, _tcp_header_size);
231 return _queue.
insert(copy, block);
243 return (
int)datagram.
get_length() <= maximum_udp_datagram;
276 return _threads.size();
320 _tcp_header_size = tcp_header_size;
331 return _tcp_header_size;
353 Threads::iterator ti;
354 for (ti = _threads.begin(); ti != _threads.end(); ++ti) {
370 void ConnectionWriter::
382 void ConnectionWriter::
383 thread_run(
int thread_index) {
384 nassertv(!_immediate);
387 while (_queue.
extract(datagram)) {
389 datagram.get_connection()->send_raw_datagram(datagram);
391 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.
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...
ConnectionWriter(ConnectionManager *manager, int num_threads, const string &thread_name=string())
Creates a new ConnectionWriter with the indicated number of threads to handle output.
bool get_raw_mode() const
Returns the current setting of the raw mode flag.
static bool is_threading_supported()
Returns true if threading support has been compiled in and enabled, or false if no threading is avail...
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.
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...
A thread; that is, a lightweight process.
void set_connection(const PT(Connection) &connection)
Specifies the socket to which the datagram should be written.
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.
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.