-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathniu.c
1200 lines (1106 loc) · 32.1 KB
/
niu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003, Tom Hunter, Paul Koning (see license.txt)
**
** Name: niu.c
**
** Description:
** Perform emulation of Plato NIU (terminal controller)
**
**--------------------------------------------------------------------------
*/
#define DEBUG_PP 0
#define DEBUG_NET 0
#define REAL_TIMING 1
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <memory.h>
#include <ctype.h>
#if defined(_WIN32)
#include <winsock.h>
#else
#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include "const.h"
#include "types.h"
#include "proto.h"
/*
** -----------------
** Private Constants
** -----------------
*/
#define NiuLocalStations 32 // range reserved for local stations
#define NiuLocalBufSize 50 // size of local input buffer
#define IoTurnsPerPoll 4
#define InBufSize 32
#define OutBufSize 256
/*
** Function codes.
*/
#define FcNiuOutput 00000
#define FcNiuInput 00040
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
#if DEBUG_PP || DEBUG_NET
#define HexColumn(x) (3 * (x) + 4)
#define AsciiColumn(x) (HexColumn(16) + 2 + (x))
#define LogLineLength (AsciiColumn(16))
#endif
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
typedef struct portParam
{
int id;
int connFd;
u16 currInput;
u8 ibytes; // how many bytes have been assembled into currInput (0..2)
bool active;
int inInIdx;
int inOutIdx;
u8 inBuffer[InBufSize];
int outInIdx;
int outOutIdx;
u8 outBuffer[OutBufSize];
} PortParam;
typedef struct localRing
{
u8 buf[NiuLocalBufSize];
int get;
int put;
} LocalRing;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static void niuClose(PortParam *pp);
static FcStatus niuInFunc(PpWord funcCode);
static void niuInIo(void);
static void niuInit(void);
static void niuLogBytes(u8 *bytes, int len);
static void niuLogFlush(void);
static FcStatus niuOutFunc(PpWord funcCode);
static void niuOutIo(void);
static void niuActivate(void);
static void niuDisconnect(void);
static void niuCheckIo(void);
static void niuWelcome(int stat);
static void niuSend(int stat, int word);
static void niuSendstr(int stat, const char *p);
#if DEBUG_PP || DEBUG_NET
static char *niuFunc2String(PpWord funcCode);
#endif
/*
** ----------------
** Public Variables
** ----------------
*/
u16 platoPort;
u16 platoConns;
/*
** -----------------
** Private Variables
** -----------------
*/
static int currInPort;
static u32 currOutput;
static DevSlot *in = NULL;
static int ioTurns = IoTurnsPerPoll - 1;
static DevSlot *out = NULL;
static int lastInPort;
static int listenFd = 0;
static LocalRing localInput[NiuLocalStations];
static int obytes;
static niuProcessOutput *outputHandler[NiuLocalStations];
static PortParam *portVector;
#if REAL_TIMING
static bool frameStart;
static u32 lastFrame;
#endif
#if DEBUG_PP || DEBUG_NET
static FILE *niuLog = NULL;
static char niuLogBuf[LogLineLength + 1];
static int niuLogBytesCol = 0;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise NIU-IN
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo normally unit number, here abused as input channel.
** channelNo output channel number.
** params optional device parameters
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void niuInInit(u8 eqNo, u8 unitNo, u8 channelNo, char *params)
{
int listenPort;
int numParam;
int portCount;
if (in != NULL)
{
fputs("Multiple NIUs not supported\n", stderr);
exit(1);
}
in = channelAttach(channelNo, eqNo, DtNiu);
in->activate = niuActivate;
in->disconnect = niuDisconnect;
in->func = niuInFunc;
in->io = niuInIo;
if (params == NULL)
{
params = "";
}
numParam = sscanf(params, "%d,%d", &listenPort, &portCount);
if (numParam > 1)
{
platoPort = listenPort;
platoConns = portCount;
}
else if (numParam > 0)
{
platoPort = listenPort;
}
if ((platoPort < 1) || (platoPort > 65535))
{
fprintf(stderr, "(niu ) Invalid TCP port number in NIU definition: %d\n", platoPort);
exit(1);
}
if (platoConns < 1)
{
fprintf(stderr, "(niu ) Invalid connection count in NIU definition: %d\n", platoConns);
exit(1);
}
if (out != NULL)
{
niuInit();
}
/*
** Print a friendly message.
*/
printf("(niu ) Initialised with input channel %o, max connections %d, TCP port %d\n", channelNo, platoConns, platoPort);
}
/*--------------------------------------------------------------------------
** Purpose: Initialise NIU-OUT
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo normally unit number, here abused as input channel.
** channelNo output channel number.
** deviceName optional device file name
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void niuOutInit(u8 eqNo, u8 unitNo, u8 channelNo, char *deviceName)
{
if (out != NULL)
{
fputs("Multiple NIUs not supported\n", stderr);
exit(1);
}
out = channelAttach(channelNo, eqNo, DtNiu);
out->activate = niuActivate;
out->disconnect = niuDisconnect;
out->func = niuOutFunc;
out->io = niuOutIo;
if (in != NULL)
{
niuInit();
}
/*
** Print a friendly message.
*/
printf("(niu ) Initialised with output channel %o\n", channelNo);
}
/*--------------------------------------------------------------------------
** Purpose: Report if NIU is configured
**
** Parameters: Name Description.
**
** Returns: TRUE if it is.
**
**------------------------------------------------------------------------*/
bool niuPresent(void)
{
return (portVector != NULL);
}
/*--------------------------------------------------------------------------
** Purpose: Process local Plato mode input
**
** Parameters: Name Description.
** key Plato key code for station
** stat Station number
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void niuLocalKey(u16 key, int stat)
{
int nextput;
LocalRing *rp;
if (stat >= NiuLocalStations)
{
fprintf(stderr, "Local station number out of range: %d\n", stat);
exit(1);
}
rp = &localInput[stat];
nextput = rp->put + 1;
if (nextput == NiuLocalBufSize)
{
nextput = 0;
}
if (nextput != rp->get)
{
rp->buf[rp->put] = (u8)key;
rp->put = nextput;
}
}
/*--------------------------------------------------------------------------
** Purpose: Set handler address for local station output
**
** Parameters: Name Description.
** h Output handler function
** stat Station number
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void niuSetOutputHandler(niuProcessOutput *h, int stat)
{
if (stat >= NiuLocalStations)
{
fprintf(stderr, "Local station number out of range: %d\n", stat);
exit(1);
}
outputHandler[stat] = h;
}
/*--------------------------------------------------------------------------
** Purpose: Show NIU status (operator interface).
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void niuShowStatus()
{
int g;
int i;
char outBuf[200];
PortParam *pp;
if (listenFd > 0)
{
sprintf(outBuf, " > %-8s C%02o E%02o ", "NIU", in->channel->id, in->eqNo);
opDisplay(outBuf);
sprintf(outBuf, FMTNETSTATUS"\n", netGetLocalTcpAddress(listenFd), "", "plato", "listening");
opDisplay(outBuf);
for (i = 0, pp = portVector; i < platoConns; i++, pp++)
{
if (pp->active && pp->connFd > 0)
{
sprintf(outBuf, " > %-8s P%02o ", "NIU", pp->id);
opDisplay(outBuf);
sprintf(outBuf, FMTNETSTATUS"\n", netGetLocalTcpAddress(pp->connFd), netGetPeerTcpAddress(pp->connFd), "plato", "connected");
opDisplay(outBuf);
}
}
}
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Execute function code on NIU input channel.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static void niuInit(void)
{
u8 i;
PortParam *pp;
#if DEBUG_PP || DEBUG_NET
if (niuLog == NULL)
{
niuLog = fopen("niuLog.txt", "wt");
fputs("\nniuLog opened.\n", niuLog);
}
#endif
pp = portVector = calloc(platoConns, sizeof(PortParam));
if (portVector == NULL)
{
fputs("Failed to allocate NIU context block\n", stderr);
exit(1);
}
in->context[0] = portVector;
/*
** Initialise port control blocks.
*/
for (i = 0; i < platoConns; i++)
{
pp->id = i;
pp->active = FALSE;
pp->connFd = 0;
pp->ibytes = 0;
pp++;
}
for (i = 0; i < NiuLocalStations; i++)
{
localInput[i].get = localInput[i].put = 0;
}
currInPort = -1;
lastInPort = 0;
ioTurns = IoTurnsPerPoll - 1;
/*
** Start listening for new connections on the configured PLATO port number
*/
listenFd = netCreateListener(platoPort);
#if defined(_WIN32)
if (listenFd == INVALID_SOCKET)
#else
if (listenFd == -1)
#endif
{
fprintf(stderr, "(niu ) Can't listen for NIU on port %d\n", platoPort);
exit(1);
}
fprintf(stdout, "(niu ) Listening on port %d (%d connections permitted).\n", platoPort, platoConns);
#if REAL_TIMING
frameStart = FALSE;
#endif
}
/*--------------------------------------------------------------------------
** Purpose: Execute function code on NIU input channel.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus niuInFunc(PpWord funcCode)
{
#if DEBUG_PP
fprintf(niuLog, "\n%06d PP:%02o CH:%02o f:%04o T:%-8s > ",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id,
funcCode,
niuFunc2String(funcCode));
#endif
niuCheckIo();
if (funcCode == FcNiuInput)
{
currInPort = -1;
activeDevice->fcode = funcCode;
return (FcAccepted);
}
else
{
return (FcDeclined);
}
}
/*--------------------------------------------------------------------------
** Purpose: Execute function code on NIU output channel.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus niuOutFunc(PpWord funcCode)
{
#if DEBUG_PP
fprintf(niuLog, "\n%06d PP:%02o CH:%02o f:%04o T:%-8s > ",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id,
funcCode,
niuFunc2String(funcCode));
#endif
niuCheckIo();
if (funcCode == FcNiuOutput)
{
obytes = 0;
activeDevice->fcode = funcCode;
return (FcAccepted);
}
else
{
return (FcDeclined);
}
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on NIU input channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void niuInIo(void)
{
int port;
int in;
int nextget;
PortParam *pp;
LocalRing *rp;
if ((activeDevice->fcode != FcNiuInput) || activeChannel->full)
{
return;
}
if (currInPort < 0)
{
// We're at the first of the two-word input sequence; find a
// port with data.
port = lastInPort;
for ( ; ;)
{
if (++port >= NiuLocalStations + platoConns)
{
port = 0;
}
if (port < NiuLocalStations)
{
// check for local terminal input
rp = &localInput[port];
if (rp->get != rp->put)
{
currInPort = lastInPort = port;
activeChannel->data = 04000 + currInPort;
activeChannel->full = TRUE;
return;
}
if (port == lastInPort)
{
return; // No input, leave channel empty
}
continue;
}
pp = portVector + (port - NiuLocalStations);
if (pp->active && (pp->inOutIdx < pp->inInIdx))
{
/*
** Port with active TCP connection has data available
*/
in = pp->inBuffer[pp->inOutIdx++];
if (pp->inOutIdx >= pp->inInIdx)
{
pp->inOutIdx = pp->inInIdx = 0;
}
#if DEBUG_PP
fprintf(niuLog, "\n%010u input byte %d %03o on port %d",
traceSequenceNo, pp->ibytes, in, pp->id);
#endif
// Connection has data -- assemble it and see if we have
// a complete input word
if (pp->ibytes != 0)
{
if ((in & 0200) == 0)
{
// Sequence error, drop the byte
#if DEBUG_PP || DEBUG_NET
fprintf(niuLog, "\n%010u input sequence error, second byte %03o, port %d",
traceSequenceNo, in, port);
#endif
continue;
}
pp->currInput |= (in & 0177);
currInPort = lastInPort = port;
activeChannel->data = 04000 + currInPort;
activeChannel->full = TRUE;
return;
}
else
{
// first byte of key data, save it for later
if ((in & 370) != 0)
{
// sequence error, drop the byte
#if DEBUG_PP || DEBUG_NET
fprintf(niuLog, "\n%010u input sequence error, first byte %03o, port %d",
traceSequenceNo, in, port);
#endif
continue;
}
pp->currInput = in << 7;
pp->ibytes = 1;
}
}
if (port == lastInPort)
{
return; // No input, leave channel empty
}
}
}
// We have a current port, so we already sent the port number;
// now send its data.
if (currInPort < NiuLocalStations)
{
// local input, send a keypress format input word
rp = &localInput[currInPort];
nextget = rp->get + 1;
if (nextget == NiuLocalBufSize)
{
nextget = 0;
}
in = rp->buf[rp->get];
rp->get = nextget;
activeChannel->data = in << 1;
}
else
{
pp = portVector + (currInPort - NiuLocalStations);
activeChannel->data = pp->currInput << 1;
pp->ibytes = 0;
}
activeChannel->full = TRUE;
currInPort = -1;
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on NIU output channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void niuOutIo(void)
{
PpWord d;
int port;
if ((activeDevice->fcode != FcNiuOutput) || !activeChannel->full)
{
return;
}
/*
** Output data.
*/
d = activeChannel->data;
activeChannel->full = FALSE;
if (obytes == 0)
{
// first word of the triple
#if REAL_TIMING
if (frameStart)
{
if (rtcClock - lastFrame < (u32)16667)
{
activeChannel->full = TRUE;
return;
}
lastFrame = rtcClock;
}
frameStart = FALSE;
#endif
#if DEBUG_PP
fprintf(niuLog, "\n%010u output byte %d %04o", traceSequenceNo, obytes, d);
#endif
#if DEBUG_PP || DEBUG_NET
if ((d & 06000) != 04000)
{
fprintf(niuLog, "\n%010u output out of sync, first word %04o",
traceSequenceNo, d);
}
#endif
currOutput = (d & 01777) << 9;
obytes = 1;
return;
}
#if DEBUG_PP
fprintf(niuLog, "\n%010u output byte %d %04o", traceSequenceNo, obytes, d);
#endif
if (obytes == 1)
{
// second word of the triple
#if DEBUG_PP || DEBUG_NET
if ((d & 06001) != 0)
{
fprintf(niuLog, "\n%010u output out of sync, second word %04o",
traceSequenceNo, d);
}
#endif
currOutput |= d >> 1;
obytes = 2;
return;
}
// Third word of the triple
#if DEBUG_PP || DEBUG_NET
if ((d & 04000) != 0)
{
fprintf(niuLog, "\n%010u output out of sync, third word %04o",
traceSequenceNo, d);
}
#endif
#if REAL_TIMING
/*
** If end of frame bit is set, remember that so the next
** output word is recognized as the start of a new frame.
*/
if ((d & 02000) != 0)
{
frameStart = TRUE;
}
#endif
port = (d & 01777);
niuSend(port, currOutput);
obytes = 0;
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void niuActivate(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void niuDisconnect(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Check for I/O availability.
**
** Parameters: Name Description.
** Nothing.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void niuCheckIo(void)
{
PortParam *availablePort;
#if defined(_WIN32)
u_long blockEnable = 1;
#endif
struct sockaddr_in from;
#if defined(_WIN32)
int fromLen;
#else
socklen_t fromLen;
#endif
int i;
int maxFd;
int n;
int optEnable = 1;
PortParam *pp;
fd_set readFds;
struct timeval timeout;
fd_set writeFds;
ioTurns = (ioTurns + 1) % IoTurnsPerPoll;
if (ioTurns != 0)
{
return;
}
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
maxFd = 0;
availablePort = NULL;
for (i = 0, pp = portVector; i < platoConns; i++, pp++)
{
if (pp->active)
{
if (pp->inInIdx < InBufSize)
{
FD_SET(pp->connFd, &readFds);
if (pp->connFd > maxFd)
{
maxFd = pp->connFd;
}
}
if (pp->outInIdx > pp->outOutIdx)
{
FD_SET(pp->connFd, &writeFds);
if (pp->connFd > maxFd)
{
maxFd = pp->connFd;
}
}
}
else if (availablePort == NULL)
{
availablePort = pp;
FD_SET(listenFd, &readFds);
if (listenFd > maxFd)
{
maxFd = listenFd;
}
}
}
if (maxFd < 1)
{
return;
}
timeout.tv_sec = 0;
timeout.tv_usec = 0;
n = select(maxFd + 1, &readFds, &writeFds, NULL, &timeout);
if (n < 1)
{
return;
}
for (i = 0, pp = portVector; i < platoConns; i++, pp++)
{
if (pp->active)
{
if (FD_ISSET(pp->connFd, &readFds))
{
n = recv(pp->connFd, &pp->inBuffer[pp->inInIdx], InBufSize - pp->inInIdx, 0);
if (n > 0)
{
#if DEBUG_NET
fprintf(niuLog, "\n%010u received %d bytes on port %02o",
traceSequenceNo, n, pp->id);
niuLogBytes(&pp->inBuffer[pp->inInIdx], n);
#endif
pp->inInIdx += n;
}
else
{
niuClose(pp);
}
}
if (FD_ISSET(pp->connFd, &writeFds) && (pp->outOutIdx < pp->outInIdx))
{
n = send(pp->connFd, &pp->outBuffer[pp->outOutIdx], pp->outInIdx - pp->outOutIdx, 0);
if (n >= 0)
{
#if DEBUG_NET
fprintf(niuLog, "\n%010u sent %d bytes to port %02o",
traceSequenceNo, n, pp->id);
niuLogBytes(&pp->outBuffer[pp->outOutIdx], n);
#endif
pp->outOutIdx += n;
if (pp->outOutIdx >= pp->outInIdx)
{
pp->outInIdx = 0;
pp->outOutIdx = 0;
}
}
}
}
}
if ((availablePort != NULL) && FD_ISSET(listenFd, &readFds))
{
fromLen = sizeof(from);
availablePort->connFd = accept(listenFd, (struct sockaddr *)&from, &fromLen);
if (availablePort->connFd > 0)
{
availablePort->active = TRUE;
availablePort->inInIdx = 0;
availablePort->inOutIdx = 0;
availablePort->outInIdx = 0;
availablePort->outOutIdx = 0;
/*
** Set Keepalive option so that we can eventually discover if
** a client has been rebooted.
*/
setsockopt(availablePort->connFd, SOL_SOCKET, SO_KEEPALIVE, (void *)&optEnable, sizeof(optEnable));
/*
** Make socket non-blocking.
*/
#if defined(_WIN32)
ioctlsocket(availablePort->connFd, FIONBIO, &blockEnable);
#else
fcntl(availablePort->connFd, F_SETFL, O_NONBLOCK);
#endif
#if DEBUG_NET
fprintf(niuLog, "\n%010u accepted connection on port %02o",
traceSequenceNo, availablePort->id);
#endif
niuWelcome(availablePort->id + NiuLocalStations);
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Close a port and mark it inactive.
**
** Parameters: Name Description.
** pp pointer to mux port parameters.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void niuClose(PortParam *pp)
{
netCloseConnection(pp->connFd);
pp->active = FALSE;
pp->connFd = 0;
#if DEBUG_NET
fprintf(niuLog, "\n%010u connection closed on port %02o",
traceSequenceNo, pp->id);
#endif
}
/*--------------------------------------------------------------------------
** Purpose: Send a welcome message to a station
**
** Parameters: Name Description.
** stat Station number
**
** Returns: nothing.
**
**------------------------------------------------------------------------*/
static void niuWelcome(int stat)
{
char msg[100];
sprintf(msg, "Connected to Plato station %d-%d", stat >> 5, stat & 037);
niuSend(stat, 0042000 + stat); // NOP with station number in it
niuSend(stat, 0100033); // mode 3, mode rewrite, screen
niuSend(stat, 0201200); // load Y = 128
niuSend(stat, 0200200); // load X = 128
niuSendstr(stat, msg);
}
/*--------------------------------------------------------------------------
** Purpose: Send a string to a station
**
** Parameters: Name Description.
** stat Station number
** p pointer to ASCII string
**
** Returns: nothing.
**
**------------------------------------------------------------------------*/
static void niuSendstr(int stat, const char *p)
{
int cc = 2;
int w = 017720;
bool shift = FALSE;
char c;
while ((c = *p++) != 0)
{
if (isupper(c))
{
c = tolower(c);
if (!shift)
{
w = (w << 6 | 077);