2021-06-17 14:43:29 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Connector.h
|
|
|
|
* An Tao
|
|
|
|
*
|
|
|
|
* Public header file in trantor lib.
|
|
|
|
*
|
|
|
|
* Copyright 2018, An Tao. All rights reserved.
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the License file.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-02-10 01:24:36 +01:00
|
|
|
#include "core/loops/event_loop.h"
|
2022-02-09 20:39:54 +01:00
|
|
|
#include "core/net/inet_address.h"
|
2021-06-17 14:43:29 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
|
2022-02-10 10:05:10 +01:00
|
|
|
namespace trantor {
|
|
|
|
class Connector : public std::enable_shared_from_this<Connector> {
|
|
|
|
protected:
|
|
|
|
Connector(const Connector &) = delete;
|
|
|
|
Connector &operator=(const Connector &) = delete;
|
|
|
|
// some uncopyable classes maybe support move constructor....
|
|
|
|
Connector(Connector &&) noexcept(true) = default;
|
|
|
|
Connector &operator=(Connector &&) noexcept(true) = default;
|
2021-06-17 14:43:29 +02:00
|
|
|
|
2022-02-10 10:05:10 +01:00
|
|
|
public:
|
|
|
|
using NewConnectionCallback = std::function<void(int sockfd)>;
|
|
|
|
using ConnectionErrorCallback = std::function<void()>;
|
|
|
|
Connector(EventLoop *loop, const InetAddress &addr, bool retry = true);
|
|
|
|
Connector(EventLoop *loop, InetAddress &&addr, bool retry = true);
|
|
|
|
void setNewConnectionCallback(const NewConnectionCallback &cb) {
|
|
|
|
newConnectionCallback_ = cb;
|
|
|
|
}
|
|
|
|
void setNewConnectionCallback(NewConnectionCallback &&cb) {
|
|
|
|
newConnectionCallback_ = std::move(cb);
|
|
|
|
}
|
|
|
|
void setErrorCallback(const ConnectionErrorCallback &cb) {
|
|
|
|
errorCallback_ = cb;
|
|
|
|
}
|
|
|
|
void setErrorCallback(ConnectionErrorCallback &&cb) {
|
|
|
|
errorCallback_ = std::move(cb);
|
|
|
|
}
|
|
|
|
const InetAddress &serverAddress() const {
|
|
|
|
return serverAddr_;
|
|
|
|
}
|
|
|
|
void start();
|
|
|
|
void restart();
|
|
|
|
void stop();
|
2021-06-17 14:43:29 +02:00
|
|
|
|
2022-02-10 10:05:10 +01:00
|
|
|
private:
|
|
|
|
NewConnectionCallback newConnectionCallback_;
|
|
|
|
ConnectionErrorCallback errorCallback_;
|
|
|
|
enum class Status {
|
|
|
|
Disconnected,
|
|
|
|
Connecting,
|
|
|
|
Connected
|
|
|
|
};
|
|
|
|
static constexpr int kMaxRetryDelayMs = 30 * 1000;
|
|
|
|
static constexpr int kInitRetryDelayMs = 500;
|
|
|
|
std::shared_ptr<Channel> channelPtr_;
|
|
|
|
EventLoop *loop_;
|
|
|
|
InetAddress serverAddr_;
|
2021-06-17 14:43:29 +02:00
|
|
|
|
2022-02-10 10:05:10 +01:00
|
|
|
std::atomic_bool connect_{ false };
|
|
|
|
std::atomic<Status> status_{ Status::Disconnected };
|
2021-06-17 14:43:29 +02:00
|
|
|
|
2022-02-10 10:05:10 +01:00
|
|
|
int retryInterval_{ kInitRetryDelayMs };
|
|
|
|
int maxRetryInterval_{ kMaxRetryDelayMs };
|
2021-06-17 14:43:29 +02:00
|
|
|
|
2022-02-10 10:05:10 +01:00
|
|
|
bool retry_;
|
|
|
|
|
|
|
|
void startInLoop();
|
|
|
|
void connect();
|
|
|
|
void connecting(int sockfd);
|
|
|
|
int removeAndResetChannel();
|
|
|
|
void handleWrite();
|
|
|
|
void handleError();
|
|
|
|
void retry(int sockfd);
|
2021-06-17 14:43:29 +02:00
|
|
|
};
|
|
|
|
|
2022-02-10 10:05:10 +01:00
|
|
|
} // namespace trantor
|