Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/net/datagramUDPHeader.cxx

Go to the documentation of this file.
00001 // Filename: datagramUDPHeader.cxx
00002 // Created by:  drose (08Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "datagramUDPHeader.h"
00020 #include "netDatagram.h"
00021 #include "datagramIterator.h"
00022 #include "config_net.h"
00023 
00024 #include <notify.h>
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: DatagramUDPHeader::Constructor
00028 //       Access: Public
00029 //  Description: This constructor creates a header based on an
00030 //               already-constructed NetDatagram.
00031 ////////////////////////////////////////////////////////////////////
00032 DatagramUDPHeader::
00033 DatagramUDPHeader(const NetDatagram &datagram) {
00034   const string &str = datagram.get_message();
00035   PRUint16 checksum = 0;
00036   for (size_t p = 0; p < str.size(); p++) {
00037     checksum += (PRUint16)(PRUint8)str[p];
00038   }
00039 
00040   // Now pack the header.
00041   _header.add_uint16(checksum);
00042   nassertv((int)_header.get_length() == datagram_udp_header_size);
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: DatagramUDPHeader::Constructor
00047 //       Access: Public
00048 //  Description: This constructor decodes a header from a block of
00049 //               data of length datagram_udp_header_size, presumably
00050 //               just read from a socket.
00051 ////////////////////////////////////////////////////////////////////
00052 DatagramUDPHeader::
00053 DatagramUDPHeader(const void *data) : _header(data, datagram_udp_header_size) {
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function: DatagramUDPHeader::verify_datagram
00058 //       Access: Public
00059 //  Description: Verifies that the indicated datagram has the
00060 //               appropriate length and checksum.  Returns true if it
00061 //               matches, false otherwise.
00062 ////////////////////////////////////////////////////////////////////
00063 bool DatagramUDPHeader::
00064 verify_datagram(const NetDatagram &datagram) const {
00065   const string &str = datagram.get_message();
00066 
00067   PRUint16 checksum = 0;
00068   for (size_t p = 0; p < str.size(); p++) {
00069     checksum += (PRUint16)(PRUint8)str[p];
00070   }
00071 
00072   if (checksum == get_datagram_checksum()) {
00073     return true;
00074   }
00075 
00076   if (net_cat.is_debug()) {
00077     net_cat.debug()
00078       << "Invalid datagram!\n";
00079     if (checksum != get_datagram_checksum()) {
00080       net_cat.debug()
00081         << "  checksum is " << checksum << ", header reports "
00082         << get_datagram_checksum() << "\n";
00083     }
00084 
00085     // We write the hex dump into a ostringstream first, to guarantee
00086     // an atomic write to the output stream in case we're threaded.
00087 
00088     ostringstream hex;
00089     datagram.dump_hex(hex);
00090     hex << "\n";
00091     net_cat.debug(false) << hex.str();
00092   }
00093 
00094   return false;
00095 }

Generated on Fri May 2 00:40:34 2003 for Panda by doxygen1.3