TcpClient.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_TCPCLIENT_H 9a1bde736SShuo Chen#define MUDUO_NET_TCPCLIENT_H 10a1bde736SShuo Chen 11a1bde736SShuo Chen#include <boost/noncopyable.hpp> 12a1bde736SShuo Chen 13a1bde736SShuo Chen#include "thread/Mutex.h" 14a1bde736SShuo Chen#include "TcpConnection.h" 15a1bde736SShuo Chen 16a1bde736SShuo Chennamespace muduo 17a1bde736SShuo Chen{ 18a1bde736SShuo Chen 19a1bde736SShuo Chenclass Connector; 20a1bde736SShuo Chentypedef boost::shared_ptr<Connector> ConnectorPtr; 21a1bde736SShuo Chen 22a1bde736SShuo Chenclass TcpClient : boost::noncopyable 23a1bde736SShuo Chen{ 24a1bde736SShuo Chen public: 25a1bde736SShuo Chen TcpClient(EventLoop* loop, 26a1bde736SShuo Chen const InetAddress& serverAddr); 27a1bde736SShuo Chen ~TcpClient(); // force out-line dtor, for scoped_ptr members. 28a1bde736SShuo Chen 29a1bde736SShuo Chen void connect(); 30a1bde736SShuo Chen void disconnect(); 31a1bde736SShuo Chen void stop(); 32a1bde736SShuo Chen 33a1bde736SShuo Chen TcpConnectionPtr connection() const 34a1bde736SShuo Chen { 35a1bde736SShuo Chen MutexLockGuard lock(mutex_); 36a1bde736SShuo Chen return connection_; 37a1bde736SShuo Chen } 38a1bde736SShuo Chen 39a1bde736SShuo Chen bool retry() const; 40a1bde736SShuo Chen void enableRetry() { retry_ = true; } 41a1bde736SShuo Chen 42a1bde736SShuo Chen /// Set connection callback. 43a1bde736SShuo Chen /// Not thread safe. 44a1bde736SShuo Chen void setConnectionCallback(const ConnectionCallback& cb) 45a1bde736SShuo Chen { connectionCallback_ = cb; } 46a1bde736SShuo Chen 47a1bde736SShuo Chen /// Set message callback. 48a1bde736SShuo Chen /// Not thread safe. 49a1bde736SShuo Chen void setMessageCallback(const MessageCallback& cb) 50a1bde736SShuo Chen { messageCallback_ = cb; } 51a1bde736SShuo Chen 52a1bde736SShuo Chen /// Set write complete callback. 53a1bde736SShuo Chen /// Not thread safe. 54a1bde736SShuo Chen void setWriteCompleteCallback(const WriteCompleteCallback& cb) 55a1bde736SShuo Chen { writeCompleteCallback_ = cb; } 56a1bde736SShuo Chen 57a1bde736SShuo Chen private: 58a1bde736SShuo Chen /// Not thread safe, but in loop 59a1bde736SShuo Chen void newConnection(int sockfd); 60a1bde736SShuo Chen /// Not thread safe, but in loop 61a1bde736SShuo Chen void removeConnection(const TcpConnectionPtr& conn); 62a1bde736SShuo Chen 63a1bde736SShuo Chen EventLoop* loop_; 64a1bde736SShuo Chen ConnectorPtr connector_; // avoid revealing Connector 65a1bde736SShuo Chen ConnectionCallback connectionCallback_; 66a1bde736SShuo Chen MessageCallback messageCallback_; 67a1bde736SShuo Chen WriteCompleteCallback writeCompleteCallback_; 68a1bde736SShuo Chen bool retry_; // atmoic 69a1bde736SShuo Chen bool connect_; // atomic 70a1bde736SShuo Chen // always in loop thread 71a1bde736SShuo Chen int nextConnId_; 72a1bde736SShuo Chen mutable MutexLock mutex_; 73a1bde736SShuo Chen TcpConnectionPtr connection_; // @BuardedBy mutex_ 74a1bde736SShuo Chen}; 75a1bde736SShuo Chen 76a1bde736SShuo Chen} 77a1bde736SShuo Chen 78a1bde736SShuo Chen#endif // MUDUO_NET_TCPCLIENT_H 79