Connector.h revision a1bde736
1a1bde736SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2a1bde736SShuo Chen// 3a1bde736SShuo Chen// Use of this source code is governed by a BSD-style license 4a1bde736SShuo Chen// that can be found in the License file. 5a1bde736SShuo Chen// 6a1bde736SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7a1bde736SShuo Chen 8a1bde736SShuo Chen#ifndef MUDUO_NET_CONNECTOR_H 9a1bde736SShuo Chen#define MUDUO_NET_CONNECTOR_H 10a1bde736SShuo Chen 11a1bde736SShuo Chen#include "InetAddress.h" 12a1bde736SShuo Chen#include "TimerId.h" 13a1bde736SShuo Chen 14a1bde736SShuo Chen#include <boost/enable_shared_from_this.hpp> 15a1bde736SShuo Chen#include <boost/function.hpp> 16a1bde736SShuo Chen#include <boost/noncopyable.hpp> 17a1bde736SShuo Chen#include <boost/scoped_ptr.hpp> 18a1bde736SShuo Chen 19a1bde736SShuo Chennamespace muduo 20a1bde736SShuo Chen{ 21a1bde736SShuo Chen 22a1bde736SShuo Chenclass Channel; 23a1bde736SShuo Chenclass EventLoop; 24a1bde736SShuo Chen 25a1bde736SShuo Chenclass Connector : boost::noncopyable 26a1bde736SShuo Chen{ 27a1bde736SShuo Chen public: 28a1bde736SShuo Chen typedef boost::function<void (int sockfd)> NewConnectionCallback; 29a1bde736SShuo Chen 30a1bde736SShuo Chen Connector(EventLoop* loop, const InetAddress& serverAddr); 31a1bde736SShuo Chen ~Connector(); 32a1bde736SShuo Chen 33a1bde736SShuo Chen void setNewConnectionCallback(const NewConnectionCallback& cb) 34a1bde736SShuo Chen { newConnectionCallback_ = cb; } 35a1bde736SShuo Chen 36a1bde736SShuo Chen void start(); // can be called in any thread 37a1bde736SShuo Chen void restart(); // must be called in loop thread 38a1bde736SShuo Chen void stop(); // can be called in any thread 39a1bde736SShuo Chen 40a1bde736SShuo Chen const InetAddress& serverAddress() const { return serverAddr_; } 41a1bde736SShuo Chen 42a1bde736SShuo Chen private: 43a1bde736SShuo Chen enum States { kDisconnected, kConnecting, kConnected }; 44a1bde736SShuo Chen static const int kMaxRetryDelayMs = 30*1000; 45a1bde736SShuo Chen static const int kInitRetryDelayMs = 500; 46a1bde736SShuo Chen 47a1bde736SShuo Chen void setState(States s) { state_ = s; } 48a1bde736SShuo Chen void startInLoop(); 49a1bde736SShuo Chen void connect(); 50a1bde736SShuo Chen void connecting(int sockfd); 51a1bde736SShuo Chen void handleWrite(); 52a1bde736SShuo Chen void handleError(); 53a1bde736SShuo Chen void retry(int sockfd); 54a1bde736SShuo Chen int removeAndResetChannel(); 55a1bde736SShuo Chen void resetChannel(); 56a1bde736SShuo Chen 57a1bde736SShuo Chen EventLoop* loop_; 58a1bde736SShuo Chen InetAddress serverAddr_; 59a1bde736SShuo Chen bool connect_; // atomic 60a1bde736SShuo Chen States state_; // FIXME: use atomic variable 61a1bde736SShuo Chen boost::scoped_ptr<Channel> channel_; 62a1bde736SShuo Chen NewConnectionCallback newConnectionCallback_; 63a1bde736SShuo Chen int retryDelayMs_; 64a1bde736SShuo Chen TimerId timerId_; 65a1bde736SShuo Chen}; 66a1bde736SShuo Chentypedef boost::shared_ptr<Connector> ConnectorPtr; 67a1bde736SShuo Chen 68a1bde736SShuo Chen} 69a1bde736SShuo Chen 70a1bde736SShuo Chen#endif // MUDUO_NET_CONNECTOR_H 71