ENet官网:http://enet.bespin.org/
该文原文出处:http://enet.bespin.org/Tutorial.html
本篇文章只是翻译,其中的源码也是官方的。而且源码是ENet1.3.0版的。后边的文章会有一个示例程序的源码。
这边有一个名词peer要理解。这个peer就是P2P(peer-to-peer)中的peer,个人感觉就是指对等网络中的一个节点。
对照翻译如下:
enet
http://enet.bespin.orgENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol). The primary feature it provides is optional reliable, in-order delivery of packets.
ENet omits certain higher level networking features such as authentication, lobbying, server discovery, encryption, or other similar tasks that are particularly application specific so that the library remains flexible, portable, and easily embeddable.
Tutorial
InitializationSending a packet to an ENet peer
教程
初始化
创建一个ENet的服务端
创建一个ENet的客户端
管理一个ENet的host(主机端)
发送一个packet给一个ENet的peer
断开一个ENet peer的连接
连接到一个ENet的host(主机端)
Initialization
You should include the file < enet/enet.h> when using ENet. Do not include < enet.h> without the directory prefix, as this may cause file name conflicts on some systems.Before using ENet, you must call enet_initialize() to initialize the library. Upon program exit, you should call enet_deinitialize() so that the library may clean up any used resources.
初始化
当使用ENet的时候,你应该包含头文件<enet/enet.h>。不要只包含<enet.h>而没有文件夹前缀,因为在某些系统,这可能命名的冲突。
在使用ENet之前,你必须调用enet_initialize()以初始化链接库。当程序结束时你应该调用enet_deinitiakize(),这样链接库就可以清掉所有的用过的资源。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <enet/enet.h>
int
main (
int
argc,
char
** argv)
{
if
(enet_initialize () != 0)
{
fprintf
(stderr,
"An error occurred while initializing ENet.\n"
);
return
EXIT_FAILURE;
}
atexit
(enet_deinitialize);
...
...
...
}
|
Creating an ENet server
Servers in ENet are constructed with enet_host_create(). You must specify an address on which to receive data and new connections, as well as the maximum allowable numbers of connected peers. You may optionally specify the incoming and outgoing bandwidth of the server in bytes per second so that ENet may try to statically manage bandwidth resources among connected peers in addition to its dynamic throttling algorithm; specifying 0 for these two options will cause ENet to rely entirely upon its dynamic throttling algorithm to manage bandwidth.When done with a host, the host may be destroyed with enet_host_destroy(). All connected peers to the host will be reset, and the resources used by the host will be freed.
创建一个ENet的Server(服务端)
ENet中用enet_host_create()构造Server。你必须指定一个地址用来接收数据和新的连接请求,最大的允许连接peer的数量。你也可以指定(可选)server传入和传出的带宽(bytes每秒),这样ENet除了会使用它的动态节流算法外,还会试着静态的处理连接的peer之间带宽资源;如果将这两个参数指定为0,ENet将会完全依赖于它的动态节流算法来处理带宽。
当要结束一个host(主机端口?)时,需要调用enet_host_destroy()来析构host。所有连接到该host的peers会被重置,所用被该host使用的资源将被释放。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
ENetAddress address;
ENetHost * server;
/* Bind the server to the default localhost. */
/* A specific host address can be specified by */
/* enet_address_set_host (& address, "x.x.x.x"); */
address.host = ENET_HOST_ANY;
/* Bind the server to port 1234. */
address.port = 1234;
server = enet_host_create (& address
/* the address to bind the server host to */
,
32
/* allow up to 32 clients and/or outgoing connections */
,
2
/* allow up to 2 channels to be used, 0 and 1 */
,
0
/* assume any amount of incoming bandwidth */
,
0
/* assume any amount of outgoing bandwidth */
);
|