| Description | The tcp_reset_window function resets the TCP window size to a default value defined with TCP_DEF_WINSIZE macro. The argument socket specifies the handle of the socket to reset the window size of.
This function can only be used with sockets that have a TCP flow control enabled. To enable a TCP flow control for the socket, tcp_get_socket function has to be called with TCP_TYPE_FLOW_CTRL attribute set. This attribute enables using a Sliding Window protocol.
In Flow Control mode, each received data packet reduces the receiving Window Size by the number of data bytes received in the packet. Soon the window size becomes very small or 0, remote host stops sending data and waits for a window update. As soon as the received data is processed, we can call a tcp_reset_window function to reopen the receiver window for further incoming data.
Depending on the context, from where this function was called, it does the following actions: - resets the window size of the socket and returns if called from the callback function. The window size is actually changed in the acknowledge packet generated by TCPnet when the callback function returns.
- resets the window size and sends out a Window Update packet if called from the other part of the user application.
- does nothing if the socket is not in TCP_STATE_CONNECT state and the TCP_TYPE_FLOW_CTRL attribute not set for the socket.
The tcp_reset_window function is in the RL-TCPnet library. The prototype is defined in rtl.h. |
| Example |
#include <rtl.h>
U8 tcp_soc;
U8 buf[TCP_DEF_WINSIZE];
U32 head, tail;
void send_to_uart (void) {
/* Send the data received from TCP to UART. */
if (uart_busy () || head == tail) {
/* Do nothing if UART is busy or when 'buf' is empty. */
return;
}
send_uart (buf[tail++]);
if (tail == head) {
/* The 'buf' is empty, all bytes sent out to UART. *.
tail = 0;
head = 0;
tcp_reset_window (tcp_soc);
}
}
U16 tcp_callback (U8 soc, U8 event, U8 *ptr, U16 par) {
/* This function is called on TCP event */
..
switch (event) {
case TCP_EVT_CONREQ:
/* Remote host is trying to connect to our TCP socket. */
/* 'ptr' points to Remote IP, 'par' holds the remote port. */
/* Return 1 to accept connection, or 0 to reject connection */
return (1);
case TCP_EVT_ABORT:
/* Connection was aborted */
tcp_soc = 0;
break;
case TCP_EVT_CONNECT:
/* Socket is connected to remote peer. */
tcp_soc = soc;
break;
case TCP_EVT_CLOSE:
/* Connection has been closed */
tcp_soc = 0;
break;
case TCP_EVT_ACK:
/* Our sent data has been acknowledged by remote peer */
break;
case TCP_EVT_DATA:
/* TCP data frame has been received, 'ptr' points to data */
/* Data length is 'par' bytes */
memcpy (&buf[head], ptr, par);
head += par;
break;
}
return (0);
}
void main (void) {
init ();
/* Initialize the TcpNet */
init_TcpNet ();
tcp_soc = tcp_get_socket (TCP_TYPE_SERVER | TCP_TYPE_FLOW_CTRL,
0, 30, tcp_callback);
if (tcp_soc != 0) {
/* Start listening on TCP port 8080 */
tcp_listen (tcp_soc, 8080);
}
head = 0;
tail = 0;
while (1);
/* Run main TcpNet 'thread' */
main_TcpNet ();
send_to_uart ();
..
}
}
|