marignotti/table.c

336 lines
7.4 KiB
C
Raw Permalink Normal View History

2012-05-05 05:08:27 +00:00
#include "marignotti.h"
#include <string.h>
#include <misctool.h>
2012-05-05 06:20:05 +00:00
#include "s16debug.h"
2012-05-05 05:08:27 +00:00
#pragma optimize 79
#pragma noroot
2012-05-12 05:29:39 +00:00
extern boolean readable(Entry *e);
2012-05-05 05:08:27 +00:00
#define TABLE_SIZE 16
#define TABLE_MASK 15
static struct Entry *table[TABLE_SIZE];
// ipids waiting to close + logout.
static struct Entry *dlist;
static void destroy_entry(Entry *e, Boolean abort);
2012-05-05 05:08:27 +00:00
void init_table(void)
{
dlist = NULL;
2012-05-05 05:08:27 +00:00
memset(table, 0, sizeof(table));
}
void destroy_table(void)
{
Entry *e;
Entry *next;
2012-05-05 05:08:27 +00:00
unsigned i;
for (i = 0; i < TABLE_SIZE; ++i)
{
SEI();
e = table[i];
table[i] = 0;
CLI();
while (e)
{
IncBusy();
next = e->next;
destroy_entry(e, true);
2012-05-05 05:08:27 +00:00
e = next;
DecBusy();
}
}
e = dlist;
while (e)
{
next = e->next;
destroy_entry(e, true);
e = next;
2012-05-05 05:08:27 +00:00
}
}
static void destroy_entry(Entry *e, Boolean abort)
{
if (abort) TCPIPAbortTCP(e->ipid);
TCPIPLogout(e->ipid);
sdelete(e->semaphore);
free(e);
}
2012-05-05 05:08:27 +00:00
Entry *find_entry(Word ipid)
{
Entry *e;
IncBusy();
e = table[ipid & TABLE_MASK];
while (e)
{
if (e->ipid == ipid) break;
e = e->next;
}
DecBusy();
return e;
}
Entry *create_entry(Word ipid)
{
Entry *e;
e = NULL;
IncBusy();
e = calloc(sizeof(Entry), 1);
DecBusy();
if (!e) return NULL;
e->semaphore = screate(0);
if (e->semaphore < 0)
{
IncBusy();
free(e);
DecBusy();
return NULL;
}
e->ipid = ipid;
e->_OOBINLINE = 1;
e->_SNDLOWAT = 1024;
e->_RCVLOWAT = 1;
2012-05-12 05:29:39 +00:00
e->select_rd_pid = 0xffff;
e->select_wr_pid = 0xffff;
2012-05-05 05:08:27 +00:00
SEI();
e->next = table[ipid & TABLE_MASK];
table[ipid & TABLE_MASK] = e;
CLI();
return e;
}
void process_table(void)
{
Word terr;
Word t;
LongWord tick;
unsigned i;
Entry *e;
Entry *next;
Entry *prev;
for (i = 0; i < TABLE_SIZE; ++i)
{
prev = NULL;
e = table[i];
while (e)
{
2012-05-05 06:20:05 +00:00
Word command;
2012-05-05 05:08:27 +00:00
next = e->next;
2012-05-12 05:29:39 +00:00
// select.
// do this first ... a close would invalidate.
if (e->select_rd_pid != 0xffff)
{
if (readable(e))
{
Word coll;
Word pid;
SEI()
coll = e->select_rd_collision;
pid = e->select_rd_pid;
e->select_rd_pid = 0xffff;
e->select_rd_collision = 0;
CLI()
if (e->select_fx)
e->select_fx(coll, pid);
if (Debug > 0)
{
s16_debug_printf("select/read wakeup.");
}
}
}
2012-05-05 06:20:05 +00:00
command = e->command;
if (command)
2012-05-05 05:08:27 +00:00
{
2012-05-05 06:20:05 +00:00
Word expired;
Word sig;
2012-05-05 05:08:27 +00:00
Word state;
2012-05-05 06:20:05 +00:00
tick = GetTick();
2012-05-05 05:08:27 +00:00
IncBusy();
2012-05-05 06:20:05 +00:00
expired = 0;
if (e->timeout != 0 && tick > e->timeout)
2012-05-05 05:08:27 +00:00
expired = 1;
terr = TCPIPStatusTCP(e->ipid, &e->sr);
t = _toolErr;
if (t) terr = t;
e->terr = terr;
2012-05-12 01:21:29 +00:00
if (Debug > 2)
{
s16_debug_printf("- ipid: %d command %d",
e->ipid, command);
2012-05-05 06:20:05 +00:00
2012-05-12 01:21:29 +00:00
s16_debug_srbuff(&e->sr);
}
2012-05-05 05:08:27 +00:00
state = e->sr.srState;
2012-05-05 06:20:05 +00:00
sig = 0;
2012-05-05 05:08:27 +00:00
2012-05-05 06:20:05 +00:00
switch(command)
2012-05-05 05:08:27 +00:00
{
case kCommandRead:
// block until data available.
2012-05-12 23:44:10 +00:00
if (expired
|| e->sr.srRcvQueued >= e->cookie
|| e->sr.srRcvQueued >= e->_RCVLOWAT
2012-05-05 05:08:27 +00:00
|| terr)
{
sig = 1;
}
break;
case kCommandWrite:
// block until data sent.
if (e->sr.srSndQueued <= e->cookie
|| expired
|| terr)
{
sig = 1;
}
break;
2012-05-13 23:46:49 +00:00
case kCommandAccept:
if (e->sr.srAcceptCount > 0)
{
sig = 1;
}
break;
2012-05-05 05:08:27 +00:00
case kCommandConnect:
// block until connection established.
2012-05-05 05:08:27 +00:00
if (state >= TCPSESTABLISHED || state == TCPSCLOSED)
{
sig = 1;
}
2012-05-05 06:20:05 +00:00
if (expired)
{
sig = 1;
}
2012-05-05 05:08:27 +00:00
break;
case kCommandDisconnect:
// block until connection closed.
2012-05-05 05:08:27 +00:00
if (state == TCPSCLOSED)
{
sig = 1;
}
break;
case kCommandDisconnectAndLogout:
// move to other list
// since it's no longer a valid fd
if (prev)
2012-05-05 06:20:05 +00:00
{
prev->next = next;
2012-05-05 06:20:05 +00:00
}
else
2012-05-05 05:08:27 +00:00
{
table[i] = next;
2012-05-05 05:08:27 +00:00
}
// add to the dlist.
e->next = dlist;
dlist = e;
e = NULL;
2012-05-05 05:08:27 +00:00
break;
}
if (sig)
{
2012-05-05 06:20:05 +00:00
s16_debug_printf("sending signal to %d", e->semaphore);
2012-05-05 05:08:27 +00:00
e->command = kCommandNone;
ssignal(e->semaphore);
}
DecBusy();
} // e->command
2012-05-12 05:29:39 +00:00
if (e) prev = e;
2012-05-05 05:08:27 +00:00
e = next;
}
}
// now process the disconnect list.
e = dlist;
prev = NULL;
while (e)
{
next = e->next;
IncBusy();
terr = TCPIPStatusTCP(e->ipid, &e->sr);
t = _toolErr;
if (t) terr = t;
DecBusy();
if (e->sr.srState == TCPSCLOSED)
{
destroy_entry(e, false);
e = NULL;
// remove..
if (prev)
{
prev->next = next;
}
else
{
dlist = next;
}
}
if (e) prev = e;
e = next;
}
2012-05-05 05:08:27 +00:00
2016-01-06 00:59:13 +00:00
}