1 #ifndef __NONECLOCKING_CONNECTTION_H_ 2 #define __NONECLOCKING_CONNECTTION_H_ 15 #include "pandabase.h" 16 #include "socket_base.h" 19 #include "buffered_datagramreader.h" 20 #include "buffered_datagramwriter.h" 21 #include "config_nativenet.h" 38 struct AddressQueue :
private pvector<Socket_Address>
42 INLINE AddressQueue() : _active_index(0) {}
45 size_t the_size = size();
50 if (_active_index >= the_size) {
54 out = (*this)[_active_index++];
64 for(ii = begin(); ii != end(); ii++)
75 virtual void PostConnect(
void) { };
76 virtual void NewWriteBuffer(
void) { };
78 inline void ClearAll(
void);
80 inline bool SendMessageBufferOnly(
Datagram &msg);
82 inline bool GetMessage(
Datagram &val);
83 inline bool DoConnect(
void);
84 inline bool IsConnected(
void);
88 inline bool SendMessage(
const Datagram &msg);
89 inline bool Flush(
void);
90 inline void Reset(
void);
94 inline void WaitForNetworkReadEvent(PN_stdfloat MaxTime)
97 fdset.setForSocket(*
this);
105 inline size_t AddressQueueSize() {
return _Addresslist.size(); };
107 inline void ClearAddresses(
void);
111 AddressQueue _Addresslist;
121 static void init_type() {
122 Socket_IP::init_type();
123 register_type(_type_handle,
"Buffered_DatagramConnection",
124 Socket_IP::get_class_type());
127 return get_class_type();
129 virtual TypeHandle force_init_type() {init_type();
return get_class_type();}
142 inline void Buffered_DatagramConnection::ClearAll(
void) {
143 nativenet_cat.error() <<
"Buffered_DatagramConnection::ClearAll Starting Auto Reset\n";
149 inline bool Buffered_DatagramConnection::DoConnect(
void) {
150 if(!_Addresslist.GetNext(_Adddress))
153 if(ActiveOpen(_Adddress,
true) ==
true) {
166 // Function name : Buffered_DatagramConnection::DoConnect
167 // Description : This is the function thah does the conection for us
169 // Return type : inline bool
172 inline bool Buffered_DatagramConnection::DoConnect(void) {
173 if(Active() != true) {
174 if(_LastConnectTry.Expired() != true)
177 if(!_Addresslist.GetNext(_Adddress)) // lookup the proper value...
180 if(ActiveOpen(_Adddress) == true) {
181 _LastConnectTry.ReStart();
182 _tryingToOpen = true; // set the flag indicating we are trying to open up
183 SetNonBlocking(); // maybe should be blocking?
184 SetSendBufferSize(1024*50); // we need to hand tune these for the os we are using
185 SetRecvBufferSize(1024*50);
193 if(_tryingToOpen) { // okay handle the i am connecting state....
195 fdset.setForSocket(*this);
196 Socket_Selector selector;
197 if(selector.WaitFor_All(fdset,0) >0) {
198 _tryingToOpen = false;
199 if(selector._error.IsSetFor(*this) == true) { // means we are in errorconnected. else writable
201 return false; // error on connect
204 return true; // just got connected
206 return true; // still connecting
234 : _Writer(wbufsize,write_flush_point) , _Reader(rbufsize)
236 nativenet_cat.error() <<
"Buffered_DatagramConnection Constructor rbufsize = " << rbufsize
237 <<
" wbufsize = " << wbufsize <<
" write_flush_point = " << write_flush_point <<
"\n";
255 nativenet_cat.warning() <<
"Buffered_DatagramConnection::SendMessage->Error On Write--Out Buffer = " << _Writer.AmountBuffered() <<
"\n";
259 #if PY_VERSION_HEX >= 0x03030000 260 PyObject *exc_type = PyExc_ConnectionError;
262 PyObject *exc_type = PyExc_OSError;
265 s << endl <<
"Error sending message: " << endl;
268 s <<
"Message data: " << msg.
get_data() << endl;
270 string message = s.str();
271 PyErr_SetString(exc_type, message.c_str());
279 inline bool Buffered_DatagramConnection::SendMessageBufferOnly(
Datagram &msg)
285 nativenet_cat.error() <<
"Buffered_DatagramConnection::SendMessageBufferOnly->Error On Write--Out Buffer = " << _Writer.AmountBuffered() <<
"\n";
299 _Addresslist.push_back(inadr);
302 inline void Buffered_DatagramConnection::ClearAddresses(
void)
304 _Addresslist.clear();
320 int ans1 = _Reader.PumpMessageReader(val,*
this);
324 nativenet_cat.error() <<
"Buffered_DatagramConnection::GetMessage->Error On PumpMessageReader--Out Buffer = " << _Writer.AmountBuffered() <<
"\n";
346 int flush_resp = _Writer.FlushNoBlock(*
this);
349 nativenet_cat.error() <<
"Buffered_DatagramConnection::Flush->Error On Flush [" <<
GetLastError() <<
"]\n" 351 <<
"Buffered_DatagramConnection::Flush->Error ..Write--Out Buffer = " << _Writer.AmountBuffered() <<
"\n";
368 nativenet_cat.error() <<
"Buffered_DatagramConnection::Reset()\n";
373 inline bool Buffered_DatagramConnection::IsConnected(
void) {
374 return (
Active() ==
true );
378 #endif //__NONECLOCKING_CONNECTTION_H_ void AddAddress(Socket_Address &inadr)
must be called to set value to the server
Base functionality for a TCP connected socket This class is pretty useless by itself but it does hide...
void dump_hex(ostream &out, unsigned int indent=0) const
Writes a representation of the entire datagram contents, as a sequence of hex (and ASCII) values...
bool Active()
Ask if the socket is open (allocated)
bool SendMessage(const Datagram &msg)
send the message
bool Flush(void)
flush all wrightes
This is our own Panda specialization on the default STL vector.
bool GetMessage(Datagram &val)
read a message
Buffered_DatagramConnection(int rbufsize, int wbufsize, int write_flush_point)
Return type : inline Argument : bool do_blocking_writes Argument : int rbufsize Argument : int wbufsi...
int AddData(const void *data, size_t len, Socket_TCP &sck)
Return type : inline int Argument : const void * data Argument : int len Argument : Socket_TCP &sck...
virtual ~Buffered_DatagramConnection(void)
Return type : inline Argument : void.
A simple place to store and munipulate tcp and port address for communication layer.
static int GetLastError()
gets the last errcode from a socket operation
This is the buffered writer.
TypeHandle is the identifier used to differentiate C++ class types.
int WaitFor_Read_Error(const Socket_fdset &fd, const Time_Span &timeout)
Helper function for WaitFor Only looks for readability and errors.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
size_t get_length() const
Returns the number of bytes in the datagram.
const void * get_data() const
Returns a pointer to the beginning of the datagram's data.