@@ -374,57 +374,90 @@ void context_finalizer(SEXP xptr) {
374374
375375SEXP rnng_protocol_open (SEXP protocol , SEXP raw ) {
376376
377- const int pro = INTEGER ( protocol )[ 0 ] ;
377+ const char * pro = CHAR ( STRING_ELT ( protocol , 0 )) ;
378378 const int rw = LOGICAL (raw )[0 ];
379+
380+ size_t slen = strlen (pro );
381+ const char * bus = "bus" , * pair = "pair" , * push = "push" , * pull = "pull" ,
382+ * pub = "pub" , * sub = "sub" , * req = "req" , * rep = "rep" , * sur = "surveyor" ,
383+ * res = "respondent" ;
384+
379385 nng_socket * sock ;
380- char * pname ;
381- int xc ;
386+ const char * pname ;
387+ int xc = -1 ;
382388 SEXP socket , klass ;
383389
384390 sock = R_Calloc (1 , nng_socket );
385- switch (pro ) {
391+
392+ switch (slen ) {
386393 case 1 :
387- pname = "bus" ;
388- xc = rw ? nng_bus0_open_raw (sock ) : nng_bus0_open (sock );
389- break ;
390394 case 2 :
391- pname = "pair" ;
392- xc = rw ? nng_pair0_open_raw (sock ) : nng_pair0_open (sock );
393- break ;
394395 case 3 :
395- pname = "push" ;
396- xc = rw ? nng_push0_open_raw (sock ) : nng_push0_open (sock );
397- break ;
396+ if (!strncmp (bus , pro , slen )) {
397+ pname = bus ;
398+ xc = rw ? nng_bus0_open_raw (sock ) : nng_bus0_open (sock );
399+ break ;
400+ }
401+ if (slen > 2 ) {
402+ if (!strncmp (pub , pro , slen )) {
403+ pname = pub ;
404+ xc = rw ? nng_pub0_open_raw (sock ) : nng_pub0_open (sock );
405+ break ;
406+ }
407+ if (!strncmp (sub , pro , slen )) {
408+ pname = sub ;
409+ xc = rw ? nng_sub0_open_raw (sock ) : nng_sub0_open (sock );
410+ break ;
411+ }
412+ if (!strncmp (req , pro , slen )) {
413+ pname = req ;
414+ xc = rw ? nng_req0_open_raw (sock ) : nng_req0_open (sock );
415+ break ;
416+ }
417+ if (!strncmp (rep , pro , slen )) {
418+ pname = rep ;
419+ xc = rw ? nng_rep0_open_raw (sock ) : nng_rep0_open (sock );
420+ break ;
421+ }
422+ }
398423 case 4 :
399- pname = "pull" ;
400- xc = rw ? nng_pull0_open_raw (sock ) : nng_pull0_open (sock );
401- break ;
424+ if (slen > 1 ) {
425+ if (!strncmp (pair , pro , slen )) {
426+ pname = pair ;
427+ xc = rw ? nng_pair0_open_raw (sock ) : nng_pair0_open (sock );
428+ break ;
429+ }
430+ if (slen > 2 ) {
431+ if (!strncmp (push , pro , slen )) {
432+ pname = push ;
433+ xc = rw ? nng_push0_open_raw (sock ) : nng_push0_open (sock );
434+ break ;
435+ }
436+ if (!strncmp (pull , pro , slen )) {
437+ pname = pull ;
438+ xc = rw ? nng_pull0_open_raw (sock ) : nng_pull0_open (sock );
439+ break ;
440+ }
441+ }
442+ }
402443 case 5 :
403- pname = "pub" ;
404- xc = rw ? nng_pub0_open_raw (sock ) : nng_pub0_open (sock );
405- break ;
406444 case 6 :
407- pname = "sub" ;
408- xc = rw ? nng_sub0_open_raw (sock ) : nng_sub0_open (sock );
409- break ;
410445 case 7 :
411- pname = "req" ;
412- xc = rw ? nng_req0_open_raw (sock ) : nng_req0_open (sock );
413- break ;
414446 case 8 :
415- pname = "rep" ;
416- xc = rw ? nng_rep0_open_raw (sock ) : nng_rep0_open (sock );
417- break ;
447+ if (slen > 2 && !strncmp (sur , pro , slen )) {
448+ pname = sur ;
449+ xc = rw ? nng_surveyor0_open_raw (sock ) : nng_surveyor0_open (sock );
450+ break ;
451+ }
418452 case 9 :
419- pname = "surveyor" ;
420- xc = rw ? nng_surveyor0_open_raw (sock ) : nng_surveyor0_open (sock );
421- break ;
422453 case 10 :
423- pname = "respondent" ;
424- xc = rw ? nng_respondent0_open_raw (sock ) : nng_respondent0_open (sock );
425- break ;
454+ if (slen > 2 && !strncmp (res , pro , slen )) {
455+ pname = res ;
456+ xc = rw ? nng_respondent0_open_raw (sock ) : nng_respondent0_open (sock );
457+ break ;
458+ }
426459 default :
427- xc = -1 ;
460+ error_return ( "'protocol' should be one of bus, pair, push, pull, pub, sub, req, rep, surveyor, respondent" ) ;
428461 }
429462
430463 if (xc ) {
0 commit comments