172 lines
5.6 KiB
C++

//----------------------------------------------------------------------------
//
// Copyright (C) Intel Corporation, 2006 - 2007.
//
// File: TcpSvcHandler.h
//
// Contents: Handles TCP connection
//
// Notes:
//----------------------------------------------------------------------------
#ifndef _MPS_TCP_SVC_HANDLER__H__
#define _MPS_TCP_SVC_HANDLER__H__
//===================================================
// INCLUDES
//===================================================
#include <ace/INET_Addr.h>
#include <ace/SOCK_Acceptor.h>
#include <ace/SOCK_Stream.h>
#include <ace/Svc_Handler.h>
#include <ace/Recursive_Thread_Mutex.h>
#include <ace/Reactor_Notification_Strategy.h>
#include "global.h"
// FW declaration
class Tcp_Supplier;
class Tcp_Consumer;
class Tunnel_consumer;
class AMT_Tunnel_Consumer;
// For debug
static int global_id = 0;
//================================================================
// class TunnelSvcHandler
//
//
//
//================================================================
class Tcp_Svc_Handler : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_MT_SYNCH>
{
friend class Tcp_Consumer;
friend class Tcp_Supplier;
friend class SocksConsumer;
friend class SocksSupplier;
public:
// = The current state of the Connection_Handler.
enum ConnectState
{
IDLE = 1, //
DISCONNECTING, // Handler is in the process of disconnecting.
DISCONNECTED, // Handler is disconnected, but pending all notifications
// in reactor to be dispatched.
};
Tcp_Svc_Handler ( ACE_UINT32 win_size = 0,
int tunnel_handler = -1,
AMT_Tunnel_Consumer* tunnel_consumer = NULL)
:_amt_win_size(win_size),
_tunnel_handler(tunnel_handler),
_tcp_state(IDLE),
_tunnel_consumer(tunnel_consumer),
_flg_mask (ACE_Event_Handler::NULL_MASK),
_identifier("UNKNOWN"),
_notification_strategy(NULL,NULL,ACE_Event_Handler::NULL_MASK),
_unique_id(global_id++),
_supplier(NULL),
_consumer(NULL),
_dispatch_output_counter(0),
_active_counter(0)
{
};
virtual ~Tcp_Svc_Handler (void);
//===================================================
// PROTOTYPE:
//===================================================
//-----------------------------------------
// virtual from ACE_Svc_Handler<>
//-----------------------------------------
virtual int open (void * pVoid);
//-----------------------------------------
// virtual from ACE_Event_Handler
//-----------------------------------------
virtual int handle_input (ACE_HANDLE h);
virtual int handle_output (ACE_HANDLE h);
virtual int handle_close (ACE_HANDLE h , ACE_Reactor_Mask mask);
////-----------------------------------------
// = Set/get the current state.
////-----------------------------------------
void state (ConnectState);
ConnectState state (void) const;
////-----------------------------------------
// = Set/get remote INET addr.
//-----------------------------------------
void remote_addr (ACE_INET_Addr &);
const ACE_INET_Addr &remote_addr (void) const;
////-----------------------------------------
// = Set/get local INET addr.
////-----------------------------------------
void local_addr (ACE_INET_Addr &);
const ACE_INET_Addr &local_addr (void) const;
// Close functions
void shutdown_tcp_svc();
protected:
//===================================================
// PROTECTED FUNCTION
//===================================================
virtual const char* identifier() {return _identifier.c_str();}
virtual Tcp_Supplier* createSupplier(Tcp_Svc_Handler* h);
virtual Tcp_Consumer* createConsumer(Tcp_Svc_Handler* h);
virtual STATUS initConnection();
STATUS initiate_io (ACE_Reactor_Mask mask);
STATUS cancel_io (ACE_Reactor_Mask mask);
int processReturnVal (int result);
//-------------------------------------------------------
// return true on the following cases:
// (1) some other thread is currently handling this object message's
// (so it will handle all messages currently in the queue)
// (2) the object is in DISCONNECT state, but we are NOT the last dispacher
// (it can happend whan some earlier thread handled several of messages,
// so we dont have anything to do)
//-------------------------------------------------------
bool needReturnWithoutWorking(int* reply);
protected:
//===================================================
// PRIVATE FUNCTION
//===================================================
STATUS getStringFullAddr(ACE_INET_Addr& addr , ACE_CString& str1);
void closeSupplier();
void closeConsumer();
//===================================================
// Data Member
//===================================================
ACE_CString _identifier;
Tcp_Supplier* _supplier;
Tcp_Consumer* _consumer;
ACE_Reactor_Mask _flg_mask; // Mask that register in the reactor
ACE_Recursive_Thread_Mutex _flag_mutex;
ACE_Recursive_Thread_Mutex _dispatch_output_counter_mutex;
ACE_UINT32 _dispatch_output_counter;
ACE_Recursive_Thread_Mutex _output_mutex;
ACE_RW_Thread_Mutex _state_mutex;
ACE_INET_Addr _remote_addr; // Address of peer.
ACE_INET_Addr _local_addr; // Address of AMT.
ACE_UINT32 _amt_win_size;
ACE_UINT32 _tunnel_handler; //will be used only on direct farwarding from amt
ConnectState _tcp_state;
AMT_Tunnel_Consumer* _tunnel_consumer; //will be used only on direct farwarding from amt
ACE_Recursive_Thread_Mutex _close_mutex; //taken only in handle_close
ACE_Recursive_Thread_Mutex _active_mutex;
ACE_INT32 _active_counter;
ACE_Reactor_Notification_Strategy _notification_strategy;
// For debug
int _unique_id;
};
#endif // _MPS_TUNNEL_SVC_HANDLER__H__