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
|
|
|
|
|
|
|
|
#define TABLE_SIZE 16
|
|
|
|
#define TABLE_MASK 15
|
|
|
|
static struct Entry *table[TABLE_SIZE];
|
|
|
|
|
2012-05-06 22:53:36 +00:00
|
|
|
// 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)
|
|
|
|
{
|
2012-05-06 22:53:36 +00:00
|
|
|
dlist = NULL;
|
2012-05-05 05:08:27 +00:00
|
|
|
memset(table, 0, sizeof(table));
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_table(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
Entry *e;
|
2012-05-06 22:53:36 +00:00
|
|
|
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;
|
2012-05-06 22:53:36 +00:00
|
|
|
destroy_entry(e, true);
|
2012-05-05 05:08:27 +00:00
|
|
|
e = next;
|
|
|
|
|
|
|
|
DecBusy();
|
|
|
|
}
|
2012-05-06 22:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e = dlist;
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
next = e->next;
|
|
|
|
destroy_entry(e, true);
|
|
|
|
e = next;
|
2012-05-05 05:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-06 22:53:36 +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;
|
|
|
|
|
|
|
|
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-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:
|
2012-05-06 22:53:36 +00:00
|
|
|
// block until data available.
|
2012-05-05 05:08:27 +00:00
|
|
|
if (e->sr.srRcvQueued >= e->cookie
|
|
|
|
|| expired
|
|
|
|
|| terr)
|
|
|
|
{
|
|
|
|
sig = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-05-06 22:53:36 +00:00
|
|
|
|
|
|
|
case kCommandWrite:
|
|
|
|
// block until data sent.
|
|
|
|
if (e->sr.srSndQueued <= e->cookie
|
|
|
|
|| expired
|
|
|
|
|| terr)
|
|
|
|
{
|
|
|
|
sig = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2012-05-05 05:08:27 +00:00
|
|
|
case kCommandConnect:
|
2012-05-06 22:53:36 +00:00
|
|
|
// 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:
|
2012-05-06 22:53:36 +00:00
|
|
|
// block until connection closed.
|
2012-05-05 05:08:27 +00:00
|
|
|
if (state == TCPSCLOSED)
|
|
|
|
{
|
|
|
|
sig = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCommandDisconnectAndLogout:
|
2012-05-06 22:53:36 +00:00
|
|
|
|
|
|
|
// move to other list
|
|
|
|
// since it's no longer a valid fd
|
|
|
|
|
|
|
|
if (prev)
|
2012-05-05 06:20:05 +00:00
|
|
|
{
|
2012-05-06 22:53:36 +00:00
|
|
|
prev->next = next;
|
2012-05-05 06:20:05 +00:00
|
|
|
}
|
2012-05-06 22:53:36 +00:00
|
|
|
else
|
2012-05-05 05:08:27 +00:00
|
|
|
{
|
2012-05-06 22:53:36 +00:00
|
|
|
table[i] = next;
|
2012-05-05 05:08:27 +00:00
|
|
|
}
|
2012-05-06 22:53:36 +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-06 22:53:36 +00:00
|
|
|
|
|
|
|
if (e) prev = e;
|
2012-05-05 05:08:27 +00:00
|
|
|
e = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2012-05-06 22:53:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
}
|