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) {
112set_collect_tcp(
bool collect_tcp) {
113 _collect_tcp = collect_tcp;
120get_collect_tcp()
const {
130set_collect_tcp_interval(
double interval) {
131 _collect_tcp_interval = interval;
140get_collect_tcp_interval()
const {
141 return _collect_tcp_interval;
160 if (elapsed < 0.0 || elapsed >= _collect_tcp_interval) {
206set_linger(
bool flag,
double time) {
208 DCAST_INTO_V(tcp, _socket);
221set_reuse_addr(
bool flag) {
230set_keep_alive(
bool flag) {
238set_recv_buffer_size(
int size) {
246set_send_buffer_size(
int size) {
248 DCAST_INTO_V(tcp, _socket);
257set_ip_time_to_live(
int ttl) {
265set_ip_type_of_service(
int tos) {
274set_no_delay(
bool flag) {
276 DCAST_INTO_V(tcp, _socket);
285set_max_segment(
int size) {
296send_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);
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);
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());
465 int data_sent = tcp->SendData(sending_data);
466 bool okflag = (data_sent == (int)sending_data.size());
470 return check_send_error(okflag);
477check_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);
This is a convenience class to specialize ConfigVariable as a boolean type.
The primary interface to the low-level networking layer in this package.
bool flush()
Sends the most recently queued TCP datagram(s) now.
size_t get_length() const
Returns the number of bytes in the datagram.
std::string get_message() const
Returns the datagram's data as a string.
Similar to MutexHolder, but for a light reentrant mutex.
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound.
const Socket_Address & get_addr() const
Returns the Socket_Address for this address.
A specific kind of Datagram, especially for sending across or receiving from a network.
const NetAddress & get_address() const
Retrieves the host from which the datagram was read, or to which it is scheduled to be sent.
A simple place to store and manipulate tcp and port address for communication layer.
Base functionality for a INET domain Socket This call should be the starting point for all other unix...
bool SetReuseAddress(bool flag=true)
Informs a socket to reuse IP address as needed.
static int GetLastError()
Gets the last errcode from a socket operation.
void Close()
Closes a socket if it is open (allocated).
int SetRecvBufferSize(int size)
Ok it sets the recv buffer size for both tcp and UDP.
bool Active()
Ask if the socket is open (allocated)
Socket_Address GetPeerName(void) const
Wrapper on berkly getpeername...
int SetNonBlocking()
this function will throw a socket into non-blocking mode
Base functionality for a TCP connected socket This class is pretty useless by itself but it does hide...
int SetSendBufferSize(int insize)
Just like it sounds.
int DontLinger()
Turn off the linger flag.
int SetLinger(int interval_seconds=0)
will control the behavior of SO_LINGER for a TCP socket
int SetNoDelay(bool flag=true)
Disable Nagle algorithm.
Base functionality for a combination UDP Reader and Writer.
bool SendTo(const char *data, int len, const Socket_Address &address)
Send data to specified address.
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
static void force_yield()
Suspends the current thread for the rest of the current epoch.
static TrueClock * get_global_ptr()
Returns a pointer to the one TrueClock object in the world.
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.
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.