00001 // Filename: datagramTCPHeader.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 "datagramTCPHeader.h" 00020 #include "netDatagram.h" 00021 #include "datagramIterator.h" 00022 #include "config_net.h" 00023 00024 #include <notify.h> 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: DatagramTCPHeader::Constructor 00028 // Access: Public 00029 // Description: This constructor creates a header based on an 00030 // already-constructed NetDatagram. 00031 //////////////////////////////////////////////////////////////////// 00032 DatagramTCPHeader:: 00033 DatagramTCPHeader(const NetDatagram &datagram) { 00034 const string &str = datagram.get_message(); 00035 PRUint16 size = str.length(); 00036 nassertv(size == str.length()); 00037 00038 // Now pack the header. 00039 _header.add_uint16(size); 00040 nassertv((int)_header.get_length() == datagram_tcp_header_size); 00041 } 00042 00043 //////////////////////////////////////////////////////////////////// 00044 // Function: DatagramTCPHeader::Constructor 00045 // Access: Public 00046 // Description: This constructor decodes a header from a block of 00047 // data of length datagram_tcp_header_size, presumably 00048 // just read from a socket. 00049 //////////////////////////////////////////////////////////////////// 00050 DatagramTCPHeader:: 00051 DatagramTCPHeader(const void *data) : _header(data, datagram_tcp_header_size) { 00052 } 00053 00054 //////////////////////////////////////////////////////////////////// 00055 // Function: DatagramTCPHeader::verify_datagram 00056 // Access: Public 00057 // Description: Verifies that the indicated datagram has the 00058 // appropriate length. Returns true if it matches, 00059 // false otherwise. 00060 //////////////////////////////////////////////////////////////////// 00061 bool DatagramTCPHeader:: 00062 verify_datagram(const NetDatagram &datagram) const { 00063 const string &str = datagram.get_message(); 00064 PRUint16 size = str.length(); 00065 nassertr(size == str.length(), false); 00066 00067 if (size == get_datagram_size()) { 00068 return true; 00069 } 00070 00071 if (net_cat.is_debug()) { 00072 net_cat.debug() 00073 << "Invalid datagram!\n"; 00074 if (size != get_datagram_size()) { 00075 net_cat.debug() 00076 << " size is " << size << " bytes, header reports " 00077 << get_datagram_size() << "\n"; 00078 } 00079 00080 // We write the hex dump into a ostringstream first, to guarantee 00081 // an atomic write to the output stream in case we're threaded. 00082 00083 ostringstream hex; 00084 datagram.dump_hex(hex); 00085 hex << "\n"; 00086 net_cat.debug() << hex.str(); 00087 } 00088 00089 return false; 00090 }