Panda3D

datagramTCPHeader.cxx

00001 // Filename: datagramTCPHeader.cxx
00002 // Created by:  drose (08Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "datagramTCPHeader.h"
00016 #include "netDatagram.h"
00017 #include "datagramIterator.h"
00018 #include "config_net.h"
00019 
00020 #include "pnotify.h"
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: DatagramTCPHeader::Constructor
00024 //       Access: Public
00025 //  Description: This constructor creates a header based on an
00026 //               already-constructed NetDatagram.
00027 ////////////////////////////////////////////////////////////////////
00028 DatagramTCPHeader::
00029 DatagramTCPHeader(const NetDatagram &datagram, int header_size) {
00030   const string &str = datagram.get_message();
00031   switch (header_size) {
00032   case 0:
00033     break;
00034 
00035   case datagram_tcp16_header_size:
00036     {
00037       PN_uint16 size = str.length();
00038       nassertv(size == str.length());
00039       _header.add_uint16(size);
00040     }
00041     break;
00042 
00043   case datagram_tcp32_header_size:
00044     {
00045       PN_uint32 size = str.length();
00046       nassertv(size == str.length());
00047       _header.add_uint32(size);
00048     }
00049     break;
00050 
00051   default:
00052     nassertv(false);
00053   }
00054 
00055   nassertv((int)_header.get_length() == header_size);
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: DatagramTCPHeader::Constructor
00060 //       Access: Public
00061 //  Description: This constructor decodes a header from a block of
00062 //               data of length datagram_tcp_header_size, presumably
00063 //               just read from a socket.
00064 ////////////////////////////////////////////////////////////////////
00065 DatagramTCPHeader::
00066 DatagramTCPHeader(const void *data, int header_size) : 
00067   _header(data, header_size) 
00068 {
00069 }
00070 
00071 ////////////////////////////////////////////////////////////////////
00072 //     Function: DatagramTCPHeader::get_datagram_size
00073 //       Access: Public
00074 //  Description: Returns the number of bytes in the associated
00075 //               datagram.
00076 ////////////////////////////////////////////////////////////////////
00077 int DatagramTCPHeader::
00078 get_datagram_size(int header_size) const {
00079   DatagramIterator di(_header);
00080   switch (header_size) {
00081   case 0:
00082     return 0;
00083 
00084   case datagram_tcp16_header_size:
00085     return di.get_uint16();
00086 
00087   case datagram_tcp32_header_size:
00088     return di.get_uint32();
00089   }
00090 
00091   return -1;
00092 }
00093 
00094 ////////////////////////////////////////////////////////////////////
00095 //     Function: DatagramTCPHeader::verify_datagram
00096 //       Access: Public
00097 //  Description: Verifies that the indicated datagram has the
00098 //               appropriate length.  Returns true if it matches,
00099 //               false otherwise.
00100 ////////////////////////////////////////////////////////////////////
00101 bool DatagramTCPHeader::
00102 verify_datagram(const NetDatagram &datagram, int header_size) const {
00103   if (header_size == 0) {
00104     // No way to validate without a header, so everything is valid.
00105     return true;
00106   }
00107 
00108   const string &str = datagram.get_message();
00109   int actual_size = str.length();
00110   int expected_size = get_datagram_size(header_size);
00111   if (actual_size == expected_size) {
00112     return true;
00113   }
00114 
00115   if (net_cat.is_debug()) {
00116     net_cat.debug()
00117       << "Invalid datagram!  Size is " << actual_size
00118       << " bytes, header reports " << expected_size << "\n";
00119 
00120     // We write the hex dump into a ostringstream first, to guarantee
00121     // an atomic write to the output stream in case we're threaded.
00122 
00123     ostringstream hex;
00124     datagram.dump_hex(hex);
00125     hex << "\n";
00126     net_cat.debug() << hex.str();
00127   }
00128 
00129   return false;
00130 }
 All Classes Functions Variables Enumerations