00001 // Filename: datagramOutputFile.cxx 00002 // Created by: drose (30Oct00) 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 "datagramOutputFile.h" 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: DatagramOutputFile::write_header 00023 // Access: Public 00024 // Description: Writes a sequence of bytes to the beginning of the 00025 // datagram file. This may be called any number of 00026 // times after the file has been opened and before the 00027 // first datagram is written. It may not be called once 00028 // the first datagram is written. 00029 //////////////////////////////////////////////////////////////////// 00030 bool DatagramOutputFile:: 00031 write_header(const string &header) { 00032 nassertr(!_wrote_first_datagram, false); 00033 00034 _out.write(header.data(), header.size()); 00035 return !_out.fail(); 00036 } 00037 00038 //////////////////////////////////////////////////////////////////// 00039 // Function: DatagramOutputFile::put_datagram 00040 // Access: Public, Virtual 00041 // Description: Writes the given datagram to the file. Returns true 00042 // on success, false if there is an error. 00043 //////////////////////////////////////////////////////////////////// 00044 bool DatagramOutputFile:: 00045 put_datagram(const Datagram &data) { 00046 _wrote_first_datagram = true; 00047 00048 // First, write the size of the upcoming datagram. We do this with 00049 // the help of a second datagram. 00050 Datagram size; 00051 size.add_uint32(data.get_length()); 00052 _out.write((const char *)size.get_data(), size.get_length()); 00053 00054 // Now, write the datagram itself. 00055 _out.write((const char *)data.get_data(), data.get_length()); 00056 00057 return !_out.fail(); 00058 } 00059 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: DatagramOutputFile::is_error 00062 // Access: Public, Virtual 00063 // Description: Returns true if the file has reached an error 00064 // condition. 00065 //////////////////////////////////////////////////////////////////// 00066 bool DatagramOutputFile:: 00067 is_error() { 00068 if (_out.fail()) { 00069 _error = true; 00070 } 00071 return _error; 00072 }