소스 검색

expose socket options

David Rose 24 년 전
부모
커밋
8a98eab678
2개의 변경된 파일152개의 추가작업 그리고 0개의 파일을 삭제
  1. 140 0
      panda/src/net/connection.cxx
  2. 12 0
      panda/src/net/connection.h

+ 140 - 0
panda/src/net/connection.cxx

@@ -100,6 +100,146 @@ get_socket() const {
   return _socket;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_nonblock
+//       Access: Public
+//  Description: Sets whether nonblocking I/O should be in effect.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_nonblock(bool flag) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_Nonblocking;
+  data.value.non_blocking = flag;
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_linger
+//       Access: Public
+//  Description: Sets the time to linger on close if data is present.
+//               If flag is false, when you close a socket with data
+//               available the system attempts to deliver the data to
+//               the peer (the default behavior).  If flag is false
+//               but time is zero, the system discards any undelivered
+//               data when you close the socket.  If flag is false but
+//               time is nonzero, the system waits up to time seconds
+//               to deliver the data.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_linger(bool flag, double time) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_Linger;
+  data.value.linger.polarity = flag;
+  data.value.linger.linger = PRIntervalTime(time * PR_INTERVAL_MIN);
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_reuse_addr
+//       Access: Public
+//  Description: Sets whether local address reuse is allowed.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_reuse_addr(bool flag) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_Reuseaddr;
+  data.value.reuse_addr = flag;
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_keep_alive
+//       Access: Public
+//  Description: Sets whether the connection is periodically tested to
+//               see if it is still alive.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_keep_alive(bool flag) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_Keepalive;
+  data.value.keep_alive = flag;
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_recv_buffer_size
+//       Access: Public
+//  Description: Sets the size of the receive buffer, in bytes.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_recv_buffer_size(int size) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_RecvBufferSize;
+  data.value.recv_buffer_size = size;
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_send_buffer_size
+//       Access: Public
+//  Description: Sets the size of the send buffer, in bytes.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_send_buffer_size(int size) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_SendBufferSize;
+  data.value.send_buffer_size = size;
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_ip_time_to_live
+//       Access: Public
+//  Description: Sets IP time-to-live.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_ip_time_to_live(int ttl) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_IpTimeToLive;
+  data.value.ip_ttl = ttl;
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_ip_type_of_service
+//       Access: Public
+//  Description: Sets IP type-of-service and precedence.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_ip_type_of_service(int tos) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_IpTypeOfService;
+  data.value.tos = tos;
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_no_delay
+//       Access: Public
+//  Description: If flag is true, this disables the Nagle algorithm,
+//               and prevents delaying of send to coalesce packets.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_no_delay(bool flag) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_NoDelay;
+  data.value.no_delay = flag;
+  PR_SetSocketOption(_socket, &data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::set_max_segment
+//       Access: Public
+//  Description: Sets the maximum segment size.
+////////////////////////////////////////////////////////////////////
+void Connection::
+set_max_segment(int size) {
+  PRSocketOptionData data;
+  data.option = PR_SockOpt_MaxSegment;
+  data.value.max_segment = size;
+  PR_SetSocketOption(_socket, &data);
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: Connection::send_datagram
 //       Access: Private

+ 12 - 0
panda/src/net/connection.h

@@ -46,6 +46,18 @@ PUBLISHED:
 
   PRFileDesc *get_socket() const;
 
+  // Socket options.
+  void set_nonblock(bool flag);
+  void set_linger(bool flag, double time);
+  void set_reuse_addr(bool flag);
+  void set_keep_alive(bool flag);
+  void set_recv_buffer_size(int size);
+  void set_send_buffer_size(int size);
+  void set_ip_time_to_live(int ttl);
+  void set_ip_type_of_service(int tos);
+  void set_no_delay(bool flag);
+  void set_max_segment(int size);
+
 private:
   bool send_datagram(const NetDatagram &datagram);