下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

最新资讯
简化任何设备的身份验证云连接。
利用 CoAP 设计节能型云连接 IoT 解决方案。
11.0.0 版 FreeRTOS 内核简介:
FreeRTOS 路线图和代码贡献流程。
使用 FreeRTOS 实现 OPC-UA over TSN。

使用 TCP 套接字(零拷贝接口)接收数据
FreeRTOS-Plus-TCP 联网教程的一部分

请参阅 接收 UDP 数据(零拷贝接口),了解关于使用 UDP 零拷贝接口接收数据的内容。

FreeRTOS_recv() 用于从 TCP 套接字接收数据。FreeRTOS_recv() 无法调用,直到 TCP 套接字 创建、 配置、绑定并连接到远程套接字。

下述源代码演示了如何通过 FreeRTOS_recv(), 使用零拷贝接口将收到的数据放入缓冲区。在本例中,假设套接字已创建 并连接。


#define BUFFER_SIZE 512

static void prvEchoClientRxTask( void *pvParameters )
{
Socket_t xSocket;
static char cRxedData[ BUFFER_SIZE ];
BaseType_t lBytesReceived;

/* It is assumed the socket has already been created and connected before

being passed into this RTOS task using the RTOS task's parameter. */

xSocket = ( Socket_t ) pvParameters;

for( ;; )
{

uint8_t *pucZeroCopyRxBuffPtr = NULL;

/* Receive data from the socket. xFlags has the zero copy bit set

(FREERTOS_ZERO_COPY) indicating to the stack that a reference to the

received data should be passed out to this RTOS task using the second

parameter to the FreeRTOS_recv() call. When this is done the

IP stack is no longer responsible for releasing the buffer, and

the RTOS task must return the buffer to the stack when it is no longer

needed using the FreeRTOS_ReleaseTCPPayloadBuffer() API. */

lBytesReceived = FreeRTOS_recv( xSocket, /* The socket being received from. */
&pucZeroCopyRxBuffPtr, /* While using FREERTOS_ZERO_COPY flag,

pvBuffer is taken as a double pointer which will

be updated with pointer to TCP RX stream buffer. */

ipconfigTCP_MSS, /* The size of the buffer provided to

receive the data. */

FREERTOS_ZERO_COPY ); /* Use FREERTOS_ZERO_COPY flag to enable

zero copy. */

if( pucZeroCopyRxBuffPtr != NULL )
{
/* Copy the data to application buffer if its required to be processed later */
memcpy( &cRxedData, pucZeroCopyRxBuffPtr, lBytesReceived );

/* Release the memory that was previously obtained by calling FreeRTOS_recv()

with the flag 'FREERTOS_ZERO_COPY' */

FreeRTOS_ReleaseTCPPayloadBuffer( xSocket, pucZeroCopyRxBuffPtr, lBytesReceived );
}

if( lBytesReceived > 0 )
{
/* Data was received, process it here. */
prvProcessData( cRxedData, lBytesReceived );
}
else if( lBytesReceived == 0 )
{
/* No data was received, but FreeRTOS_recv() did not return an error.

Timeout? */

}
else
{
/* Error (maybe the connected socket already shut down the socket?).

Attempt graceful shutdown. */

FreeRTOS_shutdown( xSocket, FREERTOS_SHUT_RDWR );
break;
}
}

/* The RTOS task will get here if an error is received on a read. Ensure the

socket has shut down (indicated by FreeRTOS_recv() returning a -pdFREERTOS_ERRNO_EINVAL

error before closing the socket). */


while( FreeRTOS_recv( xSocket, pcBufferToTransmit, xTotalLengthToSend, 0 ) >= 0 )
{
/* Wait for shutdown to complete. If a receive block time is used then

this delay will not be necessary as FreeRTOS_recv() will place the RTOS task

into the Blocked state anyway. */

vTaskDelay( pdTICKS_TO_MS( 250 ) );

/* Note - real applications should implement a timeout here, not just

loop forever. */

}

/* Shutdown is complete and the socket can be safely closed. */
FreeRTOS_closesocket( xSocket );

/* Must not drop off the end of the RTOS task - delete the RTOS task. */
xTaskDelete( NULL );
}
使用 FreeRTOS_recv() 的示例


<< 返回 RTOS TCP 网络教程索引

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.