Annotation of embedaddon/mpd/src/msg.c, revision 1.1.1.3
1.1 misho 1:
2: /*
3: * msg.c
4: *
5: * Written by Archie Cobbs <archie@freebsd.org>
6: * Copyright (c) 1995-1999 Whistle Communications, Inc. All rights reserved.
7: * See ``COPYRIGHT.whistle''
8: */
9:
10: #include "ppp.h"
11: #include "msg.h"
12:
13: /*
14: * DEFINITIONS
15: */
16:
17: /* Which pipe file descriptor is which */
18:
19: #define PIPE_READ 0
20: #define PIPE_WRITE 1
21:
22: struct mpmsg
23: {
24: int type;
25: void (*func)(int type, void *arg);
26: void *arg;
27: const char *dbg;
28: };
29: typedef struct mpmsg *Msg;
30:
1.1.1.3 ! misho 31: static struct mpmsg msgqueue[MSG_QUEUE_LEN];
! 32: static int msgqueueh = 0;
! 33: static int msgqueuet = 0;
1.1 misho 34: #define QUEUELEN() ((msgqueueh >= msgqueuet)? \
35: (msgqueueh - msgqueuet):(msgqueueh + MSG_QUEUE_LEN - msgqueuet))
36:
1.1.1.3 ! misho 37: static int msgpipe[2];
! 38: static int msgpipesent = 0;
! 39: static EventRef msgevent;
1.1 misho 40:
41: /*
1.1.1.2 misho 42: * GLOBAL VARIABLES
43: */
44:
45: int gQThresMin = 64;
46: int gQThresMax = 256;
47: int gQThresDiff = 256 - 64;
48:
49: /*
1.1 misho 50: * INTERNAL FUNCTIONS
51: */
52:
53: static void MsgEvent(int type, void *cookie);
54:
55: /*
56: * MsgRegister()
57: */
58:
59: void
60: MsgRegister2(MsgHandler *m, void (*func)(int type, void *arg), const char *dbg)
61: {
62: if ((msgpipe[0]==0) || (msgpipe[1]==0)) {
63: if (pipe(msgpipe) < 0) {
64: Perror("%s: Can't create message pipe",
65: __FUNCTION__);
66: DoExit(EX_ERRDEAD);
67: }
68: fcntl(msgpipe[PIPE_READ], F_SETFD, 1);
69: fcntl(msgpipe[PIPE_WRITE], F_SETFD, 1);
70:
71: if (fcntl(msgpipe[PIPE_READ], F_SETFL, O_NONBLOCK) < 0)
72: Perror("%s: fcntl", __FUNCTION__);
73: if (fcntl(msgpipe[PIPE_WRITE], F_SETFL, O_NONBLOCK) < 0)
74: Perror("%s: fcntl", __FUNCTION__);
75:
76: if (EventRegister(&msgevent, EVENT_READ,
77: msgpipe[PIPE_READ], EVENT_RECURRING, MsgEvent, NULL) < 0) {
78: Perror("%s: Can't register event", __FUNCTION__);
79: DoExit(EX_ERRDEAD);
80: }
81: }
82:
83: m->func = func;
84: m->dbg = dbg;
85: }
86:
87: /*
88: * MsgUnRegister()
89: */
90:
91: void
92: MsgUnRegister(MsgHandler *m)
93: {
94: m->func = NULL;
95: m->dbg = NULL;
96: }
97:
98: /*
99: * MsgEvent()
100: */
101:
102: static void
103: MsgEvent(int type, void *cookie)
104: {
105: char buf[16];
1.1.1.3 ! misho 106:
! 107: (void)type;
! 108: (void)cookie;
1.1 misho 109: /* flush signaling pipe */
110: msgpipesent = 0;
111: while (read(msgpipe[PIPE_READ], buf, sizeof(buf)) == sizeof(buf));
112:
113: while (msgqueuet != msgqueueh) {
114: Log(LG_EVENTS, ("EVENT: Message %d to %s received",
115: msgqueue[msgqueuet].type, msgqueue[msgqueuet].dbg));
116: (*(msgqueue[msgqueuet].func))(msgqueue[msgqueuet].type, msgqueue[msgqueuet].arg);
117: Log(LG_EVENTS, ("EVENT: Message %d to %s processed",
118: msgqueue[msgqueuet].type, msgqueue[msgqueuet].dbg));
119:
120: msgqueuet = (msgqueuet + 1) & MSG_QUEUE_MASK;
121: SETOVERLOAD(QUEUELEN());
122: }
123: }
124:
125: /*
126: * MsgSend()
127: */
128:
129: void
130: MsgSend(MsgHandler *m, int type, void *arg)
131: {
132: assert(m);
133: assert(m->func);
134:
135: msgqueue[msgqueueh].type = type;
136: msgqueue[msgqueueh].func = m->func;
137: msgqueue[msgqueueh].arg = arg;
138: msgqueue[msgqueueh].dbg = m->dbg;
139:
140: msgqueueh = (msgqueueh + 1) & MSG_QUEUE_MASK;
141: if (msgqueuet == msgqueueh) {
142: Log(LG_ERR, ("%s: Fatal message queue overflow!", __FUNCTION__));
143: DoExit(EX_ERRDEAD);
144: }
145:
146: SETOVERLOAD(QUEUELEN());
147:
148: if (!msgpipesent) {
149: char buf[1] = { 0x2a };
150: if (write(msgpipe[PIPE_WRITE], buf, 1) > 0)
151: msgpipesent = 1;
152: }
153: Log(LG_EVENTS, ("EVENT: Message %d to %s sent", type, m->dbg));
154: }
155:
156: /*
157: * MsgName()
158: */
159:
160: const char *
161: MsgName(int msg)
162: {
163: switch (msg)
164: {
165: case MSG_OPEN:
166: return("OPEN");
167: case MSG_CLOSE:
168: return("CLOSE");
169: case MSG_UP:
170: return("UP");
171: case MSG_DOWN:
172: return("DOWN");
173: case MSG_SHUTDOWN:
174: return("SHUTDOWN");
175: default:
176: return("???");
177: }
178: }
179:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>