DIM SHARED Users
(1 TO 1000) AS clients
' array to hold other client info PING = "PING"
ON TIMER(Update&
, .5) DisplayStats
'timer for Host status.
'OPEN "HostDebug.txt" FOR OUTPUT AS #3
HostMain HostID&
PRINT "Error starting up host"
NumClients = 0
NumClients = NumClients + 1
Users(NumClients).Id = newclient
LastJoin = UpTime&
FOR I
= 1 TO NumClients
' distribute incoming messages to all clients GET #Users
(I
).Id
, , message$
IF message$
= "PONG" THEN Users
(I
).PingTime
= -1 'User Replied to PING IF message$
= "LEAVING" THEN DropUser I
'user quits IF LEFT$(message$
, 5) = "HELLO" THEN AddNick message$
, I
'get new user nickname IF Users
(I
).PingTime
> 0 AND Users
(I
).PingTime
+ 45 = UpTime&
THEN DropUser I
'Client Failed to pong IF LEFT$(message$
, 4) = "TALK" THEN PRINT "Message recieved": MessageParse I
, message$
_DELAY 0.04 ' reduce CPU usage PRINT "Shutting down Host"
'open host for file transfer
IF port%
= 0 THEN default%
= 1979 ELSE default%
= port%
Result = HostID&
StartHost = Result
'Current Status
LOCATE 1, 1:
PRINT "Host Up Time-"; Clock$
(UpTime&
);
"("; UpTime&;
")" click%% = click%% + 1
IF click%%
= 2 THEN UpTime&
= UpTime&
+ 1: click%%
= 0
Mins%% = (tim& - Secs%%) / 60
Hrs%%
= INT(Mins%%
/ 60) - .49 Clock$ = H$ + ":" + M$ + ":" + S$
IF NumClients
> 0 THEN 'only bother if there are clients FOR I
= 1 TO NumClients
' distribute PING message to all clients Users(I).PingTime = UpTime&
LastPing = UpTime&
SendUpTime UpTime&
'if user sends "LEAVING" message or fails to respond to a PING
'then drop the user
FOR J%
= id%
TO NumClients
SWAP Users
(J%
), Users
(J%
+ 1) SWAP NICK
(J%
), NICK
(J%
+ 1) Users(J%).Id = 0
Users(J%).PingTime = 0
NICK(J%) = ""
NumClients = NumClients - 1
SUB MessageParse
(WhoFrom&
, Txt$
) 'find who the message is for
match% = FALSE
'---------------------------
'pull message body
'---------------------------
MSG$
= NICK
(WhoFrom&
) + " SAYS: " + CHR$(34) + MSG$
+ CHR$(34) 'send message to right client(hopefully)
PUT #Users
(match%
).Id
, , MSG$
ELSE 'no name given send to all clients PUT #Users
(i%
).Id
, , MSG$
'---------------------------
SUB DataParse
(WhoFrom&
, Dat$
)
InUse%% = FALSE
temp$
= MID$(TXT$
, 7) 'get new user Nickname 'make sure name is not already in use
MSG$ = "BADNICK"
PUT #Users
(WHO&
).Id
, , MSG$
TXT$ = "DATASTRGTIME" + Clock$(UpTime&)
PUT #Users
(i%
).Id
, , TXT$