www.pudn.com > TcpIpOn8051.rar > BUFFER.C


/******************************************************* 
 
*****************************************/ 
#include  
#include "public.h" 
 
#define CBUFFLEN 0x400 
typedef struct 
{ 
    WORD len;                   // Length of data (must be first)  
    DWORD in;                   // Incoming data  
    DWORD out;                  // Outgoing data  
    DWORD trial;                // Outgoing data 'on trial'  
//send data buffer para 
	BYTE txornot;				//flag of send data or not 
	BYTE flags;					//tcp flags 
	DWORD seqnum;				//seq number 
	DWORD acknum;				//acknumber 
	WORD spoint;				//the start addr of send data 
	WORD txlen;					//length of data to send 
//data 
    BYTE tdata[CBUFFLEN];      // Buffer  
} CBUFF; 
 
extern WORD mindata(WORD a, WORD b); 
extern void move_up(void *destaddr,void *srcaddr,WORD len); 
 
/* Return total length of data in buffer */ 
WORD buff_dlen(CBUFF *bp) 
{ 
    return((WORD)((bp->in - bp->out) & (bp->len - 1))); 
} 
/* Return length of untried (i.e. unsent) data in buffer */ 
WORD buff_untriedlen(CBUFF *bp) 
{ 
    return((WORD)((bp->in - bp->trial) & (bp->len - 1))); 
} 
/* Return length of trial data in buffer (i.e. data sent but unacked) */ 
WORD buff_trylen(CBUFF *bp) 
{ 
    return((WORD)((bp->trial - bp->out) & (bp->len - 1))); 
} 
/* Return length of free space in buffer */ 
WORD buff_freelen(CBUFF *bp) 
{ 
    return(bp->len ? bp->len - 1 - buff_dlen(bp) : 0); 
} 
 
/* Set all the buffer pointers to a starting value */ 
void buff_setall(CBUFF *bp, DWORD start) 
{ 
    bp->out = bp->in = bp->trial = start; 
} 
 
/* Rewind the trial pointer by the given byte count, return actual count */ 
/* 
WORD buff_retry(CBUFF *bp, WORD len) 
{ 
    len = mindata(len, buff_trylen(bp)); 
    bp->trial -= len; 
    return(len); 
} 
*/ 
/* Pre-load data into buffer, i.e. copy into the given buffer location 
** Check that existing data isn't overwritten, return byte count if OK. 
** If data pointer is null, do check but don't transfer data */ 
/* 
WORD buff_preload(CBUFF *bp, DWORD oset, BYTE *tdata, WORD len) 
{ 
    WORD in, n=0, n1, n2, free; 
    long inoff; 
 
    inoff = oset - bp->in;                  // Offset of data from I/P ptr  
    in = (WORD)oset & (bp->len-1);          // Mask I/P ptr to buffer area  
    free = buff_freelen(bp);                // Free space in buffer  
    if (inoff>=0 && inoff<(free))           // If start is in free space..  
    { 
        n = mindata(len, free);                // Get max allowable length  
        n1 = mindata(n, (WORD)(bp->len - in)); // Length up to end of buff  
        n2 = n - n1;                        // Length from start of buff  
        if (n1 && tdata)                     // If anything to copy..  
            move_up(&bp->tdata[in], tdata, n1);// ..copy up to end of buffer..  
        if (n2 && tdata)                     // ..and maybe also..  
            move_up(bp->tdata, &tdata[n1], n2);// ..copy into start of buffer  
    } 
    return(n); 
} 
*/ 
/* Load data into buffer, return byte count that could be accepted 
** If data pointer is null, adjust pointers but don't transfer data */ 
WORD buff_in(CBUFF *bp, BYTE *tdata, WORD len) 
{ 
    WORD in, n, n1, n2; 
 
    in = (WORD)bp->in & (bp->len-1);        // Mask I/P ptr to buffer area  
    n = mindata(len, buff_freelen(bp));        // Get max allowable length  
    n1 = mindata(n, (WORD)(bp->len - in));     // Length up to end of buff  
    n2 = n - n1;                            // Length from start of buff  
    if (n1 && tdata)                         // If anything to copy..  
        move_up(&bp->tdata[in], tdata, n1);    // ..copy up to end of buffer..  
    if (n2 && tdata)                         // ..and maybe also..  
        move_up(bp->tdata, &tdata[n1], n2);    // ..copy into start of buffer  
    bp->in += n;                            // Bump I/P pointer  
    return(n); 
} 
 
/* Load string into buffer, return num of chars that could be accepted */ 
WORD buff_instr(CBUFF *bp, char *str) 
{ 
    return(buff_in(bp, (BYTE *)str, (WORD)strlen(str))); 
} 
 
/* Load file into buffer, return byte count */ 
/* 
WORD buff_infile(CBUFF *bp, FILE *fp, WORD len) 
{ 
    WORD in, n, n1, n2=0; 
    int count=0; 
 
    in = (WORD)bp->in & (bp->len-1);        // Mask I/P ptr to buffer area  
    n = mindata(len, buff_freelen(bp));        // Get max allowable length  
    n1 = mindata(n, (WORD)(bp->len - in));     // Length up to end of buff  
    if (n1)                                 // If anything to read..  
    {                                       // ..get 1st block from file  
        count = fread(&bp->tdata[in], 1, n1, fp); 
        n2 = lentdata, 1, n2, fp); 
    bp->in += count;                        // Bump I/P pointer  
    return((WORD)count); 
} 
*/ 
/* Remove trial data from buffer, return byte count. 
** If data pointer is null, adjust pointers but don't transfer data */ 
WORD buff_try(CBUFF *bp, BYTE *tdata, WORD maxlen) 
{ 
    WORD trial, n, n1, n2; 
 
    trial = (WORD)bp->trial & (bp->len-1);  // Mask trial ptr to buffer area  
    n = mindata(maxlen, buff_untriedlen(bp));  // Get max allowable length  
    n1 = mindata(n, (WORD)(bp->len - trial));  // Length up to end of buff  
    n2 = n - n1;                            // Length from start of buff  
    if (n1 && tdata)                         // If anything to copy..  
        move_up(tdata, &bp->tdata[trial], n1); // ..copy up to end of buffer..  
    if (n2 && tdata)                         // ..and maybe also..  
        move_up(&tdata[n1], bp->tdata, n2);    // ..copy from start of buffer  
    bp->trial += n;                         // Bump trial pointer  
    return(n); 
} 
 
/* Remove data from buffer, return byte count 
** If data pointer is null, adjust pointers but don't transfer data */ 
WORD buff_out(CBUFF *bp, BYTE *tdata, WORD maxlen) 
{ 
    WORD out, n, n1, n2; 
 
    out = (WORD)bp->out & (bp->len-1);      // Mask O/P ptr to buffer area  
    n = mindata(maxlen, buff_dlen(bp));        // Get max allowable length  
    n1 = mindata(n, (WORD)(bp->len - out));    // Length up to end of buff  
    n2 = n - n1;                            // Length from start of buff  
    if (n1 && tdata)                         // If anything to copy..  
        move_up(tdata, &bp->tdata[out], n1);   // ..copy up to end of buffer..  
    if (n2 && tdata)                         // ..and maybe also..  
        move_up(&tdata[n1], bp->tdata, n2);    // ..copy from start of buffer  
    bp->out += n;                           // Bump O/P pointer  
    if (buff_untriedlen(bp) > buff_dlen(bp))// ..and maybe trial pointer  
        bp->trial = bp->out; 
    return(n); 
} 
/* Return length of null-delimited string in buffer, 0 if no null terminator */ 
/* 
WORD buff_strlen(CBUFF *bp) 
{ 
    return(buff_chrlen(bp, 0)); 
} 
*/ 
/* Return length of string in buffer given delimiter char, 0 if no match */ 
 
WORD buff_chrlen(CBUFF *bp, char c) 
{ 
    WORD out, n, n1, n2; 
    BYTE *p, *q=0; 
 
    out = (WORD)bp->out & (bp->len-1);      // Mask O/P ptr to buffer area  
    n = buff_dlen(bp);                      // Get max length  
    n1 = mindata(n, (WORD)(bp->len - out));    // Length up to end of buff  
    n2 = n - n1;                            // Length from start of buff  
    if (n1)                                 // Check up to end of buffer  
        q = memchr(p=&bp->tdata[out], c, n1); 
    if (!q && n2) 
        q = memchr(p=bp->tdata, c, n2);      // ..check data at buffer start  
    else 
        n1 = 0; 
    return(q ? (WORD)(q - p) + n1 : 0); 
} 
WORD buff_inprintf(CBUFF *bp, char *str, char *p) 
{ 
	char *s; 
 
	s=str; 
	while(*s && *s!='%')  
		s++; 
	if(*s=='%') 
	{ 
		*s=0; 
	    s++; 
		if(*s=='s') 
		{ 
			strcat(str,(char *)p); 
			strcat(str,++s); 
		} 
	} 
	return(buff_instr(bp,str)); 
} 
 
/*EOF*/