Connector.h revision 354280cf
1354280cfSShuo Chen// excerpts from http://code.google.com/p/muduo/ 2354280cfSShuo Chen// 3354280cfSShuo Chen// Use of this source code is governed by a BSD-style license 4354280cfSShuo Chen// that can be found in the License file. 5354280cfSShuo Chen// 6354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7354280cfSShuo Chen 8354280cfSShuo Chen#ifndef MUDUO_NET_CONNECTOR_H 9354280cfSShuo Chen#define MUDUO_NET_CONNECTOR_H 10354280cfSShuo Chen 11354280cfSShuo Chen#include "InetAddress.h" 12354280cfSShuo Chen#include "TimerId.h" 13354280cfSShuo Chen 14354280cfSShuo Chen#include <boost/enable_shared_from_this.hpp> 15354280cfSShuo Chen#include <boost/function.hpp> 16354280cfSShuo Chen#include <boost/noncopyable.hpp> 17354280cfSShuo Chen#include <boost/scoped_ptr.hpp> 18354280cfSShuo Chen 19354280cfSShuo Chennamespace muduo 20354280cfSShuo Chen{ 21354280cfSShuo Chen 22354280cfSShuo Chenclass Channel; 23354280cfSShuo Chenclass EventLoop; 24354280cfSShuo Chen 25354280cfSShuo Chenclass Connector : boost::noncopyable 26354280cfSShuo Chen{ 27354280cfSShuo Chen public: 28354280cfSShuo Chen typedef boost::function<void (int sockfd)> NewConnectionCallback; 29354280cfSShuo Chen 30354280cfSShuo Chen Connector(EventLoop* loop, const InetAddress& serverAddr); 31354280cfSShuo Chen ~Connector(); 32354280cfSShuo Chen 33354280cfSShuo Chen void setNewConnectionCallback(const NewConnectionCallback& cb) 34354280cfSShuo Chen { newConnectionCallback_ = cb; } 35354280cfSShuo Chen 36354280cfSShuo Chen void start(); // can be called in any thread 37354280cfSShuo Chen void restart(); // must be called in loop thread 38354280cfSShuo Chen void stop(); // can be called in any thread 39354280cfSShuo Chen 40354280cfSShuo Chen const InetAddress& serverAddress() const { return serverAddr_; } 41354280cfSShuo Chen 42354280cfSShuo Chen private: 43354280cfSShuo Chen enum States { kDisconnected, kConnecting, kConnected }; 44354280cfSShuo Chen static const int kMaxRetryDelayMs = 30*1000; 45354280cfSShuo Chen static const int kInitRetryDelayMs = 500; 46354280cfSShuo Chen 47354280cfSShuo Chen void setState(States s) { state_ = s; } 48354280cfSShuo Chen void startInLoop(); 49354280cfSShuo Chen void connect(); 50354280cfSShuo Chen void connecting(int sockfd); 51354280cfSShuo Chen void handleWrite(); 52354280cfSShuo Chen void handleError(); 53354280cfSShuo Chen void retry(int sockfd); 54354280cfSShuo Chen int removeAndResetChannel(); 55354280cfSShuo Chen void resetChannel(); 56354280cfSShuo Chen 57354280cfSShuo Chen EventLoop* loop_; 58354280cfSShuo Chen InetAddress serverAddr_; 59354280cfSShuo Chen bool connect_; // atomic 60354280cfSShuo Chen States state_; // FIXME: use atomic variable 61354280cfSShuo Chen boost::scoped_ptr<Channel> channel_; 62354280cfSShuo Chen NewConnectionCallback newConnectionCallback_; 63354280cfSShuo Chen int retryDelayMs_; 64354280cfSShuo Chen TimerId timerId_; 65354280cfSShuo Chen}; 66354280cfSShuo Chentypedef boost::shared_ptr<Connector> ConnectorPtr; 67354280cfSShuo Chen 68354280cfSShuo Chen} 69354280cfSShuo Chen 70354280cfSShuo Chen#endif // MUDUO_NET_CONNECTOR_H 71