Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #ifndef _PLUGIN_VC_H_
00037 #define _PLUGIN_VC_H_
00038 
00039 #include "Plugin.h"
00040 #include "P_Net.h"
00041 #include "ink_atomic.h"
00042 
00043 class PluginVCCore;
00044 
00045 struct PluginVCState
00046 {
00047   PluginVCState();
00048   VIO vio;
00049   bool shutdown;
00050 };
00051 
00052 inline
00053 PluginVCState::PluginVCState():
00054 vio(),
00055 shutdown(false)
00056 {
00057 }
00058 
00059 enum PluginVC_t
00060 {
00061   PLUGIN_VC_UNKNOWN,
00062   PLUGIN_VC_ACTIVE,
00063   PLUGIN_VC_PASSIVE
00064 };
00065 
00066 
00067 enum
00068 {
00069   PLUGIN_VC_DATA_LOCAL,
00070   PLUGIN_VC_DATA_REMOTE
00071 };
00072 
00073 enum
00074 {
00075   PLUGIN_VC_MAGIC_ALIVE = 0xaabbccdd,
00076   PLUGIN_VC_MAGIC_DEAD = 0xaabbdead
00077 };
00078 
00079 class PluginVC:public NetVConnection, public PluginIdentity
00080 {
00081   friend class PluginVCCore;
00082 public:
00083 
00084     PluginVC(PluginVCCore *core_obj);
00085    ~PluginVC();
00086 
00087   virtual VIO *do_io_read(Continuation * c = NULL, int64_t nbytes = INT64_MAX, MIOBuffer * buf = 0);
00088 
00089   virtual VIO *do_io_write(Continuation * c = NULL, int64_t nbytes = INT64_MAX, IOBufferReader * buf = 0, bool owner = false);
00090 
00091   virtual void do_io_close(int lerrno = -1);
00092   virtual void do_io_shutdown(ShutdownHowTo_t howto);
00093 
00094   
00095   virtual void reenable(VIO * vio);
00096   virtual void reenable_re(VIO * vio);
00097 
00098   
00099   virtual void set_active_timeout(ink_hrtime timeout_in);
00100   virtual void set_inactivity_timeout(ink_hrtime timeout_in);
00101   virtual void cancel_active_timeout();
00102   virtual void cancel_inactivity_timeout();
00103   virtual ink_hrtime get_active_timeout();
00104   virtual ink_hrtime get_inactivity_timeout();
00105 
00106   
00107   virtual SOCKET get_socket();
00108   virtual void set_local_addr();
00109   virtual void set_remote_addr();
00110   virtual int set_tcp_init_cwnd(int init_cwnd);
00111   virtual void apply_options();
00112 
00113   virtual bool get_data(int id, void *data);
00114   virtual bool set_data(int id, void *data);
00115 
00116   virtual PluginVC* get_other_side() { return other_side; }
00117 
00118 
00119 
00120   virtual char const* getPluginTag() const { return plugin_tag; }
00121 
00122   virtual int64_t getPluginId() const { return plugin_id; }
00123 
00124 
00125   virtual void setPluginTag(char const* tag) { plugin_tag = tag; }
00126 
00127   virtual void setPluginId(int64_t id) { plugin_id = id; }
00128 
00129 
00130   int main_handler(int event, void *data);
00131 
00132 private:
00133   void process_read_side(bool);
00134   void process_write_side(bool);
00135   void process_close();
00136   void process_timeout(Event * e, int event_to_send, Event ** our_eptr);
00137 
00138   void setup_event_cb(ink_hrtime in, Event ** e_ptr);
00139 
00140   void update_inactive_time();
00141   int64_t transfer_bytes(MIOBuffer * transfer_to, IOBufferReader * transfer_from, int64_t act_on);
00142 
00143   uint32_t magic;
00144   PluginVC_t vc_type;
00145   PluginVCCore *core_obj;
00146 
00147   PluginVC *other_side;
00148 
00149   PluginVCState read_state;
00150   PluginVCState write_state;
00151 
00152   bool need_read_process;
00153   bool need_write_process;
00154 
00155   volatile bool closed;
00156   Event *sm_lock_retry_event;
00157   Event *core_lock_retry_event;
00158 
00159   bool deletable;
00160   int reentrancy_count;
00161 
00162   ink_hrtime active_timeout;
00163   Event *active_event;
00164 
00165   ink_hrtime inactive_timeout;
00166   ink_hrtime inactive_timeout_at;
00167   Event *inactive_event;
00168 
00169   char const* plugin_tag;
00170   int64_t plugin_id;
00171 };
00172 
00173 class PluginVCCore:public Continuation
00174 {
00175   friend class PluginVC;
00176 public:
00177     PluginVCCore();
00178    ~PluginVCCore();
00179 
00180   static PluginVCCore *alloc();
00181   void init();
00182   void set_accept_cont(Continuation * c);
00183 
00184   int state_send_accept(int event, void *data);
00185   int state_send_accept_failed(int event, void *data);
00186 
00187   void attempt_delete();
00188 
00189   PluginVC *connect();
00190   Action *connect_re(Continuation * c);
00191   void kill_no_connect();
00192 
00193 
00194   void set_active_addr(
00195                        in_addr_t ip, 
00196                        int port 
00197                        );
00198 
00199   void set_active_addr(
00200                        sockaddr const *ip 
00201                        );
00202 
00203   void set_passive_addr(
00204                         in_addr_t ip, 
00205                         int port 
00206                         );
00207 
00208   void set_passive_addr(
00209                         sockaddr const* ip 
00210                         );
00211 
00212   void set_active_data(void *data);
00213   void set_passive_data(void *data);
00214 
00215   void set_transparent(bool passive_side, bool active_side);
00216 
00217 
00218   void set_plugin_id(int64_t id);
00219 
00220   void set_plugin_tag(char const* tag);
00221 
00222   
00223   
00224   
00225   PluginVC active_vc;
00226   PluginVC passive_vc;
00227 private:
00228 
00229   void destroy();
00230 
00231   Continuation *connect_to;
00232   bool connected;
00233 
00234   MIOBuffer *p_to_a_buffer;
00235   IOBufferReader *p_to_a_reader;
00236 
00237   MIOBuffer *a_to_p_buffer;
00238   IOBufferReader *a_to_p_reader;
00239 
00240   IpEndpoint passive_addr_struct;
00241   IpEndpoint active_addr_struct;
00242 
00243   void *passive_data;
00244   void *active_data;
00245 
00246   static vint32 nextid;
00247   unsigned id;
00248 };
00249 
00250 inline
00251 PluginVCCore::PluginVCCore():
00252 active_vc(this),
00253 passive_vc(this),
00254 connect_to(NULL),
00255 connected(false),
00256 p_to_a_buffer(NULL),
00257 p_to_a_reader(NULL),
00258 a_to_p_buffer(NULL),
00259 a_to_p_reader(NULL),
00260 passive_data(NULL),
00261 active_data(NULL),
00262 id(0)
00263 {
00264   memset(&active_addr_struct, 0, sizeof active_addr_struct);
00265   memset(&passive_addr_struct, 0, sizeof passive_addr_struct);
00266 
00267   id = ink_atomic_increment(&nextid, 1);
00268 }
00269 
00270 #endif