ENet接口说明

ENet教程翻译

  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.org

ENet'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.


  ENet的目的是提供一个相对薄的,简单的以及稳健的位于UDP(User Datagram Protocol)顶部的网络通信层。其提供的主要功能是可选的可靠按顺序的传送数据包。
  ENet忽略了某些高级网络的功能,诸如身份验证, lobbying, server discovery,加密或者其他类似的特别面向应用的功能。这样,这个库灵活,轻便,可移植,易于嵌入。

Tutorial

Initialization

Creating an ENet server

Creating an ENet client

Managing an ENet host

Sending a packet to an ENet peer

Disconnecting an ENet peer

Connecting to an ENet host

 

教程

初始化

创建一个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 */ );
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于GD32F450芯片的Enet接口配置示例代码,供参考: ```c #include "gd32f4xx.h" #include <stdio.h> #include <string.h> #define ENET_BUF_SIZE 1536 #define ENET_PHY_ADDRESS 0x01 #define ENET_PHY_STATUS 0x01 #define ENET_RX_DMA_CHANNEL DMA_CH1 #define ENET_TX_DMA_CHANNEL DMA_CH2 uint8_t rx_buffer[ENET_BUF_SIZE]; uint8_t tx_buffer[ENET_BUF_SIZE]; /* ENET MAC address */ uint8_t macaddr[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}; /* ENET IP address */ uint8_t ipaddr[4] = {192, 168, 0, 100}; /* ENET netmask */ uint8_t netmask[4] = {255, 255, 255, 0}; /* ENET gateway address */ uint8_t gateway[4] = {192, 168, 0, 1}; /* ENET RX packet struct */ struct enet_rx_packet { uint32_t status_info; uint32_t length; uint8_t data[ENET_BUF_SIZE]; }; /* ENET TX packet struct */ struct enet_tx_packet { uint32_t length; uint8_t data[ENET_BUF_SIZE]; }; /* ENET init */ void enet_init(void) { /* Enable GPIO clock */ rcu_periph_clock_enable(RCU_GPIOA); /* Configure PA1 as ENET_RXD0 */ gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_1); /* Configure PA2 as ENET_MDIO */ gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2); /* Configure PA7 as ENET_RXD1 */ gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_7); /* Configure PB0 as ENET_MDC */ gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0); /* Configure PB10 as ENET_TX_EN */ gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10); /* Configure PB11 as ENET_TXD0 */ gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11); /* Configure PB12 as ENET_TXD1 */ gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12); /* Enable ENET clock */ rcu_periph_clock_enable(RCU_ENET); /* Reset ENET */ enet_deinit(); /* Enable ENET MAC */ enet_mac_enable(); /* Set ENET MAC address */ enet_mac_address_set(macaddr); /* Enable ENET RX */ enet_rx_enable(); /* Enable ENET TX */ enet_tx_enable(); /* Set ENET IP address */ enet_ip_address_set(ipaddr); /* Set ENET netmask */ enet_netmask_set(netmask); /* Set ENET gateway address */ enet_gateway_address_set(gateway); /* Enable ENET interrupt */ enet_interrupt_enable(ENET_INT_TIE | ENET_INT_RIE | ENET_INT_RBUIE); /* Enable DMA clock */ rcu_periph_clock_enable(RCU_DMA); /* Enable ENET RX DMA */ enet_dma_rx_enable(); /* Enable ENET TX DMA */ enet_dma_tx_enable(); } /* ENET RX DMA interrupt handler */ void enet_rx_dma_irq_handler(void) { /* Get ENET RX DMA channel interrupt flag */ if (dma_interrupt_flag_get(DMA1, ENET_RX_DMA_CHANNEL, DMA_INT_FLAG_FTF)) { /* Clear ENET RX DMA channel interrupt flag */ dma_interrupt_flag_clear(DMA1, ENET_RX_DMA_CHANNEL, DMA_INT_FLAG_FTF); /* Get ENET RX packet struct */ struct enet_rx_packet *rx_packet = (struct enet_rx_packet *)rx_buffer; /* Process ENET RX packet */ /* ... */ /* Allocate new buffer for ENET RX DMA */ dma_memory_to_memory_disable(DMA1, ENET_RX_DMA_CHANNEL); dma_transfer_size_config(DMA1, ENET_RX_DMA_CHANNEL, ENET_BUF_SIZE); dma_memory_address_config(DMA1, ENET_RX_DMA_CHANNEL, (uint32_t)rx_buffer); dma_periph_address_config(DMA1, ENET_RX_DMA_CHANNEL, (uint32_t)&ENET->DR); dma_memory_to_memory_enable(DMA1, ENET_RX_DMA_CHANNEL); } } /* ENET TX DMA interrupt handler */ void enet_tx_dma_irq_handler(void) { /* Get ENET TX DMA channel interrupt flag */ if (dma_interrupt_flag_get(DMA1, ENET_TX_DMA_CHANNEL, DMA_INT_FLAG_FTF)) { /* Clear ENET TX DMA channel interrupt flag */ dma_interrupt_flag_clear(DMA1, ENET_TX_DMA_CHANNEL, DMA_INT_FLAG_FTF); /* Process ENET TX packet */ /* ... */ } } int main(void) { /* Initialize ENET */ enet_init(); /* Main loop */ while (1) { /* ... */ } return 0; } ``` 上述代码中使用了DMA来进行ENET数据包的传输,同时也实现了ENET的中断处理函数。需要注意的是,ENET的配置需要根据具体芯片和网络环境进行调整,上述代码仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值