00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef CONNECTIONREADER_H
00020 #define CONNECTIONREADER_H
00021
00022 #include <pandabase.h>
00023
00024 #include "connection.h"
00025
00026 #include <pointerTo.h>
00027
00028 #include <prio.h>
00029 #include <prthread.h>
00030 #include <prlock.h>
00031 #include "pvector.h"
00032 #include "pset.h"
00033
00034 class NetDatagram;
00035 class ConnectionManager;
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 class EXPCL_PANDA ConnectionReader {
00060 PUBLISHED:
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 ConnectionReader(ConnectionManager *manager, int num_threads);
00073 virtual ~ConnectionReader();
00074
00075 bool add_connection(const PT(Connection) &connection);
00076 bool remove_connection(const PT(Connection) &connection);
00077 bool is_connection_ok(const PT(Connection) &connection);
00078
00079 void poll();
00080
00081 ConnectionManager *get_manager() const;
00082 bool is_polling() const;
00083 int get_num_threads() const;
00084
00085 void set_raw_mode(bool mode);
00086 bool get_raw_mode() const;
00087
00088 protected:
00089 virtual void receive_datagram(const NetDatagram &datagram)=0;
00090
00091 class SocketInfo {
00092 public:
00093 SocketInfo(const PT(Connection) &connection);
00094 bool is_udp() const;
00095 PRFileDesc *get_socket() const;
00096
00097 PT(Connection) _connection;
00098 bool _busy;
00099 bool _error;
00100 };
00101
00102 void shutdown();
00103 void clear_manager();
00104 void finish_socket(SocketInfo *sinfo);
00105
00106 virtual void process_incoming_data(SocketInfo *sinfo);
00107 virtual void process_incoming_udp_data(SocketInfo *sinfo);
00108 virtual void process_incoming_tcp_data(SocketInfo *sinfo);
00109 virtual void process_raw_incoming_udp_data(SocketInfo *sinfo);
00110 virtual void process_raw_incoming_tcp_data(SocketInfo *sinfo);
00111
00112 private:
00113 static void thread_start(void *data);
00114 void thread_run();
00115
00116 SocketInfo *get_next_available_socket(PRIntervalTime timeout,
00117 PRInt32 current_thread_index);
00118
00119 void rebuild_poll_list();
00120
00121 protected:
00122 ConnectionManager *_manager;
00123
00124 private:
00125 bool _raw_mode;
00126 bool _shutdown;
00127
00128 typedef pvector<PRThread *> Threads;
00129 Threads _threads;
00130 PRLock *_startup_mutex;
00131 bool _polling;
00132
00133
00134
00135 typedef pvector<PRPollDesc> Poll;
00136 typedef pvector<SocketInfo *> Sockets;
00137 Poll _poll;
00138 Sockets _polled_sockets;
00139 int _next_index;
00140 int _num_results;
00141
00142
00143 PRLock *_select_mutex;
00144
00145
00146
00147
00148 PRInt32 _currently_polling_thread;
00149
00150
00151
00152 Sockets _sockets;
00153
00154
00155 Sockets _removed_sockets;
00156
00157
00158 bool _reexamine_sockets;
00159
00160 PRLock *_sockets_mutex;
00161
00162
00163 friend class ConnectionManager;
00164 };
00165
00166 #endif