Skip to content

Commit

Permalink
remove close() method for #29
Browse files Browse the repository at this point in the history
  • Loading branch information
fawdlstty committed Jun 24, 2022
1 parent dde158d commit a18ad0b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 71 deletions.
8 changes: 1 addition & 7 deletions docs/en_us/3_TcpSslClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,4 @@ co_await _conn->Send (_str.data (), _str.size ());
## Close connection
Actively close the connection:
```cpp
co_await _conn->Close ();
```

In addition to active closing, the connection is automatically closed as long as the connection object is not referenced by the code and is automatically freed by the smart pointer.
As long as the connection object is not referenced by the code, it is automatically freed by the smart pointer and the link is closed automatically.
8 changes: 1 addition & 7 deletions docs/zh_hans/3_TcpSslClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,4 @@ co_await _conn->Send (_str.data (), _str.size ());
## 关闭连接
主动关闭连接:
```cpp
co_await _conn->Close ();
```

除了主动关闭外,只要连接对象不被代码所引用,受智能指针自动释放,也会自动关闭链接。
只要连接对象不被代码所引用,受智能指针自动释放,就会自动关闭链接。
14 changes: 4 additions & 10 deletions include/fv/conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct IConn2 {
IConn2 () = default;
virtual ~IConn2 () = default;
virtual bool IsConnect () = 0;
virtual void Close () = 0;
virtual Task<void> Send (char *_data, size_t _size) = 0;
virtual void Cancel () = 0;

Expand All @@ -47,11 +46,10 @@ struct IConn: public IConn2 {
struct TcpConn: public IConn {
std::shared_ptr<Tcp::socket> Socket;

virtual ~TcpConn () { Cancel (); Close (); }
virtual ~TcpConn () { Cancel (); }
Task<void> Connect (std::string _host, std::string _port) override;
Task<void> Reconnect () override;
bool IsConnect () override { return Socket->is_open (); }
void Close () override;
Task<void> Send (char *_data, size_t _size) override;
void Cancel () override;

Expand All @@ -67,9 +65,8 @@ struct TcpConn2: public IConn2 {

TcpConn2 (Tcp::socket _sock);

virtual ~TcpConn2 () { Cancel (); Close (); }
virtual ~TcpConn2 () { Cancel (); }
bool IsConnect () override { return Socket.is_open (); }
void Close () override;
Task<void> Send (char *_data, size_t _size) override;
void Cancel () override;

Expand All @@ -83,11 +80,10 @@ struct SslConn: public IConn {
Ssl::context SslCtx { Config::SslClientVer };
std::shared_ptr<Ssl::stream<Tcp::socket>> SslSocket;

virtual ~SslConn () { Cancel (); Close (); }
virtual ~SslConn () { Cancel (); }
Task<void> Connect (std::string _host, std::string _port) override;
Task<void> Reconnect () override;
bool IsConnect () override { return SslSocket && SslSocket->next_layer ().is_open (); }
void Close () override;
Task<void> Send (char *_data, size_t _size) override;
void Cancel () override;

Expand All @@ -103,9 +99,8 @@ struct SslConn2: public IConn2 {

SslConn2 (Ssl::stream<Tcp::socket> _sock);

virtual ~SslConn2 () { Cancel (); Close (); }
virtual ~SslConn2 () { Cancel (); }
bool IsConnect () override { return SslSocket.next_layer ().is_open (); }
void Close () override;
Task<void> Send (char *_data, size_t _size) override;
void Cancel () override;

Expand All @@ -121,7 +116,6 @@ struct WsConn: public std::enable_shared_from_this<WsConn> {
std::atomic_bool Run { true };

WsConn (std::shared_ptr<IConn2> _parent, bool _is_client);
~WsConn () { Close (); }
void Init ();
bool IsConnect () { return Parent && Parent->IsConnect (); }
Task<void> SendText (char *_data, size_t _size) { co_await _Send (_data, _size, WsType::Text); }
Expand Down
55 changes: 8 additions & 47 deletions include/fv/conn_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ inline Task<void> TcpConn::Connect (std::string _host, std::string _port) {
}

inline Task<void> TcpConn::Reconnect () {
Close ();
Socket = nullptr;
TmpData = "";
std::regex _rgx { "(\\d+\\.){3}\\d+" };
std::string _ip = m_host;
Expand All @@ -105,17 +105,6 @@ inline Task<void> TcpConn::Reconnect () {
Socket->set_option (Tcp::no_delay { true });
}

inline void TcpConn::Close () {
try {
if (Socket && Socket->is_open ()) {
//Socket.shutdown (SocketBase::shutdown_both);
Socket->close ();
}
Socket = nullptr;
} catch (...) {
}
}

inline Task<void> TcpConn::Send (char *_data, size_t _size) {
if (!Socket->is_open ())
throw Exception ("Cannot send data to a closed connection.");
Expand All @@ -141,6 +130,7 @@ inline void TcpConn::Cancel () {
Socket->cancel ();
} catch (...) {
}
Socket = nullptr;
}


Expand All @@ -150,16 +140,6 @@ inline TcpConn2::TcpConn2 (Tcp::socket _sock): Socket (std::move (_sock)) {
Socket.set_option (Tcp::no_delay { true });
}

inline void TcpConn2::Close () {
try {
if (Socket.is_open ()) {
//Socket.shutdown (SocketBase::shutdown_both);
Socket.close ();
}
} catch (...) {
}
}

inline Task<void> TcpConn2::Send (char *_data, size_t _size) {
if (!Socket.is_open ())
throw Exception ("Cannot send data to a closed connection.");
Expand All @@ -174,7 +154,8 @@ inline Task<void> TcpConn2::Send (char *_data, size_t _size) {

inline void TcpConn2::Cancel () {
try {
Socket.cancel ();
if (Socket.is_open ())
Socket.cancel ();
} catch (...) {
}
}
Expand All @@ -195,7 +176,7 @@ inline Task<void> SslConn::Connect (std::string _host, std::string _port) {
}

inline Task<void> SslConn::Reconnect () {
Close ();
SslSocket = nullptr;
TmpData = "";
std::regex _rgx { "(\\d+\\.){3}\\d+" };
std::string _ip = m_host;
Expand All @@ -221,17 +202,6 @@ inline Task<void> SslConn::Reconnect () {
co_await SslSocket->async_handshake (Ssl::stream_base::client, UseAwaitable);
}

inline void SslConn::Close () {
try {
if (SslSocket && SslSocket->next_layer ().is_open ()) {
//SslSocket.next_layer ().shutdown (SocketBase::shutdown_both);
SslSocket->next_layer ().close ();
}
SslSocket = nullptr;
} catch (...) {
}
}

inline Task<void> SslConn::Send (char *_data, size_t _size) {
if (!SslSocket->next_layer ().is_open ())
throw Exception ("Cannot send data to a closed connection.");
Expand All @@ -254,6 +224,7 @@ inline void SslConn::Cancel () {
SslSocket->next_layer ().cancel ();
} catch (...) {
}
SslSocket = nullptr;
}


Expand All @@ -263,16 +234,6 @@ inline SslConn2::SslConn2 (Ssl::stream<Tcp::socket> _sock): SslSocket (std::move
SslSocket.next_layer ().set_option (Tcp::no_delay { true });
}

inline void SslConn2::Close () {
try {
if (SslSocket.next_layer ().is_open ()) {
//SslSocket.next_layer ().shutdown (SocketBase::shutdown_both);
SslSocket.next_layer ().close ();
}
} catch (...) {
}
}

inline Task<void> SslConn2::Send (char *_data, size_t _size) {
if (!SslSocket.next_layer ().is_open ())
throw Exception ("Cannot send data to a closed connection.");
Expand All @@ -287,7 +248,8 @@ inline Task<void> SslConn2::Send (char *_data, size_t _size) {

inline void SslConn2::Cancel () {
try {
SslSocket.next_layer ().cancel ();
if (SslSocket.next_layer ().is_open ())
SslSocket.next_layer ().cancel ();
} catch (...) {
}
}
Expand Down Expand Up @@ -324,7 +286,6 @@ inline Task<std::tuple<std::string, WsType>> WsConn::Recv () {
bool _is_eof = (_tmp [0] & 0x80) != 0;
WsType _type = (WsType) (_tmp [0] & 0xf);
if (_type == WsType::Close) {
Close ();
throw Exception ("Remote send close msg.");
} else if (_type == WsType::Ping) {
co_await _Send (nullptr, 0, WsType::Pong);
Expand Down

0 comments on commit a18ad0b

Please sign in to comment.