lacewing简介
本文是根据lacewing官网的介绍并结合自己的使用经验,挑比较常用到的内容提取归纳而成
lacewing简介
liblacewing是一个跨平台的,为C/C++提供的高级网络开源库,旨在提供一个简明的类,做到能够扩展并且可以平台优化(支持IOCP/EPOLL/KQUEUE等)。
Classes
lacewing::eventpump
tick polls aggressively, suited to a game loop;
start_eventloop starts a simple “block forever” event loop, suited to console applications and daemons;
start_sleepy_ticking enables a threaded callback to request the application call tick as soon as possible, suited to applications with an existing message pump (such as Win32 GUI applications)
lacewing::webserver
webserver aims to provide an embeddable, flexible HTTP server using liblacewing classes as a base.
Features of webserver include:
- Simple hooks for requests, with easy access to GET and POST data
- Support for cookies and sessions
- Support for HTTPS (see host_secure)
- HTTP file upload
webserver uses server as a base for socket communication.
on_get (webserver)
// C++ style
void on_get (lacewing::webserver webserver, lacewing::webserver_request request)
{
/*on_get的响应*/
}webserver->on_get (on_get); /* to register the hook */
// C style
void on_get (lw_ws webserver, lw_ws_request)
{
/*on_get的响应*/
}lw_ws_on_get (webserver, on_get); /* to register the hook */
Description
The on_get hook is triggered for an incoming HTTP GET
request.
This hook marks the beginning of the lifetime of a request object. If enable_manual_finish has been called, the request object will be valid until either request::finish is called OR the on_disconnect hook triggers. Otherwise, the request object is only valid until the hook returns.
The application may configure a response in this hook with functions such as request::header, and request::cookie, and write the response data with stream::write and stream::write_file.
Nothing will actually be written to the socket while the request object is still valid. This ensures that an accurate Content-Length
header can be generated, and that headers can be changed throughout the lifetime of the request.
on_post (webserver)
// C++ style
void on_get (lacewing::webserver webserver, lacewing::webserver_request &request)
{
}webserver->on_post (on_post); /* to register the hook */
// C style
void on_post (lw_ws webserver, lw_ws_request)
{
}lw_ws_on_post (webserver, on_post); /* to register the hook */
Description
The on_post hook is triggered for an incoming HTTP POST
request.
This hook marks the beginning of the lifetime of a request object. If enable_manual_finish has been called, the request object will be valid until either request::finish is called OR the on_disconnect hook triggers. Otherwise, the request object is only valid until the hook returns.
The application may configure a response in this hook with functions such as request::header, and request::cookie, and write the response data with stream::write and stream::write_file.
Nothing will actually be written to the socket while the request object is still valid. This ensures that an accurate Content-Length
header can be generated, and that headers can be changed throughout the lifetime of the request.
Methods
file_exists
// C++ style
lw_bool file_exists (const char * filename)
// C style
lw_bool lw_file_exists (const char * filename)
Description
Returns true
if the file specified by filename
exists, or false
otherwise.
This function will also return false
if the file specified by filename
is a directory. To determine if a directory exists, use path_exists instead.
Parameters
filename
Any absolute or relative filename.
path_exists
// C++ style
lw_bool lacewing::path_exists (const char * filename)
// C style
lw_bool lw_path_exists (const char * filename)
Description
Returns true if the directory specified by filename
exists, or false
otherwise.
This function will also return false
if the path specified by filename
is a file. To determine if a file exists, use file_exists instead.
Parameters
filename
Any absolute or relative filename.
file_size
// C++ style
size_t lacewing::file_size (const char * filename)
// C style
size_t lw_file_size (const char * filename)
Description
Returns the size of a file in bytes, or -1
if the file does not exist.
Parameters
filename
Any absolute or relative filename.
md5
// C++ style
void lacewing::md5 (char * output, const char * input, size_t length)
// C style
void lw_md5 (char * output, const char * input, size_t length)
Description
Returns the MD5 hash of a block of data as the raw hash bytes.
The library itself does not include an MD5 implementation - this function is implemented through either OpenSSL or the Windows Cryptography API.
Parameters
output
A buffer of at least 16 bytes to write the hash to.
input
A pointer to the input data.
length [optional]
The length of the input data, or -1 if the data is a null-terminated string.
md5_hex
// C++ style
void lacewing::md5_hex (char * output, const char * input, size_t length)
// C style
void lw_md5_hex (char * output, const char * input, size_t length)
Description
Returns the MD5 hash of a block of data as a hexadecimal digest.
The library itself does not include an MD5 implementation - this function is implemented through either OpenSSL or the Windows Cryptography API.
Parameters
output
A buffer of at least 33 bytes to write the hash digest to.
input
A pointer to the input data.
length [optional]
The length of the input data, or -1
if the data is a null-terminated string.
random
// C++ style
lw_bool lacewing::random (char * buffer, size_t size)
// C style
lw_bool lw_random (char * buffer, size_t size)
Description
Generates size
bytes of random data, returning true
on success or false
on failure.
Parameters
buffer
A buffer of at least size
bytes to store the generated random data.
size
The number of bytes of random data to generate.
sha1
// C++ style
void lacewing::sha1 (char * output, const char * input, size_t length)
// C style
void lw_sha1 (char * output, const char * input, size_t length)
Description
Returns the SHA1 hash of a block of data as the raw hash bytes.
The library itself does not include an SHA1 implementation - this function is implemented through either OpenSSL or the Windows Cryptography API.
Parameters
output
A buffer of at least 20 bytes to write the hash to.
input
A pointer to the input data.
length [optional]
The length of the input data, or -1
if the data is a null-terminated string.
sha1_hex
// C++ style
void lacewing::sha1 (char * output, const char * input, size_t length)
// C style
void lw_sha1 (char * output, const char * input, size_t length)
Description
Returns the SHA1 hash of a block of data as a hexadecimal digest.
The library itself does not include an SHA1 implementation - this function is implemented through either OpenSSL or the Windows Cryptography API.
Parameters
output
A buffer of at least 41 bytes to write the hash digest to.
input
A pointer to the input data.
length [optional]
The length of the input data, or -1
if the data is a null-terminated string.
temp_path
// C++ style
void lacewing::temp_path (char * buffer)
// C style
void lw_temp_path (char * buffer)
Description
Determines the correct place to store temporary files on the current system.
The path retrieved will have a trailing slash.
Parameters
buffer
A buffer of at least 512 bytes to write the path to.
Examples
simple webserver
Outputs "Hello world"
and lacewing::version in response to any request
#include <lacewing.h>void on_get (lw_ws webserver, lw_ws_req req)
{lw_stream_writef (req, "Hello world from %s", lw_version ());
}int main (int argc, char * argv[])
{lw_pump pump = lw_eventpump_new ();lw_ws webserver = lw_ws_new (pump);lw_ws_on_get (webserver, on_get);lw_ws_host (webserver, 8081);lw_eventpump_start_eventloop (pump);lw_ws_delete (webserver);lw_pump_delete (pump);return 0;
}
ajax
即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),Simple long-poll AJAX example using jQuery
#include <lacewing.h>#include <string.h>
#include <iostream>
#include <list>using namespace std;list <lacewing::webserver_request> waiting;void on_get (lacewing::webserver, lacewing::webserver_request request)
{request->write_file ("ajax.html");request->finish ();
}void on_post (lacewing::webserver, lacewing::webserver_request request)
{if (!strcmp (request->url (), "poll")){waiting.push_back (request);return;}if (!strcmp (request->url (), "message")){const char * message = request->POST ("message");request->writef ("Message from %s: %s\n",request->address ()->tostring (),message);/* Complete all waiting requests with the message */for(list <lacewing::webserver_request>::iterator it= waiting.begin (); it != waiting.end (); ++ it){lacewing::webserver_request waiting_req = *it;waiting_req->write (message);waiting_req->finish ();}waiting.clear ();request->finish ();return;}
}void on_disconnect (lacewing::webserver, lacewing::webserver_request request)
{for (list <lacewing::webserver_request>::iterator it= waiting.begin(); it != waiting.end (); ++ it){if(*it == request){waiting.erase (it);break;}}
}int main (int argc, char * argv[])
{lacewing::eventpump eventpump = lacewing::eventpump_new ();lacewing::webserver webserver = lacewing::webserver_new (eventpump);/* Enabling this means we will have to call request->finish() to complete* a request. Until request->finish() is called, requests will just hang,* which is exactly what we want for long-poll AJAX.*/webserver->enable_manual_finish ();webserver->on_get (on_get);webserver->on_post (on_post);webserver->on_disconnect (on_disconnect);webserver->host (8080); eventpump->start_eventloop();lacewing::webserver_delete (webserver);lacewing::pump_delete (eventpump);return 0;
}
<html><head><title>liblacewing AJAX example</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script><script type="text/javascript">$(document).ready(function(){$('#broadcast').click(function(){var message = $('#message').val();$('#message').val('');$.post('message', { message: message });});(function doPoll(){$.post('poll', function(message){if(!message)return;$('#messages').prepend($('<li></li>').text(message));doPoll();});})();});</script></head><body><label for="message">Type something to broadcast to everyone</label><input type="text" id="message" /><button id="broadcast">Broadcast!</button><br />Messages:<p><ul id="messages"></ul></p></body>
</html>
lacewing简介相关推荐
- etcd 笔记(01)— etcd 简介、特点、应用场景、常用术语、分布式 CAP 理论、分布式原理
1. etcd 简介 etcd 官网定义: A highly-available key value store for shared configuration and service discov ...
- Docker学习(一)-----Docker简介与安装
一.Docker介绍 1.1什么是docker Docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源 Docker可以让开发者打包他们的应用以及依赖包到一个轻量级,可移植 ...
- 【Spring】框架简介
[Spring]框架简介 Spring是什么 Spring是分层的Java SE/EE应用full-stack轻量级开源框架,以IOC(Inverse Of Control:反转控制)和AOP(Asp ...
- TensorRT简介
TensorRT 介绍 引用:https://arleyzhang.github.io/articles/7f4b25ce/ 1 简介 TensorRT是一个高性能的深度学习推理(Inference) ...
- 谷粒商城学习笔记——第一期:项目简介
一.项目简介 1. 项目背景 市面上有5种常见的电商模式 B2B.B2C.C2B.C2C.O2O B2B 模式(Business to Business),是指商家和商家建立的商业关系.如阿里巴巴 B ...
- 通俗易懂的Go协程的引入及GMP模型简介
本文根据Golang深入理解GPM模型加之自己的理解整理而来 Go协程的引入及GMP模型 一.协程的由来 1. 单进程操作系统 2. 多线程/多进程操作系统 3. 引入协程 二.golang对协程的处 ...
- Linux 交叉编译简介
Linux 交叉编译简介 主机,目标,交叉编译器 主机与目标 编译器是将源代码转换为可执行代码的程序.像所有程序一样,编译器运行在特定类型的计算机上,输出的新程序也运行在特定类型的计算机上. 运行编译 ...
- TVM Operator Inventory (TOPI)简介
TOPI简介 这是 TVM Operator Inventory (TOPI) 的介绍.TOPI 提供了比 TVM 具有更高抽象的 numpy 风格的,通用操作和调度.TOPI 如何在 TVM 中,编 ...
- 计算机视觉系列最新论文(附简介)
计算机视觉系列最新论文(附简介) 目标检测 1. 综述:深度域适应目标检测标题:Deep Domain Adaptive Object Detection: a Survey作者:Wanyi Li, ...
最新文章
- VC manifest
- iOS 改变字符串中数字的颜色
- php删除所以文件,php如何删除所有文件
- datagrid分页传递参数_四类数据库分页实现方案总结之Mysql分页实现
- VMware中CentOS7网卡无法启动故障
- [BTS]6912,5641,5773,5410错误处理!
- GridView RowDeleting 动态添加行,删除行记录 不删除数据库中记录
- LGB 的 .feature_importance() 函数
- Bootstrap3 滚动监听插件的方法
- oracle.sql.clob 报错,oracle11g java导出excel报错oracle.sql.CLOB@xxxx问题
- Android IPC(二)Messenger实现跨进程双向通信
- Swift的函数嵌套和返回内部函数
- linux centos挂载数据盘教程
- 动态MAC地址和静态MAC地址
- 做个grub的U盘启动盘,即将grub安装到U盘上面。
- 计算机如何制作表格基础,计算机基础教程(Word表格制作)
- 下载视频并保存至手机相册
- ZYNQ学习之路9.USB总线学习(二)
- 什么是javaweb开发?
- 第8节_数据筛选过滤