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_TCPCLIENT_H
9354280cfSShuo Chen#define MUDUO_NET_TCPCLIENT_H
10354280cfSShuo Chen
11354280cfSShuo Chen#include <boost/noncopyable.hpp>
12354280cfSShuo Chen
13354280cfSShuo Chen#include "thread/Mutex.h"
14354280cfSShuo Chen#include "TcpConnection.h"
15354280cfSShuo Chen
16354280cfSShuo Chennamespace muduo
17354280cfSShuo Chen{
18354280cfSShuo Chen
19354280cfSShuo Chenclass Connector;
20354280cfSShuo Chentypedef boost::shared_ptr<Connector> ConnectorPtr;
21354280cfSShuo Chen
22354280cfSShuo Chenclass TcpClient : boost::noncopyable
23354280cfSShuo Chen{
24354280cfSShuo Chen public:
25354280cfSShuo Chen  TcpClient(EventLoop* loop,
26354280cfSShuo Chen            const InetAddress& serverAddr);
27354280cfSShuo Chen  ~TcpClient();  // force out-line dtor, for scoped_ptr members.
28354280cfSShuo Chen
29354280cfSShuo Chen  void connect();
30354280cfSShuo Chen  void disconnect();
31354280cfSShuo Chen  void stop();
32354280cfSShuo Chen
33354280cfSShuo Chen  TcpConnectionPtr connection() const
34354280cfSShuo Chen  {
35354280cfSShuo Chen    MutexLockGuard lock(mutex_);
36354280cfSShuo Chen    return connection_;
37354280cfSShuo Chen  }
38354280cfSShuo Chen
39354280cfSShuo Chen  bool retry() const;
40354280cfSShuo Chen  void enableRetry() { retry_ = true; }
41354280cfSShuo Chen
42354280cfSShuo Chen  /// Set connection callback.
43354280cfSShuo Chen  /// Not thread safe.
44354280cfSShuo Chen  void setConnectionCallback(const ConnectionCallback& cb)
45354280cfSShuo Chen  { connectionCallback_ = cb; }
46354280cfSShuo Chen
47354280cfSShuo Chen  /// Set message callback.
48354280cfSShuo Chen  /// Not thread safe.
49354280cfSShuo Chen  void setMessageCallback(const MessageCallback& cb)
50354280cfSShuo Chen  { messageCallback_ = cb; }
51354280cfSShuo Chen
52354280cfSShuo Chen  /// Set write complete callback.
53354280cfSShuo Chen  /// Not thread safe.
54354280cfSShuo Chen  void setWriteCompleteCallback(const WriteCompleteCallback& cb)
55354280cfSShuo Chen  { writeCompleteCallback_ = cb; }
56354280cfSShuo Chen
57354280cfSShuo Chen private:
58354280cfSShuo Chen  /// Not thread safe, but in loop
59354280cfSShuo Chen  void newConnection(int sockfd);
60354280cfSShuo Chen  /// Not thread safe, but in loop
61354280cfSShuo Chen  void removeConnection(const TcpConnectionPtr& conn);
62354280cfSShuo Chen
63354280cfSShuo Chen  EventLoop* loop_;
64354280cfSShuo Chen  ConnectorPtr connector_; // avoid revealing Connector
65354280cfSShuo Chen  ConnectionCallback connectionCallback_;
66354280cfSShuo Chen  MessageCallback messageCallback_;
67354280cfSShuo Chen  WriteCompleteCallback writeCompleteCallback_;
68354280cfSShuo Chen  bool retry_;   // atmoic
69354280cfSShuo Chen  bool connect_; // atomic
70354280cfSShuo Chen  // always in loop thread
71354280cfSShuo Chen  int nextConnId_;
72354280cfSShuo Chen  mutable MutexLock mutex_;
73354280cfSShuo Chen  TcpConnectionPtr connection_; // @BuardedBy mutex_
74354280cfSShuo Chen};
75354280cfSShuo Chen
76354280cfSShuo Chen}
77354280cfSShuo Chen
78354280cfSShuo Chen#endif  // MUDUO_NET_TCPCLIENT_H
79