forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CUSM.Mod
1332 lines (1235 loc) · 34.3 KB
/
CUSM.Mod
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
(* OBERON System 3, Release 2.3.
Copyright 1999 ETH Zürich Institute for Computer Systems,
ETH Center, CH-8092 Zürich. e-mail: [email protected].
This module may be used under the conditions of the general Oberon
System 3 license contract. The full text can be downloaded from
"ftp://ftp.inf.ethz.ch/pub/software/Oberon/System3/license.txt;A"
Under the license terms stated it is in particular (a) prohibited to modify
the interface of this module in any way that disagrees with the style
or content of the system and (b) requested to provide all conversions
of the source code to another platform with the name OBERON. *)
MODULE CUSM; (** portable *) (* CU-SeeMe video receiver / Luca Balestra 13.7.95 *)
(* ejz 29.8.95, DOS version *)
IMPORT NetSystem, Texts, Oberon, Out, Objects, Gadgets, BasicGadgets, Pictures, SYSTEM, RembrandtDocs, Display,
Math, Modules;
(* pjm 01.07.96 - seems to assume Oberon.Time returns ms? *)
CONST
MAXSNDBUFLEN = 500; (* max 37 requests 56+37*12=500 *)
MAXRCVBUFLEN = 2048;
VPHLEN = 26; (* VideoPackeHeader length *)
OCDLEN = 30; (* OpenContinueData length *)
CILEN = 12; (* ClientInfo length *)
(* family*)
kGroup=0; kClient=1; kReflect=2;
(* port *)
VIDPORT=7648; SENDPORT=6666;
(* message *)
kOpenConnection=1; kCloseConnection=6;
(* dataType *)
kConfigVideoType=101;
kMessageType1=104; kMessageType2=105;
kHalfSizeVideoType=1; kFullSizeVideoType=2;
(* flags *)
RECAUDIO=1;
AUDIOCAPABLE=2;
WANTLURCKERS=4;
PCCLIENT=8;
PRIVATEAUDIO=16;
WANTVERSION=32;
(* misc *)
MSECINDAY=86400000; (* mseconds in a day *)
TYPE
Addr=RECORD
family: INTEGER;
port: INTEGER;
addr: LONGINT;
END;
Routing=RECORD
dest: Addr;
src: Addr;
END;
VideoPacketHeader = POINTER TO VideoPacketHeaderDesc;
VideoPacketHeaderDesc=RECORD
routing: Routing;
seqNum: LONGINT;
message: INTEGER;
dataType: INTEGER;
len: INTEGER;
END;
OpenContinueData = POINTER TO OpenContinueDataDesc;
OpenContinueDataDesc=RECORD
clientCount: INTEGER;
seqNum: LONGINT;
name: ARRAY 20 OF CHAR;
sendMode: CHAR;
recvMode: CHAR;
flags: CHAR;
version: CHAR;
END;
PacketLossData = RECORD
packetsSent: INTEGER;
packetsRecv: INTEGER;
END;
ClientInfo = RECORD
clientIp: LONGINT;
flags: CHAR;
aux: CHAR;
iWillRecv: CHAR;
iWillSend: CHAR;
loss: PacketLossData;
END;
Image= POINTER TO ImageDesc;
ImageDesc = RECORD
width: INTEGER;
height: INTEGER;
picture: Pictures.Picture;
END;
Client = POINTER TO ClientDesc;
ClientDesc = RECORD
next: Client;
clAdr: LONGINT;
clAdrStr: ARRAY 16 OF CHAR;
name: ARRAY 256 OF CHAR;
lastSeqNum: LONGINT;
sndBufOffset: INTEGER;
sender: BOOLEAN;
timeOut: SHORTINT;
wantVideo:BOOLEAN;
image: Image;
END;
Connection = POINTER TO ConnectionDesc;
ConnectionDesc = RECORD
reflAdr: LONGINT;
reflAdrStr: ARRAY 16 OF CHAR;
confID: INTEGER;
connected:BOOLEAN;
timeOut: INTEGER;
sndBuf: ARRAY MAXSNDBUFLEN OF CHAR;
sndBufLen: INTEGER;
vphSeqNum:INTEGER;
ocdSeqNum:INTEGER;
timeToSend,timeToUpdate: LONGINT;
clientsList: Client;
next: Connection;
END;
VAR
connectionsList:Connection;
currentConnection: Connection;
hostAdr, reflAdr: LONGINT;
rcvBuf: ARRAY MAXRCVBUFLEN OF CHAR; (* buffers to receive *)
sndTask, rcvTask: Oberon.Task;
rcvScktStarted, sndScktStarted:BOOLEAN;
sndSocket,rcvSocket: NetSystem.Socket;
rcvVPH: VideoPacketHeader;
rcvOCD: OpenContinueData;
reflAdrStr: BasicGadgets.String;
conferenceID: BasicGadgets.Integer;
hostName: BasicGadgets.String;
connectionsLog, videosLog*: Texts.Text;
(* IMAGE DECODE*)
intraRowBytes: ARRAY 16 OF INTEGER;
interRowDiff: ARRAY 16 OF LONGINT;
wordDiff: ARRAY 256 OF INTEGER;
longDiff: ARRAY 256 OF LONGINT;
conv4To8Bit: ARRAY 256 OF INTEGER;
(* imAdr,imSize: LONGINT;*)
lumin,contr: INTEGER; (* luminosity and contrast *)
gamma: REAL;
(******* GADGETS ************)
PROCEDURE ReflAdrStrHandler* (F: Objects.Object; VAR M: Objects.ObjMsg);
VAR temp: BasicGadgets.String;
BEGIN
IF M IS Objects.AttrMsg THEN
WITH M: Objects.AttrMsg DO
IF (M.id = Objects.get) & (M.name = "Gen") THEN
M.class := Objects.String; M.s := "CUSM.NewReflAdrStr";
M.res := 0
ELSE BasicGadgets.StringHandler(F, M)
END
END
ELSIF M IS Objects.FileMsg THEN
WITH M: Objects.FileMsg DO
IF M.id = Objects.load THEN
NEW(temp);
BasicGadgets.StringHandler(temp,M)
ELSE BasicGadgets.StringHandler(F,M)
END
END
ELSE BasicGadgets.StringHandler(F, M)
END
END ReflAdrStrHandler;
PROCEDURE ConfIDHandler* (F: Objects.Object; VAR M: Objects.ObjMsg);
VAR temp:BasicGadgets.Integer;
BEGIN
IF M IS Objects.AttrMsg THEN
WITH M: Objects.AttrMsg DO
IF (M.id = Objects.get) & (M.name = "Gen") THEN
M.class := Objects.String; M.s := "CUSM.NewConfID";
M.res := 0
ELSE BasicGadgets.IntegerHandler(F, M)
END
END
ELSIF M IS Objects.FileMsg THEN
WITH M: Objects.FileMsg DO
IF M.id = Objects.load THEN
NEW(temp);
BasicGadgets.IntegerHandler(temp,M)
ELSE BasicGadgets.IntegerHandler(F,M)
END
END
ELSE BasicGadgets.IntegerHandler(F, M)
END
END ConfIDHandler;
PROCEDURE HostNameHandler* (F: Objects.Object; VAR M: Objects.ObjMsg);
VAR temp:BasicGadgets.String;
BEGIN
IF M IS Objects.AttrMsg THEN
WITH M: Objects.AttrMsg DO
IF (M.id = Objects.get) & (M.name = "Gen") THEN
M.class := Objects.String; M.s := "CUSM.NewHostName";
M.res := 0
ELSE BasicGadgets.StringHandler(F, M)
END
END
ELSIF M IS Objects.FileMsg THEN
WITH M: Objects.FileMsg DO
IF M.id = Objects.load THEN
NEW(temp);
BasicGadgets.StringHandler(temp,M)
ELSE BasicGadgets.StringHandler(F,M)
END
END
ELSE BasicGadgets.StringHandler(F, M)
END
END HostNameHandler;
PROCEDURE ConnHandler*(F:Objects.Object; VAR M: Objects.ObjMsg);
VAR temp: Texts.Text;
BEGIN
IF M IS Objects.AttrMsg THEN
WITH M: Objects.AttrMsg DO
IF (M.id = Objects.get) & (M.name = "Gen") THEN
M.class := Objects.String; M.s := "CUSM.NewConnLog";
M.res := 0
ELSE Texts.Handle(F, M)
END
END
ELSIF M IS Objects.FileMsg THEN
WITH M: Objects.FileMsg DO
IF M.id = Objects.load THEN
NEW(temp);
Texts.Handle(temp,M)
ELSE Texts.Handle(F,M)
END
END
ELSE Texts.Handle(F, M)
END
END ConnHandler;
PROCEDURE VideosHandler*(F:Objects.Object; VAR M: Objects.ObjMsg);
VAR temp: Texts.Text;
BEGIN
IF M IS Objects.AttrMsg THEN
WITH M: Objects.AttrMsg DO
IF (M.id = Objects.get) & (M.name = "Gen") THEN
M.class := Objects.String; M.s := "CUSM.NewVideosLog";
M.res := 0
ELSE Texts.Handle(F, M)
END
END
ELSIF M IS Objects.FileMsg THEN
WITH M: Objects.FileMsg DO
IF M.id = Objects.load THEN
NEW(temp);
Texts.Handle(temp,M)
ELSE Texts.Handle(F,M)
END
END
ELSE Texts.Handle(F, M)
END
END VideosHandler;
PROCEDURE NewReflAdrStr*;
BEGIN
Objects.NewObj:=reflAdrStr
END NewReflAdrStr;
PROCEDURE NewConfID*;
BEGIN
Objects.NewObj:=conferenceID
END NewConfID;
PROCEDURE NewHostName*;
BEGIN
Objects.NewObj:=hostName
END NewHostName;
PROCEDURE NewConnLog*;
BEGIN
Objects.NewObj:=connectionsLog
END NewConnLog;
PROCEDURE NewVideosLog*;
BEGIN
Objects.NewObj:=videosLog
END NewVideosLog;
(********** END GADGETS ***********)
(********** VIDEO DECODE **********)
PROCEDURE SetPalette;
VAR i,c,d,inc:INTEGER;
BEGIN
c:=lumin+contr;
inc:=contr DIV 16;
i:=20;
WHILE c > lumin DO
d:=SHORT(ENTIER((Math.exp(gamma*Math.ln(c)))));
Display.SetColor(i,d,d,d);
c:=c-inc;
INC(i);
END
END SetPalette;
PROCEDURE InitVideo;
VAR i,j,c,d,m,u:INTEGER;
a,b:LONGINT;
digit: ARRAY 8 OF LONGINT;
BEGIN
(* init intraRowBytes *)
intraRowBytes[0]:=-1;
FOR i:=0 TO 4 DO
intraRowBytes[i*3 + 1]:=-1;
intraRowBytes[i*3 + 2]:=0;
intraRowBytes[i*3 + 3]:=1;
END;
(* init interRowDiff *)
interRowDiff[0]:=0;
a:=0DDDDDDDEH;
FOR i:= 0 TO 4 DO
b:=a;
FOR j:=1 TO 3 DO
interRowDiff[i*3 + j]:=b;
b:= b + 11111111H;
END;
a:= a + 11111111H
END;
(* init wordDiff *)
u:=0; d:=0; c:=0; m:=0;
FOR i:=0 TO 255 DO;
wordDiff[i]:=((m*16 + c)*16 + d)*16 + u;
INC(u);
IF u > 3 THEN u:=0; INC(d) END;
IF d > 3 THEN d:=0; INC(c) END;
IF c > 3 THEN c:=0; INC(m) END;
END;
(* init longDiff *)
FOR j:=0 TO 7 DO digit[j]:=0 END; (* digit[0] is MSB *)
FOR i:=0 TO 255 DO
longDiff[i]:=0;
FOR j:=0 TO 7 DO
longDiff[i] := longDiff[i]*16+digit[j]
END;
INC(digit[7]);
FOR j:=0 TO 6 DO
IF digit[7-j] > 1 THEN digit[7-j]:=0; INC(digit[7-(j+1)]) END
END
END;
(* init conv4To8Bit and color + 20 *)
FOR i:=0 TO 15 DO
FOR j:=0 TO 15 DO
conv4To8Bit[i*16+j]:= (i+20)+(j+20)*256
END
END;
lumin:=0;
contr:=255;
gamma:=1;
SetPalette; (* sets colors from 20 to 35 *)
END InitVideo;
PROCEDURE Decode*( VAR image: Image; VAR data: ARRAY OF CHAR; pos:INTEGER; imType: INTEGER; name:ARRAY OF CHAR);
VAR
dataLim: INTEGER;
width,height: INTEGER;
ok: BOOLEAN;
rowData: LONGINT;
(* imPos: LONGINT;*)
x, y, minx, miny, maxx, maxy: INTEGER;
PROCEDURE DecodeRow(code:INTEGER);
VAR
i,col,intra: INTEGER;
temp:LONGINT;
cols:INTEGER;
BEGIN
IF code=0 THEN
rowData:=0;
FOR i:=0 TO 3 DO
cols:=ORD(data[pos+i]);
(*SYSTEM.PUT(imPos,conv4To8Bit[cols]);*)
Pictures.Dot(image.picture, conv4To8Bit[cols] MOD 256, x+2*i, y, Display.replace);
Pictures.Dot(image.picture, conv4To8Bit[cols] DIV 256, x+2*i+1, y, Display.replace);
rowData:=rowData*256 + LONG(cols);
(* INC(imPos,2);*)
END;
INC(pos,4);
DEC(y);
(* imPos:=imPos-image.width-8 *)
ELSE
rowData:=rowData-interRowDiff[code];
intra:=intraRowBytes[code];
IF intra > 0 THEN
rowData:=rowData + LONG( wordDiff[ORD(data[pos])] )* 65536 + LONG( wordDiff[ORD(data[pos+1])]);
INC(pos,2);
ELSIF intra = 0 THEN
rowData:=rowData+ longDiff[ORD(data[pos])];
INC(pos)
END;
temp:=rowData;
(* INC(imPos,6);*)
FOR i:=0 TO 3 DO
col:=SHORT(temp MOD 256);
IF temp < 0 THEN
temp := MAX(LONGINT)+temp+1; (* put away sign bit *)
temp := temp DIV 256 + 800000H; (* shift, 800000H = sign bit shifted of 8 bit *)
ELSE
temp:=temp DIV 256
END;
Pictures.Dot(image.picture, conv4To8Bit[col] MOD 256, x+7-2*i-1, y, Display.replace);
Pictures.Dot(image.picture, conv4To8Bit[col] DIV 256, x+7-2*i, y, Display.replace);
(*SYSTEM.PUT(imPos,conv4To8Bit[col]); DEC(imPos,2) *)
END;
DEC(y);
(* imPos:=imPos-image.width+2 *)
END
END DecodeRow;
PROCEDURE DecodeSquare( );
VAR
square: INTEGER;
(* x,y: INTEGER; (* square coord. *)*)
codePos,code: INTEGER;
i: INTEGER;
BEGIN
IF dataLim - pos < 6 THEN ok:=FALSE END;
square:= ORD(data[pos])*256 + ORD(data[pos+1]); INC(pos,2); (* square number *)
IF square > LONG(image.height) * LONG(image.width) DIV 64 THEN ok:=FALSE END; (* 300 or 1200 square/image *)
x:=square MOD (image.width DIV 8);
y:=square DIV (image.width DIV 8);
(* imPos:=imAdr + x*8 + imSize - y*8*LONG(image.width); (* position in memory bitmap *)*)
x := x*8;
IF x < minx THEN
minx := x
END;
IF x > maxx THEN
maxx := x
END;
y := image.height-y*8-1;
IF y < miny THEN
miny := y
END;
IF y > maxy THEN
maxy := y
END;
codePos:=pos;
INC(pos,4);
rowData:=88888888H;
FOR i:=1 TO 4 DO
code:=ORD(data[codePos]); INC(codePos);
DecodeRow(code DIV 16);
DecodeRow(code MOD 16)
END
END DecodeSquare;
BEGIN
IF imType = 1 THEN
width:=160;
height:=120
ELSIF imType = 2 THEN
width:=320;
height:=240
ELSE
Out.String("imType not found in Video.Decode"); Out.Ln
END;
IF image= NIL THEN
NEW(image);
NEW(image.picture);
image.width:=width;
image.height:=height;
Pictures.Create(image.picture,width,height,8);
RembrandtDocs.OpenPict(image.picture,name)
ELSE
IF (width # image.width) OR (height # image.height) THEN
Pictures.Create(image.picture,width,height,8);
HALT(99)
END
END;
dataLim:=ORD(data[pos])*256 + ORD(data[pos+1]) + 2 + pos;
pos:=pos+2;
ok:=TRUE;
(* imAdr:=Pictures.Address(image.picture);
imSize:=LONG(image.width)*(LONG(image.height)-1);*)
minx := image.width;
maxx := 0;
miny := image.height;
maxy := 0;
WHILE ok & (pos < dataLim) DO
DecodeSquare()
END;
IF pos # dataLim THEN Out.String("Video data error") END;
Pictures.Update(image.picture, minx, miny-8, maxx+8, maxy)
END Decode;
PROCEDURE BlackPicture(im: Image);
BEGIN
IF (im # NIL) & (im.picture # NIL) THEN
Pictures.ReplConst(im.picture, Display.FG, 0, 0, im.width, im.height, Display.replace);
Pictures.Update(im.picture, 0, 0, im.width, im.height)
END
END BlackPicture;
PROCEDURE SetLum*;
VAR s:Texts.Scanner;
BEGIN
Texts.OpenScanner(s, Oberon.Par.text, Oberon.Par.pos);
Texts.Scan(s);
IF s.class = Texts.Int THEN
IF contr+s.i > 255 THEN
Out.String("Invalid value of luminosity"); Out.Ln
ELSE
lumin:=SHORT(s.i);
SetPalette;
END
END
END SetLum;
PROCEDURE SetContr*;
VAR s:Texts.Scanner;
BEGIN
Texts.OpenScanner(s, Oberon.Par.text, Oberon.Par.pos);
Texts.Scan(s);
IF s.class = Texts.Int THEN
IF lumin+s.i > 255 THEN
Out.String("Invalid value of contrast"); Out.Ln
ELSE
contr:=SHORT(s.i);
SetPalette
END
END
END SetContr;
PROCEDURE SetGamma*;
VAR s:Texts.Scanner;
BEGIN
Texts.OpenScanner(s, Oberon.Par.text, Oberon.Par.pos);
Texts.Scan(s);
IF s.class = Texts.Real THEN
IF (s.x >= 0.6) & (s.x <= 1.1) THEN
gamma:=s.x;
SetPalette
ELSE
Out.String("Invalid value of gamma correction"); Out.Ln
END
ELSE
Out.String("Value must be Real"); Out.Ln
END
END SetGamma;
(*********** END VIDEO ********)
PROCEDURE StartSndTask;
VAR res: INTEGER;
BEGIN
IF ~sndScktStarted THEN
NetSystem.OpenSocket(sndSocket, SENDPORT, res);
IF res # NetSystem.done THEN;
Out.String("Error in StartSndSocket"); Out.Ln;
HALT(99)
END;
Oberon.Install(sndTask);
sndScktStarted:=TRUE
END
END StartSndTask;
PROCEDURE StopSndTask;
BEGIN
IF sndScktStarted THEN
NetSystem.CloseSocket(sndSocket);
Oberon.Remove(sndTask);
sndScktStarted:=FALSE
END
END StopSndTask;
PROCEDURE StartRcvTask;
VAR res: INTEGER;
BEGIN
IF ~ rcvScktStarted THEN
NetSystem.OpenSocket(rcvSocket, VIDPORT, res);
IF res # NetSystem.done THEN;
Out.String("Error in StartRcvSocket"); Out.Ln;
HALT(99)
END;
Oberon.Install(rcvTask);
rcvScktStarted:=TRUE
END
END StartRcvTask;
PROCEDURE StopRcvTask;
BEGIN
IF rcvScktStarted THEN
NetSystem.CloseSocket(rcvSocket);
Oberon.Remove(rcvTask);
rcvScktStarted:=FALSE
END
END StopRcvTask;
PROCEDURE ConvIpAdrLtoS(adr: LONGINT; VAR ip: ARRAY OF CHAR);
VAR k: INTEGER;
PROCEDURE Write(i: LONGINT);
VAR
p, n: LONGINT;
notFirstZero: BOOLEAN;
BEGIN
notFirstZero:=FALSE;
p := 100;
WHILE p > 0 DO
n := i DIV p; i := i MOD p;
IF ((n # 0) OR notFirstZero) OR (p = 1) THEN
notFirstZero:=TRUE;
ip[k] := CHR(n + ORD("0"));
INC(k)
END;
p := p DIV 10
END
END Write;
BEGIN
k := 0;
Write(adr MOD 256); adr := adr DIV 256; ip[k] := "."; INC(k);
Write(adr MOD 256); adr := adr DIV 256; ip[k] := "."; INC(k);
Write(adr MOD 256); adr := adr DIV 256; ip[k] := "."; INC(k);
Write(adr MOD 256); ip[k] := 0X
END ConvIpAdrLtoS;
PROCEDURE UpdateConnLog;
VAR
cur: Connection;
w: Texts.Writer;
BEGIN
Texts.OpenWriter(w);
IF connectionsList # NIL THEN
cur:= connectionsList;
WHILE cur # NIL DO
IF cur.connected THEN
IF cur = currentConnection THEN
Texts.SetColor(w,1)
ELSE
Texts.SetColor(w,15)
END;
Texts.Write(w, 22X);
Texts.WriteString(w,cur.reflAdrStr); Texts.Write(w, 22X);
Texts.WriteLn(w)
END;
cur:=cur.next
END
END;
Texts.Delete(connectionsLog,0,connectionsLog.len);
Texts.Append(connectionsLog, w.buf)
END UpdateConnLog;
PROCEDURE UpdateVideoStreamsLog;
VAR
w1,w2:Texts.Writer;
cur: Client;
BEGIN
Texts.OpenWriter(w1);
Texts.OpenWriter(w2);
IF currentConnection # NIL THEN
cur:=currentConnection.clientsList;
IF cur = NIL THEN
Texts.WriteString(w1,"NO SENDERS"); Texts.WriteLn(w1)
ELSE
WHILE cur # NIL DO
IF cur.sender THEN
IF cur.image # NIL THEN
Texts.SetColor(w1,15);
Texts.WriteString(w1,cur.name); Texts.WriteString(w1," : ");
Texts.SetColor(w1,1);
Texts.Write(w1, 22X); Texts.WriteString(w1,cur.clAdrStr); Texts.Write(w1, 22X);
Texts.WriteLn(w1)
ELSE
Texts.SetColor(w2,15);
Texts.WriteString(w2,cur.name); Texts.WriteString(w2," : ");
Texts.SetColor(w2,15);
Texts.Write(w2, 22X); Texts.WriteString(w2,cur.clAdrStr); Texts.Write(w2, 22X);
Texts.WriteLn(w2)
END
END;
cur:=cur.next
END
END
END;
Texts.Delete(videosLog,0,videosLog.len);
Texts.Append(videosLog,w1.buf);
Texts.Append(videosLog,w2.buf)
END UpdateVideoStreamsLog;
PROCEDURE SearchConnection(rAddr: LONGINT): Connection;
VAR cur: Connection;
BEGIN
IF connectionsList # NIL THEN
cur:=connectionsList;
WHILE cur # NIL DO
IF cur.reflAdr = rAddr THEN RETURN cur END;
cur:=cur.next
END
END;
RETURN NIL
END SearchConnection;
PROCEDURE RemoveConnection(con: Connection);
VAR
cur:Connection;
curCl: Client;
BEGIN
curCl:=con.clientsList;
WHILE curCl # NIL DO
IF curCl.image# NIL THEN BlackPicture(curCl.image) END;
curCl:=curCl.next
END;
IF con = currentConnection THEN
currentConnection:=NIL;
UpdateVideoStreamsLog
END;
cur:=connectionsList;
IF cur=con THEN
connectionsList:=con.next
ELSE
WHILE cur.next # con DO cur:=cur.next END;
cur.next:=cur.next.next
END;
UpdateConnLog;
IF connectionsList = NIL THEN
StopSndTask; StopRcvTask
END
END RemoveConnection;
PROCEDURE SearchClientByAdr(con:Connection; ipAdr: LONGINT): Client;
VAR cur: Client;
BEGIN
cur:=con.clientsList;
WHILE cur # NIL DO
IF cur.clAdr = ipAdr THEN RETURN cur END;
cur:=cur.next
END;
RETURN NIL
END SearchClientByAdr;
PROCEDURE SearchClientByStrAdr(con:Connection; adr:ARRAY OF CHAR): Client;
VAR cur: Client;
BEGIN
IF con # NIL THEN
cur:=con.clientsList;
WHILE cur # NIL DO
IF cur.clAdrStr= adr THEN RETURN cur END;
cur:=cur.next
END
END;
RETURN NIL
END SearchClientByStrAdr;
PROCEDURE RemoveClient(con: Connection; cl: Client);
VAR cur:Client;
BEGIN
IF con#NIL THEN
cur:=con.clientsList;
IF cur=cl THEN
con.clientsList:=cl.next
ELSE
WHILE cur.next # cl DO cur:=cur.next END;
IF cur.next.image # NIL THEN
BlackPicture(cur.next.image)
END;
IF cur.next.sender THEN
IF con = currentConnection THEN
cur.next:=cur.next.next;
UpdateVideoStreamsLog
ELSE
cur.next:=cur.next.next
END
END
END
END;
END RemoveClient;
PROCEDURE Turn(ipAdr:LONGINT):LONGINT;
VAR
buf:ARRAY 4 OF CHAR;
pos:INTEGER;
BEGIN
pos:=0;
NetSystem.PutLInt(buf,pos,ipAdr); INC(pos, 4);
ipAdr:=ORD(buf[3])*256+ ORD(buf[2]);
ipAdr:=(ipAdr*256 + ORD(buf[1]))*256 + ORD(buf[0]);
RETURN ipAdr
END Turn;
PROCEDURE FullSndBuf(con: Connection; conferenceID: INTEGER; VAR hostName: ARRAY OF CHAR);
VAR
pos,i: INTEGER;
ch:CHAR;
BEGIN
pos:= 0;
NetSystem.PutInt(con.sndBuf,pos,kReflect); INC(pos, 2); (* dest.family *)
NetSystem.PutInt(con.sndBuf,pos,conferenceID); INC(pos, 2); (* dest.port *)
NetSystem.PutLInt(con.sndBuf,pos,0); INC(pos, 4); (* dest.addr -> used only to send to a particular client *)
NetSystem.PutInt(con.sndBuf,pos,kClient); INC(pos, 2); (* src.family *)
NetSystem.PutInt(con.sndBuf,pos,VIDPORT); INC(pos, 2); (* src.port for receiving packets *)
NetSystem.PutLInt(con.sndBuf,pos,hostAdr); INC(pos, 4); (* src.addr *)
NetSystem.PutLInt(con.sndBuf,pos,0); INC(pos, 4); (* sequence number of sended packe *)
NetSystem.PutInt(con.sndBuf,pos,kOpenConnection); INC(pos, 2); (* message *)
NetSystem.PutInt(con.sndBuf,pos,kConfigVideoType); INC(pos, 2); (* dataType *)
NetSystem.PutInt(con.sndBuf,pos,VPHLEN + OCDLEN); INC(pos, 2); (* length *)
NetSystem.PutInt(con.sndBuf,pos,0); INC(pos, 2); (* clientCount *)
NetSystem.PutLInt(con.sndBuf,pos,0); INC(pos, 4); (* seqNum of OpenContinueData *)
i:=0; (* name *)
ch:=hostName[i];
WHILE (ch # 0X) & (i < 19) DO
con.sndBuf[pos+i+1]:=ch;
INC(i);
ch:=hostName[i]
END;
pos:=32;
con.sndBuf[pos]:=CHR(i);
pos:=52;
con.sndBuf[pos]:=CHR(0); INC(pos); (* sendMode *)
con.sndBuf[pos]:=CHR(10); INC(pos); (* recvMode *)
con.sndBuf[pos]:=CHR(PCCLIENT); INC(pos); (* flags *)
con.sndBuf[pos]:=CHR(5); (* version 5 *)
con.sndBufLen:=VPHLEN+OCDLEN
END FullSndBuf;
PROCEDURE InsertClientInfo(con:Connection; ipAdr: LONGINT ;recv: BOOLEAN);
VAR
ccl: LONGINT;
pos,p,cci:INTEGER;
src: Client;
BEGIN
src:=SearchClientByAdr(con,ipAdr);
IF src # NIL THEN
IF src.sender THEN
IF src.sndBufOffset = 0 THEN
pos:=con.sndBufLen;
src.sndBufOffset:=pos;
INC(con.sndBufLen,12);
p:=24; NetSystem.PutInt(con.sndBuf,p,con.sndBufLen); INC(p, 2); (* update length in sndBuf *)
p:=VPHLEN; (* inc clientCount *)
NetSystem.GetInt(con.sndBuf,p,cci); INC(p, 2);
INC(cci);
p:=VPHLEN;
NetSystem.PutInt(con.sndBuf,p,cci); INC(p, 2)
ELSE
pos:=src.sndBufOffset
END;
NetSystem.PutLInt(con.sndBuf,pos,ipAdr); INC(pos, 4);
con.sndBuf[pos]:=CHR(1); INC(pos); (* flags -> 1: update video *)
con.sndBuf[pos]:=CHR(0); INC(pos); (* aux *)
IF recv THEN con.sndBuf[pos]:=CHR(1) ELSE con.sndBuf[pos]:=CHR(0) END; INC(pos); (* i WillRecv *)
con.sndBuf[pos]:=CHR(0); INC(pos); (* iWillSend *)
NetSystem.PutInt(con.sndBuf,pos,0); INC(pos, 2); (* packets sent *)
NetSystem.PutInt(con.sndBuf,pos,1000); INC(pos, 2); (* packets received *)
ELSE
Out.String("This source is not a sender"); Out.Ln
END;
p:=VPHLEN+2; (* inc OCDSeqNum *)
NetSystem.GetLInt(con.sndBuf,p,ccl); INC(p, 4);
INC(ccl);
p:=VPHLEN+2;
NetSystem.PutLInt(con.sndBuf,p,ccl); INC(p, 4)
ELSE
Out.String("Client not found"); Out.Ln
END
END InsertClientInfo;
PROCEDURE ReadVPH(VAR vph: VideoPacketHeader; VAR pos: INTEGER);
BEGIN
NetSystem.GetInt(rcvBuf,pos,vph.routing.dest.family); INC(pos, 2);
NetSystem.GetInt(rcvBuf,pos,vph.routing.dest.port); INC(pos, 2);
NetSystem.GetLInt(rcvBuf,pos,vph.routing.dest.addr); INC(pos, 4);
NetSystem.GetInt(rcvBuf,pos,vph.routing.src.family); INC(pos, 2);
NetSystem.GetInt(rcvBuf,pos,vph.routing.src.port); INC(pos, 2);
NetSystem.GetLInt(rcvBuf,pos,vph.routing.src.addr); INC(pos, 4);
NetSystem.GetLInt(rcvBuf,pos,vph.seqNum); INC(pos, 4);
NetSystem.GetInt(rcvBuf,pos,vph.message); INC(pos, 2);
NetSystem.GetInt(rcvBuf,pos,vph.dataType); INC(pos, 2);
NetSystem.GetInt(rcvBuf,pos,vph.len); INC(pos, 2)
END ReadVPH;
PROCEDURE ReadOCD(VAR ocd: OpenContinueData; VAR pos: INTEGER);
VAR i:INTEGER;
BEGIN
NetSystem.GetInt(rcvBuf,pos,ocd.clientCount); INC(pos, 2);
NetSystem.GetLInt(rcvBuf,pos,ocd.seqNum); INC(pos, 4);
ocd.name[0]:=rcvBuf[pos];
FOR i:=1 TO ORD(ocd.name[0]) DO
ocd.name[i]:=rcvBuf[pos+i]
END;
pos:=pos+20;
ocd.sendMode:=rcvBuf[pos]; INC(pos);
ocd.recvMode:=rcvBuf[pos]; INC(pos);
ocd.version:=rcvBuf[pos]; INC(pos)
END ReadOCD;
PROCEDURE UpdateClientsList(con:Connection);
VAR cur: Client;
BEGIN
cur:=con.clientsList;
WHILE cur # NIL DO
INC(cur.timeOut);
IF cur.timeOut > 10 THEN RemoveClient(con,cur) END;
cur:=cur.next
END;
END UpdateClientsList;
PROCEDURE Sender(me: Oberon.Task);
VAR
t:LONGINT;
pos:INTEGER;
curCon: Connection;
BEGIN
t:=Oberon.Time();
curCon:=connectionsList;
WHILE curCon # NIL DO
IF (t > curCon.timeToSend) & (ABS(curCon.timeToSend - t) < 50000) THEN (* ! midnight ! *)
curCon.timeToSend:=(t+3000) MOD MSECINDAY ;
NetSystem.SendDG(sndSocket, SYSTEM.VAL(NetSystem.IPAdr, curCon.reflAdr), VIDPORT, 0, curCon.sndBufLen, curCon.sndBuf);
INC(curCon.vphSeqNum);
pos:=16;
NetSystem.PutLInt(curCon.sndBuf,pos,curCon.vphSeqNum); INC(pos, 4)
END;
IF (t > curCon.timeToUpdate) & ( ABS(curCon.timeToUpdate - t) < 50000) THEN (* ! midnight ! *)
UpdateClientsList(curCon);
INC(curCon.timeOut);
IF curCon.timeOut > 10 THEN
RemoveConnection(curCon);
Out.String("No respons from reflector "); Out.String(curCon.reflAdrStr); Out.Ln
END;
curCon.timeToUpdate := (curCon.timeToUpdate + 1000) MOD MSECINDAY
END;
curCon:=curCon.next
END;
END Sender;
PROCEDURE OutMsg(mLen: INTEGER; pos: INTEGER);
VAR
w: Texts.Writer;
i:INTEGER;
BEGIN
Texts.OpenWriter(w);
FOR i := 0 TO mLen-1 DO
Texts.Write(w,rcvBuf[pos+i])
END;
Texts.WriteLn(w); Texts.WriteLn(w);
Texts.Append(Oberon.Log,w.buf)
END OutMsg;
PROCEDURE Receiver(me: Oberon.Task);
VAR
rAddr,len,t: LONGINT;
i, msgLen, pos, rPort: INTEGER;
src: Client;
curCon:Connection;
BEGIN
IF NetSystem.AvailableDG(rcvSocket) > 0 THEN
len:=MAXRCVBUFLEN;
t := 0;
NetSystem.ReceiveDG(rcvSocket, SYSTEM.VAL(NetSystem.IPAdr, rAddr), rPort, t, len, rcvBuf);
pos:=0;
curCon:=SearchConnection(rAddr);
IF curCon # NIL THEN
curCon.timeOut:=0;
ReadVPH(rcvVPH,pos);
CASE rcvVPH.dataType OF
kMessageType2: INC(pos,2);
NetSystem.GetInt(rcvBuf,pos,msgLen); INC(pos, 2);
OutMsg(msgLen,pos);
IF curCon.connected = FALSE THEN
curCon.connected:=TRUE;
UpdateConnLog
END;
|kMessageType1: INC(pos,2);
msgLen:=ORD(rcvBuf[pos]); INC(pos);
OutMsg(msgLen,pos);
RemoveConnection(curCon);
|kConfigVideoType:
IF curCon.connected = FALSE THEN
curCon.connected:=TRUE;
UpdateConnLog
END;