Skip to content

Commit

Permalink
update doc & clear connection on error for #24
Browse files Browse the repository at this point in the history
  • Loading branch information
fawdlstty committed Jun 9, 2022
1 parent be6a296 commit 77eb492
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 47 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
- Cancellation
- Transfer progress callback
- HTTP breakpoint resume (server/client)
- HTTP pipeline multi request support
16 changes: 2 additions & 14 deletions docs/en_us/0_Startup.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@

## Configure vcpkg

libfv depends on the VCPKG environment. Therefore, you need to configure it before using libfv.
First install `libfv` through `vcpkg`

```
# Installing dependency packages
vcpkg install asio fmt gzip-hpp nlohmann-json openssl zlib
# `asio` can be changed to `boost-asio`
vcpkg install fawdlstty-libfv
```

## Configure libfv

First, clone the libfv repository

```
git clone [email protected]:fawdlstty/libfv.git
```

Then add the include folder in the repository to your project's header search path. If this step does not work, copy the `include/fv` folder from the repository into your project and reference it later via `#include "fv/fv.hpp"`.

## Initialize

```cpp
Expand Down
16 changes: 2 additions & 14 deletions docs/zh_hans/0_Startup.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@

## 配置 `vcpkg` 环境

libfv 依赖 vcpkg 环境,因此在使用 libfv 之前需配置
首先通过 `vcpkg` 安装 `libfv`

```
# 安装依赖包
vcpkg install asio fmt gzip-hpp nlohmann-json openssl zlib
# `asio` 可换成 `boost-asio`
vcpkg install fawdlstty-libfv
```

## 配置 libfv 环境

首先克隆 libfv 仓库

```
git clone [email protected]:fawdlstty/libfv.git
```

然后将仓库内 include 文件夹添加到你的项目的头文件搜索路径。如果这步不会,那么将仓库内 `include/fv` 文件夹拷贝到你的项目内,后续通过 `#include "fv/fv.hpp"` 引用即可。

## 初始化

```cpp
Expand Down
6 changes: 3 additions & 3 deletions include/fv/conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct TcpConn2: public IConn2 {
bool IsConnect () override { return Socket.is_open (); }
void Close () override;
Task<void> Send (char *_data, size_t _size) override;
void Cancel () override { Socket.cancel (); }
void Cancel () override;

protected:
Task<size_t> RecvImpl (char *_data, size_t _size) override;
Expand All @@ -88,7 +88,7 @@ struct SslConn: public IConn {
virtual ~SslConn () { Cancel (); Close (); }
Task<void> Connect (std::string _host, std::string _port) override;
Task<void> Reconnect () override;
bool IsConnect () override { return SslSocket->next_layer ().is_open (); }
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 @@ -109,7 +109,7 @@ struct SslConn2: public IConn2 {
bool IsConnect () override { return SslSocket.next_layer ().is_open (); }
void Close () override;
Task<void> Send (char *_data, size_t _size) override;
void Cancel () override { SslSocket.next_layer ().cancel (); }
void Cancel () override;

protected:
Task<size_t> RecvImpl (char *_data, size_t _size) override;
Expand Down
46 changes: 37 additions & 9 deletions include/fv/conn_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ inline Task<void> TcpConn::Reconnect () {
}

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

inline Task<void> TcpConn::Send (char *_data, size_t _size) {
Expand All @@ -144,7 +147,11 @@ inline Task<size_t> TcpConn::RecvImpl (char *_data, size_t _size) {
}

inline void TcpConn::Cancel () {
Socket->cancel ();
try {
if (Socket)
Socket->cancel ();
} catch (...) {
}
}


Expand Down Expand Up @@ -173,6 +180,13 @@ inline Task<void> TcpConn2::Send (char *_data, size_t _size) {
}
}

inline void TcpConn2::Cancel () {
try {
Socket.cancel ();
} catch (...) {
}
}

inline Task<size_t> TcpConn2::RecvImpl (char *_data, size_t _size) {
if (!Socket.is_open ())
co_return 0;
Expand Down Expand Up @@ -251,7 +265,11 @@ inline Task<size_t> SslConn::RecvImpl (char *_data, size_t _size) {
}

inline void SslConn::Cancel () {
SslSocket->next_layer ().cancel ();
try {
if (SslSocket)
SslSocket->next_layer ().cancel ();
} catch (...) {
}
}


Expand All @@ -262,9 +280,12 @@ inline SslConn2::SslConn2 (Ssl::stream<Tcp::socket> _sock): SslSocket (std::move
}

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

Expand All @@ -280,6 +301,13 @@ inline Task<void> SslConn2::Send (char *_data, size_t _size) {
}
}

inline void SslConn2::Cancel () {
try {
SslSocket->next_layer ().cancel ();
} catch (...) {
}
}

inline Task<size_t> SslConn2::RecvImpl (char *_data, size_t _size) {
if (!SslSocket.next_layer ().is_open ())
co_return 0;
Expand Down
19 changes: 12 additions & 7 deletions include/fv/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct Session {

Task<Response> DoMethod (Request _r) {
co_await SessMtx.Lock ();
bool _is_lock = true;
try {
auto [_schema, _host, _port, _path] = _parse_url (_r.Url);
std::string _conn_flag = fmt::format ("{}://{}:{}", _schema, _host, _port);
Expand Down Expand Up @@ -100,16 +101,20 @@ struct Session {
}
co_await Conn->Reconnect ();
}
} catch (...) {

SessMtx.Unlock ();
_is_lock = false;

// recv
Response _ret = co_await Response::GetFromConn (Conn);
//_timer.Cancel ();
co_return _ret;
} catch (...) {
if (_is_lock)
SessMtx.Unlock ();
Conn = nullptr;
throw;
}
SessMtx.Unlock ();

// recv
Response _ret = co_await Response::GetFromConn (Conn);
//_timer.Cancel ();
co_return _ret;
}

Task<Response> Head (std::string _url) {
Expand Down

0 comments on commit 77eb492

Please sign in to comment.