Skip to content

Commit

Permalink
add document content
Browse files Browse the repository at this point in the history
  • Loading branch information
fawdlstty committed Aug 5, 2022
1 parent eab20d9 commit a329625
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 126 deletions.
46 changes: 35 additions & 11 deletions docs/en_us/1_HttpClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,44 @@ fv::Response _r = co_await fv::Get ("https://t.cn", fv::referer ("https://t.cn")
fv::Response _r = co_await fv::Get ("https://t.cn", fv::user_agent ("Mozilla/4.0 Chrome 2333"));
```

<!--
## HTTP pipeline

HTTP pipeline is a solution to economize link resources. After initiating and receiving a return from the server, the link can be retained to proceed to the next request. This method economize system resource overhead and TCP and SSL connection time for subsequent requests.
Libfv will maintain a link pool internally, providing reuse of requests for the same service address (same schema, domain name, port) without manual intervention.

Here is how to use HTTP pipeline:
## Example

```cpp
// Creates a session
fv::Session _sess {};
// Multiple requests for the same TCP connect. If the schema, host, and port have not changed then reuse the link
fv::Response _r = co_await _sess.Get ("https://t.cn");
_r = co_await _sess.Get ("https://t.cn");
_r = co_await _sess.Get ("https://t.cn");
#ifdef _MSC_VER
# define _WIN32_WINNT 0x0601
# pragma warning (disable: 4068)
# pragma comment (lib, "Crypt32.lib")
//# ifdef _RESUMABLE_FUNCTIONS_SUPPORTED
//# undef _RESUMABLE_FUNCTIONS_SUPPORTED
//# endif
//# ifndef __cpp_lib_coroutine
//# define __cpp_lib_coroutine
//# endif
#endif

#include <iostream>
#include <string>
#include <fv/fv.h>



Task<void> test_client () {
fv::Response _r = co_await fv::Get ("https://www.fawdlstty.com");
std::cout << "received content length: " << _r.Content.size () << '\n';
std::cout << "press any key to exit\n";
char ch;
std::cin >> ch;
fv::Tasks::Stop ();
}

int main () {
fv::Tasks::Init ();
fv::Tasks::RunAsync (test_client);
fv::Tasks::Run ();
return 0;
}
```
-->
55 changes: 55 additions & 0 deletions docs/en_us/2_WebsocketClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,58 @@ 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.

## Example

```cpp
#ifdef _MSC_VER
# define _WIN32_WINNT 0x0601
# pragma warning (disable: 4068)
# pragma comment (lib, "Crypt32.lib")
//# ifdef _RESUMABLE_FUNCTIONS_SUPPORTED
//# undef _RESUMABLE_FUNCTIONS_SUPPORTED
//# endif
//# ifndef __cpp_lib_coroutine
//# define __cpp_lib_coroutine
//# endif
#endif

#include <iostream>
#include <string>
#include <fv/fv.h>



Task<void> test_client () {
try {
std::shared_ptr<fv::WsConn> _conn = co_await fv::ConnectWS ("ws://82.157.123.54:9010/ajaxchattest", fv::header ("Origin", "http://coolaf.com"));
while (true) {
std::cout << "press any char to continue (q to exit)\n";
char ch;
std::cin >> ch;
if (ch == 'q')
break;

std::string _str = "hello";
std::cout << "send" << std::endl;
co_await _conn->SendText (_str.data (), _str.size ());

std::cout << "recv: ";
auto [_data, _type] = co_await _conn->Recv ();
std::cout << _data << std::endl;
}
} catch (std::exception &_e) {
std::cout << "catch exception: " << _e.what () << std::endl;
} catch (...) {
std::cout << "catch exception" << std::endl;
}
fv::Tasks::Stop ();
}

int main () {
fv::Tasks::Init ();
fv::Tasks::RunAsync (test_client);
fv::Tasks::Run ();
return 0;
}
```
4 changes: 4 additions & 0 deletions docs/en_us/3_TcpSslClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ co_await _conn->Send (_str.data (), _str.size ());
## Close connection
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.
## Example
TODO
38 changes: 38 additions & 0 deletions docs/en_us/4_HttpServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,41 @@ _server.OnUnhandled ([] (fv::Request &_req) -> Task<fv::Response> {
```cpp
co_await _server.Run (8080);
```

## Example

```cpp
#ifdef _MSC_VER
# define _WIN32_WINNT 0x0601
# pragma warning (disable: 4068)
# pragma comment (lib, "Crypt32.lib")
//# ifdef _RESUMABLE_FUNCTIONS_SUPPORTED
//# undef _RESUMABLE_FUNCTIONS_SUPPORTED
//# endif
//# ifndef __cpp_lib_coroutine
//# define __cpp_lib_coroutine
//# endif
#endif

#include <iostream>
#include <string>
#include <fv/fv.h>



Task<void> test_server () {
fv::HttpServer _server {};
_server.SetHttpHandler ("/hello", [] (fv::Request &_req) -> Task<fv::Response> {
co_return fv::Response::FromText ("hello world");
});
std::cout << "You can access from browser: http://127.0.0.1:8080/hello\n";
co_await _server.Run (8080);
}

int main () {
fv::Tasks::Init ();
fv::Tasks::RunMainAsync (test_server);
fv::Tasks::Run ();
return 0;
}
```
4 changes: 4 additions & 0 deletions docs/en_us/5_TcpSslServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ size_t _count = co_await m_tcpserver.BroadcastData (_data.data (), _data.size ()
```cpp
co_await _server.Run (8080);
```

## Example

TODO
4 changes: 4 additions & 0 deletions docs/en_us/6_Other.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ Get the count of available resources:
```cpp
size_t _count = _mtx.GetResCount ();
```

## Example

TODO
46 changes: 35 additions & 11 deletions docs/zh_hans/1_HttpClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,44 @@ fv::Response _r = co_await fv::Get ("https://t.cn", fv::referer ("https://t.cn")
fv::Response _r = co_await fv::Get ("https://t.cn", fv::user_agent ("Mozilla/4.0 Chrome 2333"));
```

<!--
## HTTP pipeline

HTTP pipeline 是一种节省链接资源的方案。在发起并获取服务端返回之后,这个链接可以保留,继续下一个请求。这种方式对后续请求会节省系统资源开销以及 TCP 及 SSL 链接耗时。
libfv内部将维护一个链接池,提供对相同服务地址(协议、域名、端口均相同)的请求的复用,无需手工干预

下面给出 HTTP pipeline 使用方式:
## 示例

```cpp
// 创建会话
fv::Session _sess {};
// 同一会话(TCP 链接)多次请求。协议、域名及端口没有变化则复用链接
fv::Response _r = co_await _sess.Get ("https://t.cn");
_r = co_await _sess.Get ("https://t.cn");
_r = co_await _sess.Get ("https://t.cn");
#ifdef _MSC_VER
# define _WIN32_WINNT 0x0601
# pragma warning (disable: 4068)
# pragma comment (lib, "Crypt32.lib")
//# ifdef _RESUMABLE_FUNCTIONS_SUPPORTED
//# undef _RESUMABLE_FUNCTIONS_SUPPORTED
//# endif
//# ifndef __cpp_lib_coroutine
//# define __cpp_lib_coroutine
//# endif
#endif

#include <iostream>
#include <string>
#include <fv/fv.h>



Task<void> test_client () {
fv::Response _r = co_await fv::Get ("https://www.fawdlstty.com");
std::cout << "received content length: " << _r.Content.size () << '\n';
std::cout << "press any key to exit\n";
char ch;
std::cin >> ch;
fv::Tasks::Stop ();
}

int main () {
fv::Tasks::Init ();
fv::Tasks::RunAsync (test_client);
fv::Tasks::Run ();
return 0;
}
```
-->
55 changes: 55 additions & 0 deletions docs/zh_hans/2_WebsocketClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,58 @@ co_await _conn->Close ();
```

除了主动关闭外,只要连接对象不被代码所引用,受智能指针自动释放,也会自动关闭链接。

## 示例

```cpp
#ifdef _MSC_VER
# define _WIN32_WINNT 0x0601
# pragma warning (disable: 4068)
# pragma comment (lib, "Crypt32.lib")
//# ifdef _RESUMABLE_FUNCTIONS_SUPPORTED
//# undef _RESUMABLE_FUNCTIONS_SUPPORTED
//# endif
//# ifndef __cpp_lib_coroutine
//# define __cpp_lib_coroutine
//# endif
#endif

#include <iostream>
#include <string>
#include <fv/fv.h>



Task<void> test_client () {
try {
std::shared_ptr<fv::WsConn> _conn = co_await fv::ConnectWS ("ws://82.157.123.54:9010/ajaxchattest", fv::header ("Origin", "http://coolaf.com"));
while (true) {
std::cout << "press any char to continue (q to exit)\n";
char ch;
std::cin >> ch;
if (ch == 'q')
break;

std::string _str = "hello";
std::cout << "send" << std::endl;
co_await _conn->SendText (_str.data (), _str.size ());

std::cout << "recv: ";
auto [_data, _type] = co_await _conn->Recv ();
std::cout << _data << std::endl;
}
} catch (std::exception &_e) {
std::cout << "catch exception: " << _e.what () << std::endl;
} catch (...) {
std::cout << "catch exception" << std::endl;
}
fv::Tasks::Stop ();
}

int main () {
fv::Tasks::Init ();
fv::Tasks::RunAsync (test_client);
fv::Tasks::Run ();
return 0;
}
```
4 changes: 4 additions & 0 deletions docs/zh_hans/3_TcpSslClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ co_await _conn->Send (_str.data (), _str.size ());
## 关闭连接
只要连接对象不被代码所引用,受智能指针自动释放,就会自动关闭链接。
## 示例
TODO
38 changes: 38 additions & 0 deletions docs/zh_hans/4_HttpServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,41 @@ _server.OnUnhandled ([] (fv::Request &_req) -> Task<fv::Response> {
```cpp
co_await _server.Run (8080);
```

## 示例

```cpp
#ifdef _MSC_VER
# define _WIN32_WINNT 0x0601
# pragma warning (disable: 4068)
# pragma comment (lib, "Crypt32.lib")
//# ifdef _RESUMABLE_FUNCTIONS_SUPPORTED
//# undef _RESUMABLE_FUNCTIONS_SUPPORTED
//# endif
//# ifndef __cpp_lib_coroutine
//# define __cpp_lib_coroutine
//# endif
#endif

#include <iostream>
#include <string>
#include <fv/fv.h>



Task<void> test_server () {
fv::HttpServer _server {};
_server.SetHttpHandler ("/hello", [] (fv::Request &_req) -> Task<fv::Response> {
co_return fv::Response::FromText ("hello world");
});
std::cout << "You can access from browser: http://127.0.0.1:8080/hello\n";
co_await _server.Run (8080);
}

int main () {
fv::Tasks::Init ();
fv::Tasks::RunMainAsync (test_server);
fv::Tasks::Run ();
return 0;
}
```
4 changes: 4 additions & 0 deletions docs/zh_hans/5_TcpSslServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ size_t _count = co_await m_tcpserver.BroadcastData (_data.data (), _data.size ()
```cpp
co_await _server.Run (8080);
```

## 示例

TODO
4 changes: 4 additions & 0 deletions docs/zh_hans/6_Other.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ _mtx.Release ();
```cpp
size_t _count = _mtx.GetResCount ();
```

## 示例

TODO
Loading

0 comments on commit a329625

Please sign in to comment.