Panda3D
Loading...
Searching...
No Matches
socketStream.h
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 socketStream.h
10 * @author drose
11 * @date 2002-10-15
12 */
13
14#ifndef SOCKETSTREAM_H
15#define SOCKETSTREAM_H
16
17#include "pandabase.h"
18#include "trueClock.h"
19#include "config_express.h" // for collect_tcp
20#include "datagram.h"
21#include "pdeque.h"
22#include "typedReferenceCount.h"
23#include "pointerTo.h"
24#include "vector_uchar.h"
25
26// At the present, this module is not compiled if OpenSSL is not available,
27// since the only current use for it is to implement OpenSSL-defined
28// constructs (like ISocketStream).
29
30#ifdef HAVE_OPENSSL
31
32class HTTPChannel;
33
34/**
35 * An internal class for reading from a socket stream. This serves as a base
36 * class for both ISocketStream and SocketStream; its purpose is to minimize
37 * redundant code between them. Do not use it directly.
38 */
39class EXPCL_PANDA_DOWNLOADER SSReader {
40public:
41 SSReader(std::istream *stream);
42 virtual ~SSReader();
43
44PUBLISHED:
45 INLINE bool receive_datagram(Datagram &dg);
46
47 virtual bool is_closed() = 0;
48 virtual void close() = 0;
49
50 INLINE void set_tcp_header_size(int tcp_header_size);
51 INLINE int get_tcp_header_size() const;
52
53private:
54 bool do_receive_datagram(Datagram &dg);
55
56 std::istream *_istream;
57 size_t _data_expected;
58 vector_uchar _data_so_far;
59 int _tcp_header_size;
60
61#ifdef SIMULATE_NETWORK_DELAY
62PUBLISHED:
63 void start_delay(double min_delay, double max_delay);
64 void stop_delay();
65
66private:
67 void delay_datagram(const Datagram &datagram);
68 bool get_delayed(Datagram &datagram);
69
70 class DelayedDatagram {
71 public:
72 double _reveal_time;
73 Datagram _datagram;
74 };
75
76 typedef pdeque<DelayedDatagram> Delayed;
77 Delayed _delayed;
78 bool _delay_active;
79 double _min_delay, _delay_variance;
80
81#endif // SIMULATE_NETWORK_DELAY
82};
83
84/**
85 * An internal class for writing to a socket stream. This serves as a base
86 * class for both OSocketStream and SocketStream; its purpose is to minimize
87 * redundant code between them. Do not use it directly.
88 */
89class EXPCL_PANDA_DOWNLOADER SSWriter {
90public:
91 SSWriter(std::ostream *stream);
92 virtual ~SSWriter();
93
94PUBLISHED:
95 bool send_datagram(const Datagram &dg);
96
97 virtual bool is_closed() = 0;
98 virtual void close() = 0;
99
100 INLINE void set_collect_tcp(bool collect_tcp);
101 INLINE bool get_collect_tcp() const;
102 INLINE void set_collect_tcp_interval(double interval);
103 INLINE double get_collect_tcp_interval() const;
104
105 INLINE void set_tcp_header_size(int tcp_header_size);
106 INLINE int get_tcp_header_size() const;
107
108 INLINE bool consider_flush();
109 INLINE bool flush();
110
111private:
112 std::ostream *_ostream;
113 bool _collect_tcp;
114 double _collect_tcp_interval;
115 double _queued_data_start;
116 int _tcp_header_size;
117};
118
119/**
120 * This is a base class for istreams implemented in Panda that read from a
121 * (possibly non-blocking) socket. It adds is_closed(), which can be called
122 * after an eof condition to check whether the socket has been closed, or
123 * whether more data may be available later.
124 */
125class EXPCL_PANDA_DOWNLOADER ISocketStream : public std::istream, public SSReader {
126public:
127 INLINE ISocketStream(std::streambuf *buf);
128 virtual ~ISocketStream();
129
130#if _MSC_VER >= 1800
131 INLINE ISocketStream(const ISocketStream &copy) = delete;
132#endif
133
134PUBLISHED:
135 enum ReadState {
136 RS_initial,
137 RS_reading,
138 RS_complete,
139 RS_error,
140 };
141
142 virtual bool is_closed() = 0;
143 virtual void close() = 0;
144 virtual ReadState get_read_state() = 0;
145
146protected:
147 HTTPChannel *_channel;
148
149private:
150 friend class HTTPChannel;
151};
152
153/**
154 * A base class for ostreams that write to a (possibly non-blocking) socket.
155 * It adds is_closed(), which can be called after any write operation fails to
156 * check whether the socket has been closed, or whether more data may be sent
157 * later.
158 */
159class EXPCL_PANDA_DOWNLOADER OSocketStream : public std::ostream, public SSWriter {
160public:
161 INLINE OSocketStream(std::streambuf *buf);
162
163#if _MSC_VER >= 1800
164 INLINE OSocketStream(const OSocketStream &copy) = delete;
165#endif
166
167PUBLISHED:
168 virtual bool is_closed() = 0;
169 virtual void close() = 0;
170
171 INLINE bool flush();
172};
173
174/**
175 * A base class for iostreams that read and write to a (possibly non-blocking)
176 * socket.
177 */
178class EXPCL_PANDA_DOWNLOADER SocketStream : public std::iostream, public SSReader, public SSWriter {
179public:
180 INLINE SocketStream(std::streambuf *buf);
181
182#if _MSC_VER >= 1800
183 INLINE SocketStream(const SocketStream &copy) = delete;
184#endif
185
186PUBLISHED:
187 virtual bool is_closed() = 0;
188 virtual void close() = 0;
189
190 INLINE void set_tcp_header_size(int tcp_header_size);
191 INLINE int get_tcp_header_size() const;
192
193 INLINE bool flush();
194};
195
196
197#include "socketStream.I"
198
199#endif // HAVE_OPENSSL
200
201
202#endif
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
This is our own Panda specialization on the default STL deque.
Definition pdeque.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.