-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rconnection2.cpp
1167 lines (1050 loc) · 29.9 KB
/
Rconnection2.cpp
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
/*
* C++ Interface to Rserve
* Copyright (C) 2004-8 Simon Urbanek, All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 2.1 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Leser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Although this code is licensed under LGPL v2.1, we strongly encourage
* everyone modifying this software to contribute back any improvements and
* bugfixes to the project for the benefit all other users. Thank you.
*
* $Id$
*/
/* external defines:
SWAPEND - needs to be defined for platforms with inverse endianess related to Intel
see also SOCK_ERROR, MAIN and other defines in sisocks.h
*/
/* locally generated status error and return codes:
-1 - operation failed (e.g. connect failed)
-2 - handhake failed
-3 - invalid ID string
-4 - protocol not supported
-5 - not connected
-6 - - unused -
-7 - remote connection close
-8 - malformed packet
-9 - send error
-10 - out of memory
-11 - operation is unsupported (e.g. unix login while crypt is not linked)
-12 - eval didn't return a SEXP (possibly the server is too old/buggy or crashed)
*/
#include "Rconnection2.h"
#include "sisocks.h"
#ifdef unix
#include <sys/un.h>
#include <unistd.h>
#else
#define AF_LOCAL -1
#endif
#if defined HAVE_NETINET_TCP_H && defined HAVE_NETINET_IN_H
#define CAN_TCP_NODELAY
#include <netinet/tcp.h>
#include <netinet/in.h>
#endif
#ifdef Win32
#define CAN_TCP_NODELAY
#endif
#include "Rsrv.h"
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
// NOTE: 0103 compatibility has not been established! use at your own risk!
static const char *myID = "Rsrv0103QAP1"; /* this client supports up to protocol version 0103 */
#define IS_LIST_TYPE_(TYPE) ((TYPE) == XT_LIST || (TYPE) == XT_LIST_NOTAG || (TYPE) == XT_LIST_TAG)
#define IS_SYMBOL_TYPE_(TYPE) ((TYPE) == XT_SYM || (TYPE) == XT_SYMNAME)
// [IP] Custom commands
#define CMD_CustomStatus 0x50
#if defined(BUILDING_RCONNECTION2_DLL) && defined(INSTANTIATE_STD_TEMPLATES)
template class RCONNECTION2_API std::basic_string < char >;
template class RCONNECTION2_API std::vector < char>;
template class RCONNECTION2_API std::vector < unsigned int >;
template class RCONNECTION2_API std::vector < std::string >;
template class RCONNECTION2_API std::shared_ptr < Rconnection2::MessageBuffer >;
template class RCONNECTION2_API std::shared_ptr<Rconnection2::Rexp>;
template class RCONNECTION2_API std::weak_ptr<Rconnection2::Rexp>;
#endif
namespace Rconnection2 {
Rmessage::Rmessage()
:
complete_(0),
len_(0)
{
memset(&header_, 0, sizeof(header_));
}
Rmessage::Rmessage(int cmd)
:
complete_(1),
len_(0)
{
memset(&header_, 0, sizeof(header_));
header_.cmd = cmd;
}
Rmessage::Rmessage(int cmd, const char *txt)
:
complete_(1),
len_(0)
{
memset(&header_, 0, sizeof(header_));
int tl = strlen(txt) + 1;
if ((tl & 3) > 0)
tl = (tl + 4) & 0xffffc; // allign the text
len_ = tl + 4; // message length is tl + 4 (short format only)
header_.cmd = cmd;
header_.len = len_;
alloc_data_only(tl + 16);
memset(get_data(), 0, tl + 16);
*((int*)get_data()) = itop(SET_PAR(DT_STRING, tl));
strcpy(get_data() + 4, txt);
}
Rmessage::Rmessage(int cmd, const void *buf, Rsize_t dlen, int raw_data)
:
complete_(1),
len_(0)
{
memset(&header_, 0, sizeof(header_));
len_ = (raw_data) ? dlen : (dlen + 4);
header_.cmd = cmd;
header_.len = len_;
alloc_data_only(len_);
memcpy(get_data(), (raw_data) ? buf : ((char*)buf + 4), dlen);
if (!raw_data)
*((int*)get_data()) = itop(SET_PAR(DT_BYTESTREAM, dlen));
}
Rmessage::Rmessage(int cmd, int i)
:
complete_(1),
len_(0)
{
memset(&header_, 0, sizeof(header_));
len_ = 8; // DT_INT+len (4) + payload-1xINT (4)
header_.cmd = cmd;
header_.len = len_;
alloc_data_only(8);
*((int*)get_data()) = itop(SET_PAR(DT_INT, 4));
((int*)get_data())[1] = itop(i);
}
int Rmessage::read(IRconnection& conn)
{
SOCKET s = conn.getSocket();
complete_ = 0;
int n = recv(s, (char*)&header_, sizeof(header_), 0);
if (n != sizeof(header_))
{
conn.disconnect();
return (n == 0) ? -7 : -8;
}
Rsize_t i = len_ = header_.len = ptoi(header_.len);
header_.cmd = ptoi(header_.cmd);
header_.dof = ptoi(header_.dof);
header_.res = ptoi(header_.res);
if (header_.dof > 0) // skip past DOF if present
{
char sb[256];
int k = header_.dof;
while (k > 0)
{
n = recv(s, sb, (k > 256) ? 256 : k, 0);
if (n < 1)
{
conn.disconnect();
return -8; // malformed packet
}
k -= n;
}
}
if (i > 0)
{
alloc_data_only(i);
char *dp = get_data();
while (i > 0 && (n = recv(s, dp, i, 0)) > 0)
{
dp += n;
i -= n;
}
if (i > 0)
{
conn.disconnect();
return -8;
}
}
parse();
complete_ = 1;
return 0;
}
void Rmessage::parse()
{
par_.clear();
if (len_ < 4) return;
char *c = get_data(), *eop = c + len_;
while (c < eop)
{
int hs = 4;
unsigned int *pp = (unsigned int*)c;
unsigned int p1 = ptoi(pp[0]);
Rsize_t len = p1 >> 8;
if ((p1&DT_LARGE) > 0)
{
hs += 4;
unsigned int p2 = ptoi(pp[1]);
len |= ((Rsize_t)p2) << 24;
}
#ifdef DEBUG_CXX
std::cout << " par " << par_.size() << ": " << (p1 & 0x3f)
<< " length " << len << "\n";
#endif
par_.push_back((unsigned int*)c);
c += hs;
c += len;
}
}
int Rmessage::send(IRconnection& conn)
{
SOCKET s = conn.getSocket();
int failed = 0;
header_.cmd = itop(header_.cmd);
header_.len = itop(header_.len);
header_.dof = itop(header_.dof);
header_.res = itop(header_.res);
if (::send(s, (char*)&header_, sizeof(header_), 0) != sizeof(header_))
failed = -1;
if (!failed && len_ > 0 && (Rsize_t)::send(s, get_data(), len_, 0) != len_)
failed = -1;
header_.cmd = ptoi(header_.cmd);
header_.len = ptoi(header_.len);
header_.dof = ptoi(header_.dof);
header_.res = ptoi(header_.res);
return failed;
}
Rexp::Rexp(const std::shared_ptr<Rmessage>& msg)
:
len_(0),
type_(-1),
data_(NULL),
next_(NULL),
buffer_(msg->get_buffer())
{
#ifdef DEBUG_CXX
std::cout << "new Rexp@" << (void*)this << std::endl;
#endif
int hl=1;
const unsigned int *hp = msg->get_par(0);
Rsize_t plen=hp[0]>>8;
if ((hp[0]&DT_LARGE)>0) {
hl++;
plen|=((Rsize_t)hp[1])<<24;
}
next_ = parseBytes(hp+hl);
}
Rexp::Rexp(const unsigned int *pos, const std::shared_ptr<MessageBuffer>& buffer)
:
len_(0),
type_(-1),
data_(NULL),
next_(NULL),
buffer_(buffer)
{
#ifdef DEBUG_CXX
std::cout << "new Rexp@" << static_cast<void*>(this) << std::endl;
#endif
next_ = parseBytes(pos);
}
Rexp::Rexp(int type, const char *data, Rsize_t len, std::shared_ptr<Rexp> attr)
:
len_(len),
type_(type),
data_(NULL),
next_(NULL),
attr_(attr)
{
#ifdef DEBUG_CXX
std::cout << "new Rexp2@" << static_cast<void*>(this) << std::endl;
#endif
if (len_)
{
buffer_ = std::make_shared<MessageBuffer>(len_);
data_ = buffer_->get<char>();
memcpy(data_, data, len_);
}
next_ = (char*)data + len_;
}
std::shared_ptr<Rexp> Rexp::create(const std::shared_ptr<Rmessage>& msg)
{
int hl = 1;
const unsigned int* d = msg->get_par(0);
Rsize_t plen = d[0] >> 8;
if ((d[0] & DT_LARGE) > 0)
{
hl++;
plen |= ((Rsize_t)d[1]) << 24;
}
return createFromBytes(d + hl, msg->get_buffer());
}
std::shared_ptr<Rexp> Rexp::createFromBytes(const unsigned int* d, const std::shared_ptr<MessageBuffer>& buffer)
{
int type = ptoi(*d) & 0x3f;
#ifdef DEBUG_CXX
std::cout << "new_parsed_Rexp(" << (void*)(d) << ") type=" << type << std::endl;
#endif
std::shared_ptr<Rexp> expr;
switch (type)
{
case XT_ARRAY_INT:
case XT_INT:
{
auto p = Rinteger::create(d, buffer);
expr = std::shared_ptr<Rexp>(p, static_cast<Rexp*>(p.get()));
break;
}
case XT_ARRAY_DOUBLE:
case XT_DOUBLE:
{
auto p = Rdouble::create(d, buffer);
expr = std::shared_ptr<Rexp>(p, static_cast<Rexp*>(p.get()));
break;
}
case XT_VECTOR:
{
auto p = Rvector::create(d, buffer);
expr = std::shared_ptr<Rexp>(p, static_cast<Rexp*>(p.get()));
break;
}
case XT_STR:
{
auto p = Rstring::create(d, buffer);
expr = std::shared_ptr<Rexp>(p, static_cast<Rexp*>(p.get()));
break;
}
case XT_SYM:
case XT_SYMNAME:
{
auto p = Rsymbol::create(d, buffer);
expr = std::shared_ptr<Rexp>(p, static_cast<Rexp*>(p.get()));
break;
}
case XT_ARRAY_STR:
{
auto p = Rstrings::create(d, buffer);
expr = std::shared_ptr<Rexp>(p, static_cast<Rexp*>(p.get()));
break;
}
default:
{
if (IS_LIST_TYPE_(type))
{
auto p = Rlist::create(d, buffer);
expr = std::shared_ptr<Rexp>(p, static_cast<Rexp*>(p.get()));
}
else
expr = Rexp::createFromBytes(d, buffer);
break;
}
}
return expr;
}
char *Rexp::parseBytes(const unsigned int *pos)
{
// plen is not used
int hl = 1;
unsigned int p1 = ptoi(pos[0]);
len_ = p1 >> 8;
if ((p1&XT_LARGE) > 0)
{
hl++;
len_ |= ((Rsize_t)(ptoi(pos[1]))) << 24;
}
data_ = (char*)(pos + hl);
if (p1&XT_HAS_ATTR)
{
attr_ = Rexp::createFromBytes((unsigned int*)data_, buffer_);
len_ -= attr_->next_ - data_;
data_ = attr_->next_;
}
type_ = p1 & 0x3f;
#ifdef DEBUG_CXX
std::cout << "Rexp(type=" << type_ << ", len=" << len_
<< ", attr=" << (void*)attr_.get() << ")\n";
#endif
return data_ + len_;
}
void Rexp::store(char *buf) const
{
int hl = 4;
unsigned int *i = (unsigned int*)buf;
i[0] = SET_PAR(type_, len_);
i[0] = itop(i[0]);
if (len_ > 0x7fffff)
{
buf[0] |= XT_LARGE;
i[1] = itop(len_ >> 24);
hl += 4;
}
memcpy(buf + hl, data_, len_);
}
std::shared_ptr<Rexp> Rexp::attribute(const char *name) const
{
return (attr_ && IS_LIST_TYPE_(attr_->get_type())) ? static_cast<Rlist*>(attr_.get())->entryByTagName(name) : std::shared_ptr<Rexp>();
}
const std::vector<std::string>& Rexp::attributeNames() const
{
if (!attr_ || !IS_LIST_TYPE_(attr_->get_type()))
{
attrnames_.clear();
}
else if (attrnames_.empty())
{
std::shared_ptr<Rexp> L = attr_;
while (L && IS_LIST_TYPE_(L->get_type()))
{
Rlist* LL = static_cast<Rlist*>(L.get());
if (LL->get_tag() && IS_SYMBOL_TYPE_(LL->get_tag()->get_type()))
attrnames_.push_back(std::string(((Rsymbol*)LL->get_tag().get())->symbolName()));
L = LL->get_tail();
}
}
return attrnames_;
}
void Rinteger::fix_content()
{
if (!data_) return;
#ifdef SWAPEND
int *i = (int*) data_;
int *j = (int*) (data_+len_);
while (i<j)
{
*i=ptoi(*i);
i++;
}
#endif
}
void Rdouble::fix_content()
{
if (!data_) return;
#ifdef SWAPEND
double *i = (double*) data_;
double *j = (double*) (data_+len_);
while (i<j)
{
*i=ptod(*i);
i++;
}
#endif
}
void Rsymbol::fix_content()
{
if (type_ == XT_SYM && *data_ == 3) name_ = data_ + 4; // normally the symbol should consist of a string SEXP specifying its name - no further content is defined as of now
if (type_ == XT_SYMNAME) name_ = data_; // symname consists solely of the name
#ifdef DEBUG_CXX
std::cout << "SYM " << (void*)this <<" \"" << name_ << "\"\n";
#endif
}
inline Rsize_t align_length(Rsize_t len)
{
return (((len) + 3L) & (rlen_max ^ 3L));
}
template<typename T>
static inline char* putValue(char* p, T v)
{
memcpy(p, &v, sizeof(v));
return p + sizeof(v);
}
static inline char* putLength(char* p, Rsize_t len)
{
Rsize_t txlen = len - 4;
if (len > 0xfffff0)
{
p = putValue(p, itop(SET_PAR(PAR_TYPE(((unsigned char*) p)[4] | XT_LARGE), txlen & 0xffffff)));
p = putValue(p, itop(txlen >> 24));
}
else p = putValue(p, itop(SET_PAR(PAR_TYPE(ptoi(*p)), txlen)));
return p;
}
std::shared_ptr<Rstrings> Rstrings::create(const std::vector<std::string>& sv)
{
// compute required buffer length
Rsize_t len = 4, padding_len = 0;
for (const auto& s : sv)
len += s.length() + 1 + ((!s.empty() && (unsigned char)s[0] == 0xFF) ? 1 : 0);
if (len > 0xfffff0) len += 4;
while (len & 3) { ++len; ++padding_len; }
#ifdef DEBUG_CXX
std::cout << "Rstrings::create(): BufferLen=" << len << std::endl;
#endif
// allocate buffer
std::shared_ptr<MessageBuffer> buffer = std::make_shared<MessageBuffer>(len);
// put element type
char* p0 = buffer->get<char>();
putValue(p0, static_cast<unsigned>(itop(XT_ARRAY_STR)));
// put total length
char* p = putLength(p0, len);
for (const auto& s : sv)
{
if (!s.empty() && (unsigned char)s[0] == 0xFF) *p++ = (char)0xFF;
strcpy(p, s.c_str());
p += s.length() + 1;
}
for (Rsize_t k = 0; k < padding_len; ++k) *p++ = 1;
return create(buffer->get<unsigned int>(), buffer);
}
void Rstrings::fix_content()
{
cont_.clear();
char *c = data_;
unsigned i = 0;
while (i < len_)
{
char* p = c;
while (*c && i < len_) ++c, ++i;
if (i < len_)
{
cont_.push_back(p);
++c; ++i;
}
}
}
void Rlist::fix_content()
{
char *ptr = data_;
char *eod = data_ + len_;
#ifdef DEBUG_CXX
std::cout << "Rlist::fix_content data_=" << (void*) ptr <<", type=" << type_ <<"\n";
#endif
if (type_ == XT_LIST)
{
/* old-style lists */
head_ = Rexp::createFromBytes((unsigned int*)ptr, buffer_);
if (head_)
{
ptr = head_->get_next();
if (ptr < eod)
{
tail_ = Rexp::createFromBytes((unsigned int*)ptr, buffer_);
if (tail_)
{
ptr = static_cast<Rlist*>(tail_.get())->next_;
if (ptr < eod)
tag_ = Rexp::createFromBytes((unsigned int*)ptr, buffer_);
if (tail_->get_type() != XT_LIST)
tail_.reset();
}
}
}
}
else if (type_ == XT_LIST_NOTAG) /* new style list w/o tags */
{
std::shared_ptr<Rexp> lt = Rexp::shared_from_this();
int n = 0;
while (ptr < eod)
{
std::shared_ptr<Rexp> h = Rexp::createFromBytes((unsigned int*)ptr, buffer_);
if (!h) break;
if (n)
{
static_cast<Rlist*>(lt.get())->tail_.reset((Rexp*)new Rlist(type_, h, 0, h->get_next()));
lt = static_cast<Rlist*>(lt.get())->tail_;
}
else
static_cast<Rlist*>(lt.get())->head_ = h;
n++;
ptr = h->get_next();
}
}
else if (type_ == XT_LIST_TAG) /* new style list with tags */
{
std::shared_ptr<Rexp> lt = Rexp::shared_from_this();
int n = 0;
while (ptr < eod)
{
std::shared_ptr<Rexp> h = Rexp::createFromBytes((unsigned int*)ptr, buffer_);
#ifdef DEBUG_CXX
std::cout << " LIST_TAG: n=" << n <<", ptr=" << (void*)ptr <<", h=" << h <<"\n";
#endif
if (!h) break;
ptr = h->get_next();
std::shared_ptr<Rexp> t = Rexp::createFromBytes((unsigned int*)ptr, buffer_);
#ifdef DEBUG_CXX
std::cout << " tag=" << (void*)t.get() <<" (ptr=" << (void*)ptr <<")\n";
#endif
if (!t) break;
if (n)
{
static_cast<Rlist*>(lt.get())->tail_.reset((Rexp*)new Rlist(type_, h, t, t->get_next()));
lt = static_cast<Rlist*>(lt.get())->tail_;
}
else
{
static_cast<Rlist*>(lt.get())->head_ = h;
static_cast<Rlist*>(lt.get())->tag_ = t;
}
ptr = t->get_next();
n++;
}
next_ = ptr;
}
#ifdef DEBUG_CXX
std::cout << " end of list " << (void*) this << ", ptr=" << (void*)ptr << "\n";
#endif
}
const std::vector<std::string>& Rvector::strings()
{
if (!strs_populated_)
{
for (const auto& p : cont_)
{
if (p->get_type() == XT_STR)
strs_.push_back(static_cast<Rstring*>(p.get())->c_str());
}
strs_populated_ = true;
}
return strs_;
}
size_t Rvector::indexOf(const std::shared_ptr<Rexp>& exp) const
{
auto it = std::find(cont_.begin(), cont_.end(), exp);
return it == cont_.end() ? std::string::npos : std::distance(cont_.begin(), it);
}
size_t Rvector::indexOfString(const char *str) const
{
size_t i = 0;
for (const auto& p : cont_)
{
if (p && p->get_type() == XT_STR && !strcmp(((Rstring*)p.get())->c_str(), str))
return i;
++i;
}
return std::string::npos;
}
int Rstrings::indexOfString(const char *str)
{
for (size_t i = 0, n = cont_.size(); i < n; ++i)
if (!strcmp(cont_[i], str)) return i;
return -1;
}
void Rvector::fix_content()
{
char *ptr = data_;
char *eod = data_ + len_;
cont_.clear();
while (ptr < eod)
{
cont_.push_back(Rexp::createFromBytes((unsigned int*)ptr, buffer_));
if (cont_.back())
ptr = cont_.back()->get_next();
else
break;
}
}
std::shared_ptr<Rexp> Rvector::byName_Rexp(const char *name) const
{
/* here we are not using IS_LIST_TYPE_() because XT_LIST_NOTAG is guaranteed to not match */
if (cont_.empty() || !attr_ || (attr_->get_type() != XT_LIST && attr_->get_type() != XT_LIST_TAG))
return std::shared_ptr<Rexp>();
std::shared_ptr<Rexp> e = ((Rlist*)attr_.get())->get_head();
if (((Rlist*)attr_.get())->get_tag())
e = ((Rlist*)attr_.get())->entryByTagName("names");
if (!e || (e->get_type() != XT_VECTOR && e->get_type() != XT_ARRAY_STR && e->get_type() != XT_STR))
return std::shared_ptr<Rexp>();
if (e->get_type() == XT_VECTOR)
{
size_t pos = ((Rvector*)e.get())->indexOfString(name);
if (pos != std::string::npos && pos < cont_.size()) return cont_[pos];
}
else if (e->get_type() == XT_ARRAY_STR)
{
size_t pos = ((Rstrings*)e.get())->indexOfString(name);
if (pos != std::string::npos && pos < cont_.size()) return cont_[pos];
}
else
{
if (!strcmp(((Rstring*)e.get())->c_str(), name))
return cont_[0];
}
return std::shared_ptr<Rexp>();
}
Rconnection::Rconnection(const char *host, int port)
:
host_(host),
port_(port),
family_((port == -1) ? AF_LOCAL : AF_INET),
s_(-1),
auth_(0)
{
salt_[0] = '.';
salt_[1] = '.';
}
Rconnection::Rconnection(const Rsession& session)
{
const char *sHost = session.host();
if (!sHost) sHost = "127.0.0.1";
host_ = sHost;
port_ = session.port();
family_ = AF_INET;
s_ = -1;
auth_ = 0;
salt_[0] = '.';
salt_[1] = '.';
session_key_.resize(32);
memcpy(&session_key_[0], session.key(), 32);
}
int Rconnection::connect()
{
#ifdef unix
struct sockaddr_un sau;
#endif
SAIN sai;
char IDstring[33];
if (family_ == AF_INET)
{
memset(&sai, 0, sizeof(sai));
build_sin(&sai, (char*)host_.c_str(), port_);
}
else
{
#ifdef unix
memset(&sau, 0, sizeof(sau));
sau.sun_family = AF_LOCAL;
strcpy(sau.sun_path, host_.c_str()); // FIXME: possible overflow!
#else
return -11; // unsupported
#endif
}
IDstring[32] = 0;
int i;
s_ = socket(family_, SOCK_STREAM, 0);
if (family_ == AF_INET)
{
#ifdef CAN_TCP_NODELAY
int opt = 1;
setsockopt(s_, IPPROTO_TCP, TCP_NODELAY, (const char*) &opt, sizeof(opt));
#endif
i = ::connect(s_, (SA*)&sai, sizeof(sai));
}
#ifdef unix
else
i = ::connect(s_, (SA*)&sau, sizeof(sau));
#endif
if (i == -1)
{
disconnect();
return -1; // connect failed
}
if (!session_key_.empty()) // resume a session
{
int n = ::send(s_, &session_key_[0], 32, 0);
if (n != 32)
{
disconnect();
return -2; // handshake failed (session key send error)
}
std::shared_ptr<Rmessage> msg = Rmessage::create();
int q = msg->read(*this);
return q;
}
int n = recv(s_, IDstring, sizeof(IDstring), 0);
if (n != 32)
{
disconnect();
return -2; // handshake failed (no IDstring)
}
if (strncmp(IDstring, myID, 4))
{
disconnect();
return -3; // invalid IDstring
}
if (strncmp(IDstring + 8, myID + 8, 4) || strncmp(IDstring + 4, myID + 4, 4) > 0)
{
disconnect();
return -4; // protocol not supported
}
{
int i = 12;
while (i < 32)
{
if (!strncmp(IDstring + i, "ARuc", 4)) auth_ |= A_required | A_crypt;
if (!strncmp(IDstring + i, "ARpt", 4)) auth_ |= A_required | A_plain;
if (IDstring[i] == 'K')
{
salt_[0] = IDstring[i + 1];
salt_[1] = IDstring[i + 2];
}
i += 4;
}
}
return 0;
}
bool Rconnection::disconnect()
{
#ifdef WIN32
if(s_ != INVALID_SOCKET)
#else
if (s_ > -1)
#endif
{
closesocket(s_);
s_ = -1;
return true;
}
else return false;
}
int Rconnection::getLastSocketError(char* buffer, size_t buffer_len, int options) const
{
return sockerrorchecks(buffer, buffer_len, options);
}
/**--- low-level functions --*/
int Rconnection::request(Rmessage& msg, int cmd, Rsize_t len, void *par)
{
struct phdr ph;
if (s_ == -1) return -5; // not connected
memset(&ph, 0, sizeof(ph));
ph.len = itop(len);
ph.cmd = itop(cmd);
if (send(s_, (char*)&ph, sizeof(ph), 0) != sizeof(ph))
{
disconnect();
return -9;
}
if (len > 0 && send(s_, (char*)par, len, 0) != len)
{
disconnect();
return -9;
}
return msg.read(*this);
}
int Rconnection::request(Rmessage& targetMsg, Rmessage& contents)
{
if (s_ == -1) return -5; // not connected
if (contents.send(*this))
{
disconnect();
return -9; // send error
}
int res = targetMsg.read(*this);
if (res) return res;
return (targetMsg.get_header().cmd & RESP_ERR) == RESP_ERR ? -20 : 0;
}
/** --- high-level functions -- */
int Rconnection::shutdown(const char *key)
{
std::shared_ptr<Rmessage> msg = Rmessage::create();
std::shared_ptr<Rmessage> cmdMessage = key ? Rmessage::create(CMD_shutdown) : Rmessage::create(CMD_shutdown, key);
int res = request(*msg, *cmdMessage);
return res;
}
int Rconnection::assign(const char *symbol, const Rexp& exp)
{
std::shared_ptr<Rmessage> msg = Rmessage::create();
std::shared_ptr<Rmessage> cmdMessage = Rmessage::create(CMD_setSEXP);
int tl = strlen(symbol) + 1;
if (tl & 3) tl = (tl + 4) & 0xfffc;
Rsize_t xl = exp.storageSize();
Rsize_t hl = 4 + tl + 4;
if (xl > 0x7fffff) hl += 4;
cmdMessage->alloc_data(hl + xl);
((unsigned int*)cmdMessage->get_data())[0] = SET_PAR(DT_STRING, tl);
((unsigned int*)cmdMessage->get_data())[0] = itop(((unsigned int*)cmdMessage->get_data())[0]);
strcpy(cmdMessage->get_data() + 4, symbol);
((unsigned int*)(cmdMessage->get_data() + 4 + tl))[0] = SET_PAR((Rsize_t)((xl > 0x7fffff) ? (DT_SEXP | DT_LARGE) : DT_SEXP), (Rsize_t)xl);
((unsigned int*)(cmdMessage->get_data() + 4 + tl))[0] = itop(((unsigned int*)(cmdMessage->get_data() + 4 + tl))[0]);
if (xl > 0x7fffff)
((unsigned int*)(cmdMessage->get_data() + 4 + tl))[1] = itop(xl >> 24);
exp.store(cmdMessage->get_data() + hl);
int res = request(*msg, *cmdMessage);
if (!res)
res = CMD_STAT(msg->command());
return res;
}
int Rconnection::voidEval(const char *cmd)
{
int status = 0;
eval<Rexp>(cmd, &status, 1);
return status;
}
std::shared_ptr<Rexp> Rconnection::eval_to_Rexp(const char *cmd, int *status, int opt)
{
/* opt = 1 -> void eval */
std::shared_ptr<Rmessage> msg = Rmessage::create();
std::shared_ptr<Rmessage> cmdMessage = Rmessage::create((opt & 1) ? CMD_voidEval : CMD_eval, cmd);
int res = request(*msg, *cmdMessage);
if (status) *status = res;
if (res || (opt & 1))
return std::shared_ptr<Rexp>();
else if (msg->get_par_count() != 1 || (ptoi(msg->get_par(0, 0)) & 0x3f) != DT_SEXP)
{
if (status) *status = -12; // returned object is not SEXP
return std::shared_ptr<Rexp>();
}
else
return Rexp::create(msg);
}
/** detached eval (aka detached void eval) initiates eval and detaches the session.
* @param cmd command to evaluate. If NULL equivalent to simple detach()
* @param status optional status to be reported (zero on success)
* @return object describintg he session.
* Note that the caller is responsible for freeing the object if not needed. */
std::shared_ptr<Rsession> Rconnection::detachedEval(const char *cmd, int *status)
{
std::shared_ptr<Rmessage> msg = Rmessage::create();
std::shared_ptr<Rmessage> cmdMessage = cmd
? Rmessage::create(CMD_detachedVoidEval, cmd)
: Rmessage::create(CMD_detachedVoidEval);
int res = request(*msg, *cmdMessage);
if (res)
{
if (status) *status = res;
return 0;