Commit 1d5b5784 authored by Niels Möller's avatar Niels Möller
Browse files

*** empty log message ***

Rev: src/blocking_write.c:1.1
Rev: src/blocking_write.h:1.1
Rev: src/packet_dispatch.c:1.1
Rev: src/packet_dispatch.h:1.1
parent 37dd36d6
/* blocking_write.c
*
*/
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
static int do_write(struct pad_processor *closure,
struct simple_packet *packet)
{
UINT32 left = packet->length;
UINT8 *p = packet->data;
while(left)
{
int written = write(closure->fd, p, left);
if ( (written < 0)
&& (errno == EINTR) )
continue;
if (written <= 0)
return 0;
left -= written;
p += written;
}
simple_packet_free(packet);
return 1;
}
struct packet_processor *make_blocking_write_procesor(int fd)
{
struct blocking_write_processor *closure
= xalloc(sizeof(struct blocking_write_processor_processor));
closure->p->f = (raw_processor_function) do_write;
closure->fd = fd;
return (struct packet_processor *) closure;
}
/* blocking_write.h
*
*/
#ifndef LSH_BLOCKING_WRITE_H_INCLUDED
#define LSH_BLOCKING_WRITE_H_INCLUDED
struct blocking_write_processor
{
struct packet_processor p;
int fd;
};
struct packet_processor *make_blocking_write_procesor(int fd);
#endif /* LSH_BLOCKING_WRITE_H_INCLUDED */
/* packet_dispatch.c
*
*/
static int do_dispatch(struct dispatch_processor *closure,
struct simple_packet *packet)
{
unsigned start;
unsigned end;
unsigned msg;
if (!packet->length)
return 0;
msg = packet->data[0];
/* Do a binary serch. The number of valid message types should be rather small.
*/
start = 0;
end = closure->table_size;
while(1)
{
unsigned middle = (start + end) / 2;
unsigned middle_msg = closure->dispatch_table[middle]->msg;
if (middle_msg == msg)
{
/* Found right method */
return apply_processor(closure->dispatch_table[middle]->f,
packet);
}
if (middle == start)
/* Not found */
break;
if (middle_id < msg)
start = middle;
else
end = middle;
}
if (closure->default)
return apply_processor(closure->default, packet);
else
return 0;
}
struct packet_processor *
make_dispatch_processor(unsigned size,
struct dispatch_assoc *table,
struct packet_processor *default)
{
struct dispatch_processor *closure = xalloc(sizeof(struct dispatch_processor));
unsigned i;
/* Check that message numbers are increasing */
for(i = 0; i+1 < size; i++)
if (table[i]->msg >= table[i+1]->msg)
fatal("make_dispatch_processor: Table out of order");
closure->p->f = (raw_processor_function) do_dispatch;
closure->default = default;
closure->table_size = size;
closure->dispatch_table = table;
return (struct packet_processor *) closure;
}
/* packet_dispatch.h
*
* Pass packets on to one of several handlers.
*/
#ifndef LSH_PACKET_DISPATH_H_INCLUDED
#define LSH_PACKET_DISPATH_H_INCLUDED
struct dispatch_assoc
{
int msg;
struct packet_processor *f;
};
struct dispatch_processor
{
struct packet_processor p;
struct packet_processor *default;
unsigned table_size;
/* Should be sorted by message number */
struct dispatch_assoc *dispatch_table;
};
struct packet_processor *
make_dispatch_processor(unsigned size,
struct dispatch_assoc *table,
struct packet_processor *default);
#endif /* LSH_PACKET_DISPATH_H_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment