Go to the documentation of this file.
24 #include "socket_ip.h"
25 #include "socket_tcp.h"
40 _collect_tcp = collect_tcp;
41 _collect_tcp_interval = collect_tcp_interval;
42 _queued_data_start = 0.0;
45 #if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
50 <<
"Unable to set non-blocking status on socket\n";
61 <<
"Deleting connection " << (
void *)
this <<
"\n";
63 if (_socket !=
nullptr) {
113 _collect_tcp = collect_tcp;
131 _collect_tcp_interval = interval;
141 return _collect_tcp_interval;
160 if (elapsed < 0.0 || elapsed >= _collect_tcp_interval) {
208 DCAST_INTO_V(tcp, _socket);
248 DCAST_INTO_V(tcp, _socket);
276 DCAST_INTO_V(tcp, _socket);
296 send_datagram(
const NetDatagram &datagram,
int tcp_header_size) {
297 nassertr(_socket !=
nullptr,
false);
302 DCAST_INTO_R(udp, _socket,
false);
307 data += header.get_header();
310 if (net_cat.is_debug()) {
311 header.verify_datagram(datagram);
314 int bytes_to_send = data.length();
317 bool okflag = udp->
SendTo(data, addr);
318 #if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
321 okflag = udp->
SendTo(data, addr);
323 #endif // SIMPLE_THREADS
325 if (net_cat.is_spam()) {
327 <<
"Sent UDP datagram with "
328 << bytes_to_send <<
" bytes to " << (
void *)
this
329 <<
", ok = " << okflag <<
"\n";
332 return check_send_error(okflag);
336 if (tcp_header_size == 2 && datagram.
get_length() >= 0x10000) {
338 <<
"Attempt to send TCP datagram of " << datagram.
get_length()
339 <<
" bytes--too long!\n";
340 nassert_raise(
"Datagram too long");
347 _queued_data += header.get_header();
351 if (net_cat.is_debug()) {
352 header.verify_datagram(datagram, tcp_header_size);
370 nassertr(_socket !=
nullptr,
false);
375 DCAST_INTO_R(udp, _socket,
false);
381 bool okflag = udp->
SendTo(data, addr);
382 #if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
385 okflag = udp->
SendTo(data, addr);
387 #endif // SIMPLE_THREADS
389 if (net_cat.is_spam()) {
391 <<
"Sent UDP datagram with "
392 << data.size() <<
" bytes to " << (
void *)
this
393 <<
", ok = " << okflag <<
"\n";
396 return check_send_error(okflag);
418 if (_queued_data.empty()) {
424 if (net_cat.is_spam()) {
426 <<
"Sending " << _queued_count <<
" TCP datagram(s) with "
427 << _queued_data.length() <<
" total bytes to " << (
void *)
this <<
"\n";
431 DCAST_INTO_R(tcp, _socket,
false);
433 std::string sending_data;
434 _queued_data.swap(sending_data);
439 #if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
440 int max_send = net_max_write_per_epoch;
441 int data_sent = tcp->SendData(sending_data.data(), std::min((
size_t)max_send, sending_data.size()));
442 bool okflag = (data_sent == (int)sending_data.size());
446 total_sent += data_sent;
449 while (!okflag && tcp->
Active() &&
450 (data_sent > 0 || tcp->
GetLastError() == LOCAL_BLOCKING_ERROR)) {
451 if (data_sent == 0) {
456 data_sent = tcp->SendData(sending_data.data() + total_sent, std::min((
size_t)max_send, sending_data.size() - total_sent));
458 total_sent += data_sent;
460 okflag = (total_sent == (int)sending_data.size());
464 #else // SIMPLE_THREADS
465 int data_sent = tcp->SendData(sending_data);
466 bool okflag = (data_sent == (int)sending_data.size());
468 #endif // SIMPLE_THREADS
470 return check_send_error(okflag);
477 check_send_error(
bool okflag) {
480 if (abort_send_error) {
481 nassert_raise(
"send error");
487 if (_manager !=
nullptr) {
488 _manager->flush_read_connection(
this);
489 _manager->connection_reset(
this, okflag);
Base functionality for a INET domain Socket This call should be the starting point for all other unix...
Connection(ConnectionManager *manager, Socket_IP *socket)
Creates a connection.
This is a convenience class to specialize ConfigVariable as a boolean type.
~Connection()
Closes a connection.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_max_segment(int size)
Sets the maximum segment size.
int DontLinger()
Turn off the linger flag.
void set_ip_time_to_live(int ttl)
Sets IP time-to-live.
Base functionality for a TCP connected socket This class is pretty useless by itself but it does hide...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_collect_tcp_interval(double interval)
Specifies the interval in time, in seconds, for which to hold TCP packets before sending all of the r...
int SetSendBufferSize(int insize)
Just like it sounds.
Socket_IP * get_socket() const
Returns the internal Socket_IP that defines the connection.
int SetNoDelay(bool flag=true)
Disable Nagle algorithm.
Similar to MutexHolder, but for a light reentrant mutex.
Socket_Address GetPeerName(void) const
Wrapper on berkly getpeername...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_send_buffer_size(int size)
Sets the size of the send buffer, in bytes.
bool get_collect_tcp() const
Returns the current setting of "collect-tcp" mode.
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
int SetNonBlocking()
this function will throw a socket into non-blocking mode
double get_collect_tcp_interval() const
Returns the interval in time, in seconds, for which to hold TCP packets before sending all of the rec...
const NetAddress & get_address() const
Retrieves the host from which the datagram was read, or to which it is scheduled to be sent.
const Socket_Address & get_addr() const
Returns the Socket_Address for this address.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
NetAddress get_address() const
Returns the address bound to this connection, if it is a TCP connection.
bool Active()
Ask if the socket is open (allocated)
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound.
Base functionality for a combination UDP Reader and Writer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void Close()
Closes a socket if it is open (allocated).
size_t get_length() const
Returns the number of bytes in the datagram.
bool SetReuseAddress(bool flag=true)
Informs a socket to reuse IP address as needed.
bool SendTo(const char *data, int len, const Socket_Address &address)
Send data to specified address.
bool flush()
Sends the most recently queued TCP datagram(s) now.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The primary interface to the low-level networking layer in this package.
void set_keep_alive(bool flag)
Sets whether the connection is periodically tested to see if it is still alive.
void set_recv_buffer_size(int size)
Sets the size of the receive buffer, in bytes.
static TrueClock * get_global_ptr()
Returns a pointer to the one TrueClock object in the world.
std::string get_message() const
Returns the datagram's data as a string.
A specific kind of Datagram, especially for sending across or receiving from a network.
void set_ip_type_of_service(int tos)
Sets IP type-of-service and precedence.
A simple place to store and manipulate tcp and port address for communication layer.
static int GetLastError()
Gets the last errcode from a socket operation.
int SetRecvBufferSize(int size)
Ok it sets the recv buffer size for both tcp and UDP.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_linger(bool flag, double time)
Sets whether nonblocking I/O should be in effect.
int SetLinger(int interval_seconds=0)
will control the behavior of SO_LINGER for a TCP socket
bool consider_flush()
Sends the most recently queued TCP datagram(s) if enough time has elapsed.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool is_exact_type(TypeHandle handle) const
Returns true if the current object is the indicated type exactly.
void set_reuse_addr(bool flag)
Sets whether local address reuse is allowed.
void set_collect_tcp(bool collect_tcp)
Enables or disables "collect-tcp" mode.
ConnectionManager * get_manager() const
Returns a pointer to the ConnectionManager object that serves this connection.
static void force_yield()
Suspends the current thread for the rest of the current epoch.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_no_delay(bool flag)
If flag is true, this disables the Nagle algorithm, and prevents delaying of send to coalesce packets...