David Rose 24 سال پیش
والد
کامیت
9f8953c44b
4فایلهای تغییر یافته به همراه97 افزوده شده و 0 حذف شده
  1. 63 0
      panda/src/net/connection.cxx
  2. 1 0
      panda/src/net/connection.h
  3. 31 0
      panda/src/net/connectionWriter.cxx
  4. 2 0
      panda/src/net/connectionWriter.h

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

@@ -314,3 +314,66 @@ send_datagram(const NetDatagram &datagram) {
 
   return true;
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: Connection::send_raw_datagram
+//       Access: Private
+//  Description: This method is intended only to be called by
+//               ConnectionWriter.  It atomically writes the given
+//               datagram to the socket, without the Datagram header.
+////////////////////////////////////////////////////////////////////
+bool Connection::
+send_raw_datagram(const NetDatagram &datagram) {
+  nassertr(_socket != (PRFileDesc *)NULL, false);
+
+  PR_Lock(_write_mutex);
+
+  PRInt32 bytes_sent;
+  PRInt32 result;
+  if (PR_GetDescType(_socket) == PR_DESC_SOCKET_UDP) {
+    string data = datagram.get_message();
+    bytes_sent = data.length();
+    result = PR_SendTo(_socket,
+                       data.data(), bytes_sent,
+                       0,
+                       datagram.get_address().get_addr(),
+                       PR_INTERVAL_NO_TIMEOUT);
+  } else {
+    string data = datagram.get_message();
+    bytes_sent = data.length();
+    result = PR_Send(_socket,
+                     data.data(), bytes_sent,
+                     0,
+                     PR_INTERVAL_NO_TIMEOUT);
+  }
+
+  PRErrorCode errcode = PR_GetError();
+
+  PR_Unlock(_write_mutex);
+
+  if (result < 0) {
+    if (errcode == PR_CONNECT_RESET_ERROR
+#ifdef PR_SOCKET_SHUTDOWN_ERROR
+        || errcode == PR_SOCKET_SHUTDOWN_ERROR
+        || errcode == PR_CONNECT_ABORTED_ERROR
+#endif
+        ) {
+      // The connection has been reset; tell our manager about it
+      // and ignore it.
+      if (_manager != (ConnectionManager *)NULL) {
+        _manager->connection_reset(this);
+      }
+
+    } else if (errcode != PR_PENDING_INTERRUPT_ERROR) {
+      pprerror("PR_SendTo");
+    }
+
+    return false;
+
+  } else if (result != bytes_sent) {
+    net_cat.error() << "Not enough bytes sent to socket.\n";
+    return false;
+  }
+
+  return true;
+}

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

@@ -60,6 +60,7 @@ PUBLISHED:
 
 private:
   bool send_datagram(const NetDatagram &datagram);
+  bool send_raw_datagram(const NetDatagram &datagram);
 
   ConnectionManager *_manager;
   PRFileDesc *_socket;

+ 31 - 0
panda/src/net/connectionWriter.cxx

@@ -172,6 +172,37 @@ send(const Datagram &datagram, const PT(Connection) &connection,
   }
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: ConnectionWriter::send_raw
+//       Access: Public
+//  Description: Enqueues a datagram for transmission on the indicated
+//               socket, *without* sending the Datagram header.  This
+//               will not be intelligible to a ConnectionReader on the
+//               other end, which will expect to receive a Datagram
+//               header.  However, it may be necessary to send raw
+//               data to some other kind of client (like a proxy
+//               server).
+//
+//               The data is always sent immediately, regardless of
+//               whether this is a queued connection or not.
+////////////////////////////////////////////////////////////////////
+bool ConnectionWriter::
+send_raw(const Datagram &datagram, const PT(Connection) &connection) {
+  nassertr(connection != (Connection *)NULL, false);
+  nassertr(PR_GetDescType(connection->get_socket()) == PR_DESC_SOCKET_TCP, false);
+
+  if (net_cat.is_debug()) {
+    net_cat.debug()
+      << "Sending TCP raw datagram of " << datagram.get_length()
+      << " bytes\n";
+  }
+
+  NetDatagram copy(datagram);
+  copy.set_connection(connection);
+
+  return connection->send_raw_datagram(copy);
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: ConnectionWriter::is_valid_for_udp
 //       Access: Public

+ 2 - 0
panda/src/net/connectionWriter.h

@@ -54,6 +54,8 @@ PUBLISHED:
             const PT(Connection) &connection,
             const NetAddress &address);
 
+  bool send_raw(const Datagram &datagram, const PT(Connection) &connection);
+
   bool is_valid_for_udp(const Datagram &datagram) const;
 
   ConnectionManager *get_manager() const;