--- l2_channel.c 2001/05/22 20:00:12 1.3
+++ l2_channel.c 2001/05/24 09:40:28 1.4
@@ -31,6 +31,8 @@
#include "l2.h"
#include "l2_p.h"
+#include <string.h>
+
l2_channel_t *l2_channel_create(l2_handler_t *h)
{
l2_channel_t *ch;
@@ -39,15 +41,10 @@
return NULL;
if ((ch = (l2_channel_t *)malloc(sizeof(l2_channel_t))) == NULL)
return NULL;
- ch->below = NULL;
- ch->context.vp = NULL;
- ch->handler.create = h->create;
- ch->handler.configure = h->configure;
- ch->handler.open = h->open;
- ch->handler.write = h->write;
- ch->handler.flush = h->flush;
- ch->handler.close = h->close;
- ch->handler.destroy = h->destroy;
+ ch->state = L2_CHSTATE_CREATED;
+ ch->downstream = NULL;
+ memset(&ch->context, 0, sizeof(l2_context_t));
+ memcpy(&ch->handler, h, sizeof(l2_handler_t));
if (ch->handler.create(&ch->context) == L2_ERROR) {
free(ch);
return NULL;
@@ -59,8 +56,11 @@
{
if (ch1 == NULL || ch2 == NULL)
return NULL;
- ch2->below = ch1;
- return ch2;
+ if ( ch1->state != L2_CHSTATE_CREATED
+ || ch2->state != L2_CHSTATE_CREATED)
+ return NULL;
+ ch1->downstream = ch2;
+ return ch1;
}
int l2_channel_configure(l2_channel_t *ch, const char *fmt, ...)
@@ -70,6 +70,8 @@
if (ch == NULL || fmt == NULL)
return L2_ERROR;
+ if (ch->state != L2_CHSTATE_CREATED)
+ return L2_ERROR;
va_start(ap, fmt);
rv = ch->handler.configure(&ch->context, fmt, ap);
va_end(ap);
@@ -78,62 +80,91 @@
int l2_channel_open(l2_channel_t *ch)
{
+ l2_result_t rv;
+
if (ch == NULL)
return L2_ERROR;
+ if (ch->state != L2_CHSTATE_CREATED)
+ return L2_ERROR;
while (ch != NULL && ch->handler.open == NULL)
- ch = ch->below;
+ ch = ch->downstream;
if (ch == NULL)
return L2_ERROR;
- return ch->handler.open(&ch->context, ch->below);
+ rv = ch->handler.open(&ch->context, ch->downstream);
+ if (rv == L2_OK)
+ ch->state = L2_CHSTATE_OPENED;
+ return rv;
}
int l2_channel_write(l2_channel_t *ch, const char *buf, size_t bufsize)
{
- if (ch == NULL || buf == NULL || bufsize <= 0)
- return L2_ERROR;
- while (ch != NULL && ch->handler.write == NULL)
- ch = ch->below;
+ l2_result_t rv;
+
if (ch == NULL)
return L2_ERROR;
+ if (ch->state != L2_CHSTATE_OPENED)
+ return L2_ERROR;
if (buf == NULL)
return L2_ERROR;
if (bufsize == 0)
return L2_OK;
- return ch->handler.write(&ch->context, ch->below, buf, bufsize);
+ while (ch != NULL && ch->handler.write == NULL)
+ ch = ch->downstream;
+ if (ch == NULL)
+ return L2_ERROR;
+ rv = ch->handler.write(&ch->context, ch->downstream, buf, bufsize);
+ return rv;
}
int l2_channel_flush(l2_channel_t *ch)
{
+ l2_result_t rv;
+
if (ch == NULL)
return L2_ERROR;
+ if (ch->state != L2_CHSTATE_OPENED)
+ return L2_ERROR;
while (ch != NULL && ch->handler.flush == NULL)
- ch = ch->below;
+ ch = ch->downstream;
if (ch == NULL)
return L2_ERROR;
- return ch->handler.flush(&ch->context, ch->below);
+ rv = ch->handler.flush(&ch->context, ch->downstream);
+ return rv;
}
int l2_channel_close(l2_channel_t *ch)
{
+ l2_result_t rv;
+
if (ch == NULL)
return L2_ERROR;
+ if (ch->state != L2_CHSTATE_OPENED)
+ return L2_ERROR;
while (ch != NULL && ch->handler.close == NULL)
- ch = ch->below;
+ ch = ch->downstream;
if (ch == NULL)
return L2_ERROR;
- return ch->handler.close(&ch->context, ch->below);
+ rv = ch->handler.close(&ch->context, ch->downstream);
+ if (rv == L2_OK)
+ ch->state = L2_CHSTATE_CREATED;
+ return rv;
}
int l2_channel_destroy(l2_channel_t *ch)
{
- int rv = L2_OK;
+ l2_result_t rv;
if (ch == NULL)
return L2_ERROR;
+ if (ch->state == L2_CHSTATE_OPENED)
+ if ((rv = l2_channel_close(ch)) != L2_OK)
+ return rv;
while (ch != NULL && ch->handler.destroy == NULL)
- ch = ch->below;
+ ch = ch->downstream;
if (ch != NULL)
rv = ch->handler.destroy(&ch->context);
+ else
+ rv = L2_OK;
free(ch);
return rv;
}
@@ -145,6 +176,8 @@
int ok;
int i;
+ if (pa == NULL || fmt == NULL || ap == NULL)
+ return L2_ERROR;
cpE = fmt;
while (*cpE != '\0') {
/* determine begin of parameter name */
|