Why we need "Write Offset" variable in the buffer structure?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why we need "Write Offset" variable in the buffer structure?

1,067 Views
omkardixit
Contributor III

Currently, I'm working on the TWR-K70F120M in which I'm trying to understand the "httpsrv" example from the following path->(E:\Freescale\Freescale_MQX_4_1\rtcs\example\httpsrv\buil \uv4\httpsrv_twrk70f120m)

When I put the IP address 192.168.1.202 in the browser I observed that some data is gettings changed automatically so, I wanted to manipulate the data that's why  when I started debugging one thing i noticed is after the following line,

length = send(session->sock, session->buffer.data, session->buffer.offset, 0);

Data is getting changed, so while understanding I came across the following structure,

typedef struct httpsrv_buffer
{
uint32_t offset; /* Write offset */
char* data; /* Buffer data */
}HTTPSRV_BUFF_STRUCT;

So here I tried to find out in the RTCS documents but it was not there. Can anyone please help me to understand why there is offset used? why do we need a "write offset" variable?

Regards

Omkar Dixit

 

Labels (1)
0 Kudos
1 Reply

1,000 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi

 

From API httpsrv_write, you can see the offset usage.

 

int32_t httpsrv_write(HTTPSRV_SESSION_STRUCT *session, char *src, int32_t length)
{
uint32_t space = HTTPSRV_SES_BUF_SIZE_PRV - session->buffer.offset;
int32_t retval = length;
uint32_t n_send;

RTCS_ASSERT(session != NULL);
RTCS_ASSERT(src != NULL);

/* User buffer is bigger than session buffer - send user data directly */
if (length > HTTPSRV_SES_BUF_SIZE_PRV)
{
/* If there are some data already buffered send them to client first */
if (httpsrv_ses_flush(session) == RTCS_ERROR)
{
return(RTCS_ERROR);
}
else
{
return(httpsrv_send(session, src, length, 0));
}
}

/* No space in buffer - make some */
if
(
(space < length) &&
(httpsrv_ses_flush(session) == RTCS_ERROR)
)
{
return(RTCS_ERROR);
}

/* Now we can save user data to buffer and eventually send them to client */
space = HTTPSRV_SES_BUF_SIZE_PRV - session->buffer.offset;
n_send = (space < length) ? space : length;
_mem_copy(src, session->buffer.data+session->buffer.offset, n_send);
session->buffer.offset += n_send;

if (session->buffer.offset >= HTTPSRV_SES_BUF_SIZE_PRV)
{
if (httpsrv_ses_flush(session) == RTCS_ERROR)
{
return(RTCS_ERROR);
}
else
{
retval = n_send;
}
}

return(retval);
}

 

 

 

Regards

Daniel

0 Kudos