Panda3D
socketStreamRecorder.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file socketStreamRecorder.cxx
10  * @author drose
11  * @date 2004-01-28
12  */
13 
14 #include "socketStreamRecorder.h"
15 #include "recorderController.h"
16 #include "bamReader.h"
17 #include "bamWriter.h"
18 
19 #ifdef HAVE_OPENSSL
20 
21 TypeHandle SocketStreamRecorder::_type_handle;
22 
23 /**
24  * Receives a datagram over the socket by expecting a little-endian 16-bit
25  * byte count as a prefix. If the socket stream is non-blocking, may return
26  * false if the data is not available; otherwise, returns false only if the
27  * socket closes.
28  */
29 bool SocketStreamRecorder::
30 receive_datagram(Datagram &dg) {
31  if (is_playing()) {
32  // If we're playing back data, the datagrams come only from the queue, not
33  // from the live connection.
34  if (!_data.empty()) {
35  dg = _data.front();
36  _data.pop_front();
37  return true;
38  }
39 
40  return false;
41 
42  } else {
43  // If we're not in playback mode, forward the request to the connection.
44  bool got_data = false;
45  if (_stream != nullptr) {
46  got_data = _stream->receive_datagram(dg);
47  }
48 
49  if (got_data && is_recording()) {
50  // If we're in recording mode, save the data.
51  _data.push_back(dg);
52  }
53 
54  return got_data;
55  }
56 }
57 
58 /**
59  * Records the most recent data collected into the indicated datagram, and
60  * returns true if there is any interesting data worth recording, or false if
61  * the datagram is meaningless.
62  */
63 void SocketStreamRecorder::
64 record_frame(BamWriter *manager, Datagram &dg) {
65  nassertv(is_recording());
66  dg.add_bool(_closed);
67  dg.add_uint16(_data.size());
68  for (Data::iterator di = _data.begin(); di != _data.end(); ++di) {
69  dg.add_string((*di).get_message());
70  }
71  _data.clear();
72 }
73 
74 
75 /**
76  * Reloads the most recent data collected from the indicated datagram.
77  */
78 void SocketStreamRecorder::
79 play_frame(DatagramIterator &scan, BamReader *manager) {
80  nassertv(is_playing());
81  _closed = scan.get_bool();
82 
83  int num_packets = scan.get_uint16();
84  for (int i = 0; i < num_packets; i++) {
85  _data.push_back(Datagram(scan.get_blob()));
86  }
87 }
88 
89 /**
90  * Tells the BamReader how to create objects of type Lens.
91  */
92 void SocketStreamRecorder::
93 register_with_read_factory() {
94  RecorderController::get_factory()->register_factory(get_class_type(), make_recorder);
95 }
96 
97 /**
98  * Writes the contents of this object to the datagram for encoding in the
99  * session file. This is very similar to write_datagram() for TypedWritable
100  * objects, but it is used specifically to write the Recorder object when
101  * generating the session file. In many cases, it will be the same as
102  * write_datagram().
103  */
104 void SocketStreamRecorder::
105 write_recorder(BamWriter *manager, Datagram &dg) {
106  RecorderBase::write_recorder(manager, dg);
107 }
108 
109 /**
110  * This is similar to make_from_bam(), but it is designed for loading the
111  * RecorderBase object from the session log created by a RecorderController.
112  */
113 RecorderBase *SocketStreamRecorder::
114 make_recorder(const FactoryParams &params) {
115  SocketStreamRecorder *node = new SocketStreamRecorder;
116  DatagramIterator scan;
117  BamReader *manager;
118 
119  parse_params(params, scan, manager);
120  node->fillin_recorder(scan, manager);
121 
122  return node;
123 }
124 
125 /**
126  * This internal function is called by make_from_bam to read in all of the
127  * relevant data from the BamFile for the new SocketStreamRecorder.
128  */
129 void SocketStreamRecorder::
130 fillin_recorder(DatagramIterator &scan, BamReader *manager) {
131  RecorderBase::fillin_recorder(scan, manager);
132 }
133 
134 #endif // HAVE_OPENSSL
bool get_bool()
Extracts a boolean value.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
static RecorderFactory * get_factory()
Returns the global RecorderFactory for generating TypedWritable objects.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:34
void add_string(const std::string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:219
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
This is the base class to a number of objects that record particular kinds of user input (like a Mous...
Definition: recorderBase.h:46
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write_recorder(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for encoding in the session file.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
vector_uchar get_blob()
Extracts a variable-length binary blob.
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.