datagramOutputFile.cxx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Filename: datagramOutputFile.h
  2. // Created by: drose (30Oct00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "datagramOutputFile.h"
  6. ////////////////////////////////////////////////////////////////////
  7. // Function: DatagramOutputFile::write_header
  8. // Access: Public
  9. // Description: Writes a sequence of bytes to the beginning of the
  10. // datagram file. This may be called any number of
  11. // times after the file has been opened and before the
  12. // first datagram is written. It may not be called once
  13. // the first datagram is written.
  14. ////////////////////////////////////////////////////////////////////
  15. bool DatagramOutputFile::
  16. write_header(const string &header) {
  17. nassertr(!_wrote_first_datagram, false);
  18. _out.write(header.data(), header.size());
  19. return !_out.fail();
  20. }
  21. ////////////////////////////////////////////////////////////////////
  22. // Function: DatagramOutputFile::put_datagram
  23. // Access: Public, Virtual
  24. // Description: Writes the given datagram to the file. Returns true
  25. // on success, false if there is an error.
  26. ////////////////////////////////////////////////////////////////////
  27. bool DatagramOutputFile::
  28. put_datagram(const Datagram &data) {
  29. _wrote_first_datagram = true;
  30. // First, write the size of the upcoming datagram. We do this with
  31. // the help of a second datagram.
  32. Datagram size;
  33. size.add_uint32(data.get_length());
  34. _out.write((const char *)size.get_data(), size.get_length());
  35. // Now, write the datagram itself.
  36. _out.write((const char *)data.get_data(), data.get_length());
  37. return !_out.fail();
  38. }
  39. ////////////////////////////////////////////////////////////////////
  40. // Function: DatagramOutputFile::is_error
  41. // Access: Public, Virtual
  42. // Description: Returns true if the file has reached an error
  43. // condition.
  44. ////////////////////////////////////////////////////////////////////
  45. bool DatagramOutputFile::
  46. is_error() {
  47. if (_out.fail()) {
  48. _error = true;
  49. }
  50. return _error;
  51. }