Annotation of embedaddon/mpd/src/msg.c, revision 1.1.1.2
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:
31: struct mpmsg msgqueue[MSG_QUEUE_LEN];
32: int msgqueueh = 0;
33: int msgqueuet = 0;
34: #define QUEUELEN() ((msgqueueh >= msgqueuet)? \
35: (msgqueueh - msgqueuet):(msgqueueh + MSG_QUEUE_LEN - msgqueuet))
36:
37: int msgpipe[2];
38: int msgpipesent = 0;
39: EventRef msgevent;
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];
106: /* flush signaling pipe */
107: msgpipesent = 0;
108: while (read(msgpipe[PIPE_READ], buf, sizeof(buf)) == sizeof(buf));
109:
110: while (msgqueuet != msgqueueh) {
111: Log(LG_EVENTS, ("EVENT: Message %d to %s received",
112: msgqueue[msgqueuet].type, msgqueue[msgqueuet].dbg));
113: (*(msgqueue[msgqueuet].func))(msgqueue[msgqueuet].type, msgqueue[msgqueuet].arg);
114: Log(LG_EVENTS, ("EVENT: Message %d to %s processed",
115: msgqueue[msgqueuet].type, msgqueue[msgqueuet].dbg));
116:
117: msgqueuet = (msgqueuet + 1) & MSG_QUEUE_MASK;
118: SETOVERLOAD(QUEUELEN());
119: }
120: }
121:
122: /*
123: * MsgSend()
124: */
125:
126: void
127: MsgSend(MsgHandler *m, int type, void *arg)
128: {
129: assert(m);
130: assert(m->func);
131:
132: msgqueue[msgqueueh].type = type;
133: msgqueue[msgqueueh].func = m->func;
134: msgqueue[msgqueueh].arg = arg;
135: msgqueue[msgqueueh].dbg = m->dbg;
136:
137: msgqueueh = (msgqueueh + 1) & MSG_QUEUE_MASK;
138: if (msgqueuet == msgqueueh) {
139: Log(LG_ERR, ("%s: Fatal message queue overflow!", __FUNCTION__));
140: DoExit(EX_ERRDEAD);
141: }
142:
143: SETOVERLOAD(QUEUELEN());
144:
145: if (!msgpipesent) {
146: char buf[1] = { 0x2a };
147: if (write(msgpipe[PIPE_WRITE], buf, 1) > 0)
148: msgpipesent = 1;
149: }
150: Log(LG_EVENTS, ("EVENT: Message %d to %s sent", type, m->dbg));
151: }
152:
153: /*
154: * MsgName()
155: */
156:
157: const char *
158: MsgName(int msg)
159: {
160: switch (msg)
161: {
162: case MSG_OPEN:
163: return("OPEN");
164: case MSG_CLOSE:
165: return("CLOSE");
166: case MSG_UP:
167: return("UP");
168: case MSG_DOWN:
169: return("DOWN");
170: case MSG_SHUTDOWN:
171: return("SHUTDOWN");
172: default:
173: return("???");
174: }
175: }
176:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>