57
57
58
58
#define CONN_CLIENT 000001
59
59
#define CONN_SERVER 000002
60
+ #define CONN_SENT_RTS 000010
61
+ #define CONN_SENT_STR 000020
60
62
#define CONN_GOT_RTS 000100
61
63
#define CONN_GOT_STR 000200
62
64
#define CONN_GOT_SOCKET 000400
83
85
unsigned flags ;
84
86
int listen ;
85
87
struct { int link , size ; uint32_t lsock , rsock ; } rcv , snd ;
88
+ void (* rrp_callback ) (int );
89
+ void (* rfnm_callback ) (int );
86
90
} connection [CONNECTIONS ];
87
91
88
92
struct
@@ -122,6 +126,48 @@ static const char *type_name[] =
122
126
static uint8_t packet [200 ];
123
127
static uint8_t app [200 ];
124
128
129
+ static void when_rrp (int i , void (* cb ) (int ))
130
+ {
131
+ connection [i ].rrp_callback = cb ;
132
+ }
133
+
134
+ static void check_rrp (int host )
135
+ {
136
+ void (* cb ) (int );
137
+ int i ;
138
+ for (i = 0 ; i < CONNECTIONS ; i ++ ) {
139
+ if (connection [i ].host != host )
140
+ continue ;
141
+ if (connection [i ].rrp_callback == NULL )
142
+ continue ;
143
+ cb = connection [i ].rrp_callback ;
144
+ connection [i ].rrp_callback = NULL ;
145
+ cb (i );
146
+ }
147
+ }
148
+
149
+ static void when_rfnm (int i , void (* cb ) (int ))
150
+ {
151
+ connection [i ].rfnm_callback = cb ;
152
+ }
153
+
154
+ static void check_rfnm (int host )
155
+ {
156
+ void (* cb ) (int );
157
+ int i ;
158
+ for (i = 0 ; i < CONNECTIONS ; i ++ ) {
159
+ if (connection [i ].host != host )
160
+ continue ;
161
+ if (connection [i ].rfnm_callback == NULL )
162
+ continue ;
163
+ if (hosts [connection [i ].host ].outstanding_rfnm >= 4 )
164
+ continue ;
165
+ cb = connection [i ].rfnm_callback ;
166
+ connection [i ].rfnm_callback = NULL ;
167
+ cb (i );
168
+ }
169
+ }
170
+
125
171
static int find_link (int host , int link )
126
172
{
127
173
int i ;
@@ -188,6 +234,8 @@ static void destroy (int i)
188
234
connection [i ].rcv .lsock = connection [i ].rcv .rsock =
189
235
connection [i ].snd .lsock = connection [i ].snd .rsock = 0 ;
190
236
connection [i ].flags = 0 ;
237
+ connection [i ].rrp_callback = NULL ;
238
+ connection [i ].rfnm_callback = NULL ;
191
239
}
192
240
193
241
static void send_imp (int flags , int type , int destination , int link , int id ,
@@ -504,6 +552,7 @@ static int process_rts (uint8_t source, uint8_t *data)
504
552
connection [i ].snd .lsock ,
505
553
connection [i ].snd .rsock ,
506
554
connection [i ].snd .size );
555
+ connection [i ].flags |= CONN_SENT_STR ;
507
556
} else if ((i = find_snd_sockets (source , lsock , rsock )) != -1 ) {
508
557
/* There already exists a connection for this socket pair, which
509
558
means an STR was sent previously. */
@@ -527,6 +576,7 @@ static int process_rts (uint8_t source, uint8_t *data)
527
576
connection [i ].snd .lsock ,
528
577
connection [i ].snd .rsock ,
529
578
connection [i ].snd .size );
579
+ connection [i ].flags |= CONN_SENT_STR ;
530
580
maybe_reply (i );
531
581
} else {
532
582
/* There is no connection for this socket pair. */
@@ -543,6 +593,7 @@ static int process_rts (uint8_t source, uint8_t *data)
543
593
connection [i ].snd .lsock ,
544
594
connection [i ].snd .rsock ,
545
595
connection [i ].snd .size );
596
+ connection [i ].flags |= CONN_SENT_STR ;
546
597
}
547
598
548
599
return 9 ;
@@ -597,6 +648,7 @@ static int process_str (uint8_t source, uint8_t *data)
597
648
connection [i ].rcv .lsock ,
598
649
connection [i ].rcv .rsock ,
599
650
connection [i ].rcv .link );
651
+ connection [i ].flags |= CONN_SENT_RTS ;
600
652
maybe_reply (i );
601
653
} else {
602
654
/* There is no connection for this socket pair. */
@@ -614,6 +666,7 @@ static int process_str (uint8_t source, uint8_t *data)
614
666
connection [i ].rcv .lsock ,
615
667
connection [i ].rcv .rsock ,
616
668
connection [i ].rcv .link );
669
+ connection [i ].flags |= CONN_SENT_RTS ;
617
670
}
618
671
619
672
return 9 ;
@@ -671,6 +724,52 @@ static int process_cls (uint8_t source, uint8_t *data)
671
724
return 8 ;
672
725
}
673
726
727
+ static void send_rts (int i )
728
+ {
729
+ if (connection [i ].flags & CONN_SENT_RTS )
730
+ return ;
731
+ fprintf (stderr , "NCP: Send ICP RTS %u:%u link %d.\n" ,
732
+ connection [i ].rcv .lsock , connection [i ].rcv .rsock ,
733
+ connection [i ].rcv .link );
734
+ ncp_rts (connection [i ].host , connection [i ].rcv .lsock ,
735
+ connection [i ].rcv .rsock , connection [i ].rcv .link );
736
+ connection [i ].flags |= CONN_SENT_RTS ;
737
+ }
738
+
739
+ static void send_str_and_rts (int i )
740
+ {
741
+ if ((connection [i ].flags & CONN_SENT_STR ) == 0 ) {
742
+ fprintf (stderr , "NCP: Send ICP STR %u:%u byte size %d.\n" ,
743
+ connection [i ].snd .lsock , connection [i ].snd .rsock ,
744
+ connection [i ].snd .size );
745
+ ncp_str (connection [i ].host , connection [i ].snd .lsock ,
746
+ connection [i ].snd .rsock , connection [i ].snd .size );
747
+ connection [i ].flags |= CONN_SENT_STR ;
748
+ }
749
+ when_rfnm (i , send_rts );
750
+ check_rfnm (connection [i ].host );
751
+ }
752
+
753
+ static void send_cls_snd (int i )
754
+ {
755
+ fprintf (stderr , "NCP: Close ICP %u:%u link %d.\n" ,
756
+ connection [i ].snd .lsock , connection [i ].snd .rsock ,
757
+ connection [i ].snd .link );
758
+ ncp_cls (connection [i ].host ,
759
+ connection [i ].snd .lsock , connection [i ].snd .rsock );
760
+ connection [i ].snd .size = connection [i ].rcv .size = -1 ;
761
+ }
762
+
763
+ static void send_cls_rcv (int i )
764
+ {
765
+ fprintf (stderr , "NCP: Close ICP %u:%u link %d.\n" ,
766
+ connection [i ].rcv .lsock , connection [i ].rcv .rsock ,
767
+ connection [i ].rcv .link );
768
+ ncp_cls (connection [i ].host ,
769
+ connection [i ].rcv .lsock , connection [i ].rcv .rsock );
770
+ connection [i ].snd .size = connection [i ].rcv .size = -1 ;
771
+ }
772
+
674
773
static int process_all (uint8_t source , uint8_t * data )
675
774
{
676
775
int i , j ;
@@ -708,21 +807,9 @@ static int process_all (uint8_t source, uint8_t *data)
708
807
fprintf (stderr , "NCP: New connection %d.\n" , j );
709
808
connection [j ].snd .size = 8 ;
710
809
connection [j ].rcv .link = 44 ;
711
- fprintf (stderr , "NCP: Send ICP STR %u:%u byte size %d.\n" ,
712
- connection [j ].snd .lsock , connection [j ].snd .rsock ,
713
- connection [j ].snd .size );
714
- ncp_str (connection [j ].host , connection [j ].snd .lsock ,
715
- connection [j ].snd .rsock , connection [j ].snd .size );
716
- fprintf (stderr , "NCP: Send ICP RTS %u:%u link %d.\n" ,
717
- connection [j ].rcv .lsock , connection [j ].rcv .rsock ,
718
- connection [j ].rcv .link );
719
- ncp_rts (connection [j ].host , connection [j ].rcv .lsock ,
720
- connection [j ].rcv .rsock , connection [j ].rcv .link );
721
- fprintf (stderr , "NCP: Close ICP %u:%u link %d.\n" ,
722
- connection [i ].snd .lsock , connection [i ].snd .rsock , connection [i ].snd .link );
723
- ncp_cls (connection [i ].host ,
724
- connection [i ].snd .lsock , connection [i ].snd .rsock );
725
- connection [i ].snd .size = connection [i ].rcv .size = -1 ;
810
+ when_rfnm (j , send_str_and_rts );
811
+ when_rfnm (i , send_cls_snd );
812
+ check_rfnm (source );
726
813
}
727
814
return 7 ;
728
815
}
@@ -877,6 +964,7 @@ static int process_rrp (uint8_t source, uint8_t *data)
877
964
{
878
965
fprintf (stderr , "NCP: recieved RRP from %03o.\n" , source );
879
966
hosts [source ].flags |= HOST_ALIVE ;
967
+ check_rrp (source );
880
968
return 0 ;
881
969
}
882
970
@@ -956,7 +1044,7 @@ static void process_regular (uint8_t *packet, int length)
956
1044
if (connection [i ].flags & CONN_CLIENT ) {
957
1045
uint32_t s = sock (& packet [9 ]);
958
1046
fprintf (stderr , "NCP: ICP link %u socket %u.\n" , link , s );
959
- ncp_cls ( source , connection [ i ]. rcv . lsock , connection [ i ]. rcv . rsock );
1047
+ when_rfnm ( i , send_cls_rcv );
960
1048
connection [i ].snd .rsock = s ;
961
1049
962
1050
j = find_rcv_sockets (source , connection [i ].rcv .lsock + 2 , s + 1 );
@@ -969,23 +1057,11 @@ static void process_regular (uint8_t *packet, int length)
969
1057
connection [j ].snd .size = 8 ;
970
1058
connection [j ].rcv .link = 45 ;
971
1059
fprintf (stderr , "NCP: New connection %d.\n" , j );
972
- fprintf (stderr , "NCP: Send ICP STR %u:%u byte size %d.\n" ,
973
- connection [j ].snd .lsock , connection [j ].snd .rsock ,
974
- connection [j ].snd .size );
975
- ncp_str (connection [j ].host ,
976
- connection [j ].snd .lsock ,
977
- connection [j ].snd .rsock ,
978
- connection [j ].snd .size );
979
- fprintf (stderr , "NCP: Send ICP RTS %u:%u link %d.\n" ,
980
- connection [j ].rcv .lsock , connection [j ].rcv .rsock ,
981
- connection [j ].rcv .link );
982
- ncp_rts (connection [j ].host ,
983
- connection [j ].rcv .lsock ,
984
- connection [j ].rcv .rsock ,
985
- connection [j ].rcv .link );
1060
+ when_rfnm (j , send_str_and_rts );
986
1061
}
987
1062
connection [j ].listen = connection [i ].rcv .rsock ;
988
1063
connection [j ].flags |= CONN_GOT_SOCKET ;
1064
+ check_rfnm (connection [j ].host );
989
1065
maybe_reply (j );
990
1066
991
1067
return ;
@@ -1028,6 +1104,7 @@ static void process_rfnm (uint8_t *packet, int length)
1028
1104
fprintf (stderr , "NCP: Ready for next message to host %03o link %u.\n" ,
1029
1105
host , packet [2 ]);
1030
1106
hosts [host ].outstanding_rfnm -- ;
1107
+ check_rfnm (host );
1031
1108
}
1032
1109
1033
1110
static void process_full (uint8_t * packet , int length )
@@ -1177,6 +1254,13 @@ static void app_echo (void)
1177
1254
ncp_eco (host , app [2 ]);
1178
1255
}
1179
1256
1257
+ static void app_open_send_rts (int i )
1258
+ {
1259
+ // Send first ICP RTS.
1260
+ ncp_rts (connection [i ].host , connection [i ].rcv .lsock ,
1261
+ connection [i ].rcv .rsock , connection [i ].rcv .link );
1262
+ }
1263
+
1180
1264
static void app_open (void )
1181
1265
{
1182
1266
uint32_t socket ;
@@ -1187,22 +1271,22 @@ static void app_open (void)
1187
1271
fprintf (stderr , "NCP: Application open socket %u on host %03o.\n" ,
1188
1272
socket , host );
1189
1273
1190
- if ((hosts [host ].flags & HOST_ALIVE ) == 0 ) {
1191
- // We haven't communicated with this host yet.
1192
- ncp_rst (host );
1193
- // Wait for RRP.
1194
- }
1195
-
1196
1274
// Initiate a connection.
1197
1275
i = make_open (host , 1002 , socket , 0 , 0 );
1198
1276
connection [i ].rcv .link = 42 ; //Receive link.
1199
1277
connection [i ].flags |= CONN_CLIENT ;
1200
1278
memcpy (& connection [i ].client .addr , & client , len );
1201
1279
connection [i ].client .len = len ;
1202
1280
1203
- // Send first ICP RTS.
1204
- ncp_rts (connection [i ].host , connection [i ].rcv .lsock ,
1205
- connection [i ].rcv .rsock , connection [i ].rcv .link );
1281
+ if ((hosts [host ].flags & HOST_ALIVE ) == 0 ) {
1282
+ // We haven't communicated with this host yet.
1283
+ ncp_rst (host );
1284
+ // Wait for RRP, then send RTS.
1285
+ when_rrp (i , app_open_send_rts );
1286
+ } else {
1287
+ // Ok to send RTS directly.
1288
+ app_open_send_rts (i );
1289
+ }
1206
1290
}
1207
1291
1208
1292
static void app_listen (void )
0 commit comments