File: sockbuf.c

package info (click to toggle)
modemu 0.0.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 200 kB
  • ctags: 266
  • sloc: ansic: 1,860; lex: 146; makefile: 72
file content (81 lines) | stat: -rw-r--r-- 1,748 bytes parent folder | download | duplicates (4)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifdef TERMNET
#include <termnet.h>
#endif
#include <stdio.h>	/*stderr,(perror)*/
#include <sys/types.h>	/*->socket.h (u_short etc.)*/
#include <sys/socket.h>	/*(send,recv)*/

#include "defs.h"	/*->sockbuf.h (uchar,SOCKBUFR_SIZE,TTYBUFR_SIZE)*/
#include "sockbuf.h"	/*sockBufR,sockBufW*/
#include "sock.h"	/*sock*/
#include "verbose.h"	/*VERB_MISC*/


/* reading socket */

void
sockBufRead(void)
{
    int l;

    l = recv(sock.fd, sockBufR.buf, sizeof(sockBufR.buf), 0);
    if (l <= 0) {
	sock.alive = 0;
	if (l == 0)
	    verboseOut(VERB_MISC, "Connection closed by peer.\r\n");
	else
	    /* PPP link down or something to reach here. */
	    /* v0.0 exited, which comm progs don't expect. */
	    /* now just NO CARRIERs. Thanks >> Rod May */
	    perror("recv()");
	return;
    }
    sockBufR.ptr = sockBufR.buf;
    sockBufR.end = sockBufR.buf + l;
}


/* writing socket */

void
sockBufWrite(void)
{
    int wl,l;

    wl = sockBufW.ptr - sockBufW.top;
    if (wl == 0) return;
    l = send(sock.fd, sockBufW.top, wl, 0);
    if (l <= 0) {
	sock.alive = 0;
	if (l == 0)
	    verboseOut(VERB_MISC, "Connection closed by peer.\r\n");
	else
	    perror("send()");
	return;
    } else if (l < wl) {
	sockBufW.top += l;
	/*return 1;*/ /* needs retry */
	return;
    }
    sockBufW.ptr = sockBufW.top = sockBufW.buf;
    sockBufW.stop = 0;
    return;
}

void
putSock1(uchar c)
{
    if (sockBufW.ptr >= sockBufW.buf + SOCKBUFW_SIZE) { /* limit */
	if (sockBufW.ptr >= sockBufW.buf + SOCKBUFW_SIZE_A) { /*actual limit*/
	    fprintf(stderr,"\asockBufW overrun.\n");
	    return;
	} else sockBufW.stop = 1; /* flow control */
    }
    *sockBufW.ptr++ = c;
}

void
putSockN(const uchar *cp, int n)
{
    for (; n > 0; n--,cp++) putSock1(*cp);
}