00001 // Filename: socketStream.I 00002 // Created by: drose (15Oct02) 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 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: ISocketStream::Constructor 00022 // Access: Public 00023 // Description: 00024 //////////////////////////////////////////////////////////////////// 00025 INLINE ISocketStream:: 00026 ISocketStream(streambuf *buf) : istream(buf) { 00027 _data_expected = 0; 00028 } 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function: OSocketStream::Constructor 00032 // Access: Public 00033 // Description: 00034 //////////////////////////////////////////////////////////////////// 00035 INLINE OSocketStream:: 00036 OSocketStream(streambuf *buf) : ostream(buf) { 00037 _collect_tcp = collect_tcp; 00038 _collect_tcp_interval = collect_tcp_interval; 00039 _queued_data_start = 0.0; 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: OSocketStream::set_collect_tcp 00044 // Access: Published 00045 // Description: Enables or disables "collect-tcp" mode. In this 00046 // mode, individual TCP packets are not sent 00047 // immediately, but rather they are collected together 00048 // and accumulated to be sent periodically as one larger 00049 // TCP packet. This cuts down on overhead from the 00050 // TCP/IP protocol, especially if many small packets 00051 // need to be sent on the same connection, but it 00052 // introduces additional latency (since packets must be 00053 // held before they can be sent). 00054 // 00055 // See set_collect_tcp_interval() to specify the 00056 // interval of time for which to hold packets before 00057 // sending them. 00058 // 00059 // If you enable this mode, you may also need to 00060 // periodically call consider_flush() to flush the queue 00061 // if no packets have been sent recently. 00062 //////////////////////////////////////////////////////////////////// 00063 INLINE void OSocketStream:: 00064 set_collect_tcp(bool collect_tcp) { 00065 _collect_tcp = collect_tcp; 00066 } 00067 00068 //////////////////////////////////////////////////////////////////// 00069 // Function: OSocketStream::get_collect_tcp 00070 // Access: Published 00071 // Description: Returns the current setting of "collect-tcp" mode. 00072 // See set_collect_tcp(). 00073 //////////////////////////////////////////////////////////////////// 00074 INLINE bool OSocketStream:: 00075 get_collect_tcp() const { 00076 return _collect_tcp; 00077 } 00078 00079 //////////////////////////////////////////////////////////////////// 00080 // Function: OSocketStream::set_collect_tcp_interval 00081 // Access: Published 00082 // Description: Specifies the interval in time, in seconds, for which 00083 // to hold TCP packets before sending all of the 00084 // recently received packets at once. This only has 00085 // meaning if "collect-tcp" mode is enabled; see 00086 // set_collect_tcp(). 00087 //////////////////////////////////////////////////////////////////// 00088 INLINE void OSocketStream:: 00089 set_collect_tcp_interval(double interval) { 00090 _collect_tcp_interval = interval; 00091 } 00092 00093 //////////////////////////////////////////////////////////////////// 00094 // Function: OSocketStream::get_collect_tcp_interval 00095 // Access: Published 00096 // Description: Returns the interval in time, in seconds, for which 00097 // to hold TCP packets before sending all of the 00098 // recently received packets at once. This only has 00099 // meaning if "collect-tcp" mode is enabled; see 00100 // set_collect_tcp(). 00101 //////////////////////////////////////////////////////////////////// 00102 INLINE double OSocketStream:: 00103 get_collect_tcp_interval() const { 00104 return _collect_tcp_interval; 00105 } 00106 00107 //////////////////////////////////////////////////////////////////// 00108 // Function: OSocketStream::consider_flush 00109 // Access: Published 00110 // Description: Sends the most recently queued data if enough time 00111 // has elapsed. This only has meaning if 00112 // set_collect_tcp() has been set to true. 00113 //////////////////////////////////////////////////////////////////// 00114 INLINE bool OSocketStream:: 00115 consider_flush() { 00116 if (!_collect_tcp || 00117 ClockObject::get_global_clock()->get_real_time() - _queued_data_start >= _collect_tcp_interval) { 00118 return flush(); 00119 } 00120 return true; 00121 } 00122 00123 //////////////////////////////////////////////////////////////////// 00124 // Function: OSocketStream::flush 00125 // Access: Published 00126 // Description: Sends the most recently queued data now. This only 00127 // has meaning if set_collect_tcp() has been set to 00128 // true. 00129 //////////////////////////////////////////////////////////////////// 00130 bool OSocketStream:: 00131 flush() { 00132 ostream::flush(); 00133 _queued_data_start = ClockObject::get_global_clock()->get_real_time(); 00134 return !is_closed(); 00135 } 00136 00137 //////////////////////////////////////////////////////////////////// 00138 // Function: SocketStream::Constructor 00139 // Access: Public 00140 // Description: 00141 //////////////////////////////////////////////////////////////////// 00142 INLINE SocketStream:: 00143 SocketStream(streambuf *buf) : iostream(buf) { 00144 _data_expected = 0; 00145 _collect_tcp = collect_tcp; 00146 _collect_tcp_interval = collect_tcp_interval; 00147 _queued_data_start = 0.0; 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: SocketStream::set_collect_tcp 00152 // Access: Published 00153 // Description: Enables or disables "collect-tcp" mode. In this 00154 // mode, individual TCP packets are not sent 00155 // immediately, but rather they are collected together 00156 // and accumulated to be sent periodically as one larger 00157 // TCP packet. This cuts down on overhead from the 00158 // TCP/IP protocol, especially if many small packets 00159 // need to be sent on the same connection, but it 00160 // introduces additional latency (since packets must be 00161 // held before they can be sent). 00162 // 00163 // See set_collect_tcp_interval() to specify the 00164 // interval of time for which to hold packets before 00165 // sending them. 00166 // 00167 // If you enable this mode, you may also need to 00168 // periodically call consider_flush() to flush the queue 00169 // if no packets have been sent recently. 00170 //////////////////////////////////////////////////////////////////// 00171 INLINE void SocketStream:: 00172 set_collect_tcp(bool collect_tcp) { 00173 _collect_tcp = collect_tcp; 00174 } 00175 00176 //////////////////////////////////////////////////////////////////// 00177 // Function: SocketStream::get_collect_tcp 00178 // Access: Published 00179 // Description: Returns the current setting of "collect-tcp" mode. 00180 // See set_collect_tcp(). 00181 //////////////////////////////////////////////////////////////////// 00182 INLINE bool SocketStream:: 00183 get_collect_tcp() const { 00184 return _collect_tcp; 00185 } 00186 00187 //////////////////////////////////////////////////////////////////// 00188 // Function: SocketStream::set_collect_tcp_interval 00189 // Access: Published 00190 // Description: Specifies the interval in time, in seconds, for which 00191 // to hold TCP packets before sending all of the 00192 // recently received packets at once. This only has 00193 // meaning if "collect-tcp" mode is enabled; see 00194 // set_collect_tcp(). 00195 //////////////////////////////////////////////////////////////////// 00196 INLINE void SocketStream:: 00197 set_collect_tcp_interval(double interval) { 00198 _collect_tcp_interval = interval; 00199 } 00200 00201 //////////////////////////////////////////////////////////////////// 00202 // Function: SocketStream::get_collect_tcp_interval 00203 // Access: Published 00204 // Description: Returns the interval in time, in seconds, for which 00205 // to hold TCP packets before sending all of the 00206 // recently received packets at once. This only has 00207 // meaning if "collect-tcp" mode is enabled; see 00208 // set_collect_tcp(). 00209 //////////////////////////////////////////////////////////////////// 00210 INLINE double SocketStream:: 00211 get_collect_tcp_interval() const { 00212 return _collect_tcp_interval; 00213 } 00214 00215 //////////////////////////////////////////////////////////////////// 00216 // Function: SocketStream::consider_flush 00217 // Access: Published 00218 // Description: Sends the most recently queued data if enough time 00219 // has elapsed. This only has meaning if 00220 // set_collect_tcp() has been set to true. 00221 //////////////////////////////////////////////////////////////////// 00222 INLINE bool SocketStream:: 00223 consider_flush() { 00224 if (!_collect_tcp || 00225 ClockObject::get_global_clock()->get_real_time() - _queued_data_start >= _collect_tcp_interval) { 00226 return flush(); 00227 } 00228 return true; 00229 } 00230 00231 //////////////////////////////////////////////////////////////////// 00232 // Function: SocketStream::flush 00233 // Access: Published 00234 // Description: Sends the most recently queued data now. This only 00235 // has meaning if set_collect_tcp() has been set to 00236 // true. 00237 //////////////////////////////////////////////////////////////////// 00238 bool SocketStream:: 00239 flush() { 00240 iostream::flush(); 00241 _queued_data_start = ClockObject::get_global_clock()->get_real_time(); 00242 return !is_closed(); 00243 }