-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaarq.cpp
879 lines (705 loc) · 21.9 KB
/
aarq.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
/************************************************************************
* DICOMLIB
* Copyright 2003 Sunnybrook and Women's College Health Science Center
* Implemented by Trevor Morgan ([email protected])
*
* See LICENSE.txt for copyright and licensing info.
*************************************************************************/
#include <algorithm>
#include "aarq.hpp"
#include "UIDs.hpp"
#include "Exceptions.hpp"
using std::for_each;
/*IMPL_VERSION_NAME
There is much repeated functionality in this file,
implying a need for a shared base class.
Some of the size members can be consolidated?
*/
/*
We need a clearer approach to managing message sizes. Generally speaking,
the Size() member function does two things. It sets the Length_ member
and returns a value. Generally Size() is the entire byte-size of the message,
and Length_ is the size of the message minus the initial couple of fields.
(The initial fields generally being 'item type', 'reserved' and 'length'
So Length_ corresponds to the Length_ field, and Size() to the entire
message Size().
There are two problems here from a coding perspective. One is that the
Size() function has non-obvious side effects, and the other is that we
seem to be duplicating information, which is asking for bugs, as has
already occured a few times. My proposal is to lose Length_ completely
as a member, and when it is needed use Size()-(size of initial fields).
I think this would increase data integrity.
IMPORTANT! Are all Size() fields UINT16 ????
NO THEY ARE NOT! EXTRA CAUTION REQUIRED!
*/
/*************************************************************************
*
* Application Context Class
*
************************************************************************/
namespace dicom
{
void EnforceItemType(BYTE Given, BYTE Expected)
{
if(Expected!=Given)
throw BadItemType(Given,Expected);
}
namespace primitive
{
#ifndef _MSC_VER
/*
difference of opinion here between gcc and msvc as to whether constant static members
need to be instantiated seperately.
*/
const BYTE PresentationContext::ItemType_;
const BYTE PresentationContext::Reserved1_;
const BYTE PresentationContext::Reserved2_;
const BYTE PresentationContext::Reserved3_;
const BYTE PresentationContext::Reserved4_;
const BYTE MaximumSubLength::ItemType_;
const BYTE MaximumSubLength::Reserved1_;
const UINT16 MaximumSubLength::Length_;
const BYTE UserInformation::ItemType_;
const BYTE UserInformation::Reserved_;
const BYTE AAssociateRQ::ItemType_;
const BYTE AAssociateRQ::Reserved1_;
const UINT16 AAssociateRQ::ProtocolVersion_;
const UINT16 AAssociateRQ::Reserved2_;
const BYTE ApplicationContext::ItemType_;
const BYTE ApplicationContext::Reserved_;
const BYTE AbstractSyntax::ItemType_;
const BYTE AbstractSyntax::Reserved_;
const BYTE TransferSyntax::ItemType_;
const BYTE TransferSyntax::Reserved_;
const BYTE ImplementationClass::ItemType_;
const BYTE ImplementationClass::Reserved_;
const BYTE ImplementationVersion::ItemType_;
const BYTE ImplementationVersion::Reserved_;
const BYTE SCPSCURoleSelect::ItemType_;
const BYTE SCPSCURoleSelect::Reserved_;
#endif
/*
CRIMINALLY DANGEROUS CODE AHEAD! PLEASE FIX!
*/
namespace
{ // these aren't thread safe, which could lead to horrendous complications if
// you ever trust the data in them. Only use them for dummy data
// that never gets read.
BYTE tmpBYTE;
UINT32 tmpUINT32;
UINT16 tmpUINT16;
}
ApplicationContext::ApplicationContext(const UID &uid)
:UID_(uid)
{
}
UINT32 ApplicationContext::Size()const
{
return ( sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16) + UID_.str().size() );
}
UINT32 ApplicationContext::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
socket >> tmpBYTE;
UINT16 UIDLength;
socket >> UIDLength;
byteread+=sizeof(tmpBYTE)+sizeof(UIDLength);
std::string s(UIDLength, ' ');
socket.Read(s);
byteread+=UIDLength;
UID_=UID(s);
return byteread;
}
void ApplicationContext::Write(Network::Socket& socket)const
{
socket << ItemType_;
socket << Reserved_;
UINT16 Length=UINT16(UID_.str().size()); //--|
socket << Length; //--|these two lines could be amalgamated
socket.Send(UID_.str());
}
/*************************************************************************
*
* Abstract Syntax Class
*
************************************************************************/
AbstractSyntax::AbstractSyntax(const UID &uid)
:UID_(uid)
{}
UINT32 AbstractSyntax::Size()
{
return ( sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16) + UID_.str().size());
}
UINT32 AbstractSyntax::Read(Network::Socket& socket)
{
UINT32 byteread=0;
BYTE b;
socket >> b;
byteread+=sizeof(b);
EnforceItemType(b,ItemType_);
byteread+=ReadDynamic(socket);
return byteread;
}
UINT32 AbstractSyntax::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
socket >> tmpBYTE;
UINT16 Length;
socket >> Length;
byteread+=sizeof(tmpBYTE)+sizeof(Length);
//UID_.assign(Length,' ');
std::string s(Length,' ');
socket.Read(s);
byteread+=Length;
UID_=UID(s);
return byteread;
}
void AbstractSyntax::Write(Network::Socket& socket)
{
socket << ItemType_;
socket << Reserved_;
socket << UINT16(UID_.str().size());
socket.Send(UID_.str());
}
TransferSyntax::TransferSyntax(const UID& uid)
//:ProtocolDataUnit(uid,0x40)
:UID_(uid)
{}
UINT32 TransferSyntax::Size()
{
return ( sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16) + UID_.str().size());
}
void TransferSyntax::Write(Network::Socket& socket)
{
socket << ItemType_;
socket << Reserved_;
socket << UINT16(UID_.str().size());
socket.Send(UID_.str());
}
UINT32 TransferSyntax::Read(Network::Socket& socket)
{
UINT32 byteread=0;
socket >> tmpBYTE;
socket >> tmpBYTE;
byteread+=sizeof(tmpBYTE)+sizeof(tmpBYTE);
UINT16 Length;
socket >> Length;
byteread+=sizeof(Length);
std::string s(Length,' ');
socket.Read(s);
byteread+=Length;
UID_=UID(s);
return byteread;
}
/************************************************************************
*
* Implementation Class
*
************************************************************************/
ImplementationClass::ImplementationClass(const UID &uid)
//:ProtocolDataUnit(uid,0x52)
:UID_(uid)
{}
UINT32 ImplementationClass::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
socket >> tmpBYTE;
UINT16 Length;
socket >> Length;
byteread+=sizeof(tmpBYTE)+sizeof(Length);
std::string s(Length,' ');
socket.Read(s);
byteread+=Length;
UID_=UID(s);
return byteread;
}
UINT32 ImplementationClass::Size()
{
return ( sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16) + UID_.str().size() );
}
void ImplementationClass::Write(Network::Socket& socket)
{
socket << ItemType_;
socket << Reserved_;
socket << UINT16(UID_.str().size());
socket.Send(UID_.str());
}
/************************************************************************
*
* Implementation Version
*
************************************************************************/
UINT32 ImplementationVersion::Size()
{
if(Name.empty())
return 0;//message doesn't get sent if it's empty.
return ( sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16) + Name.size() );
}
UINT32 ImplementationVersion::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
socket >> tmpBYTE;
UINT16 Length;
socket >> Length;
byteread+=sizeof(tmpBYTE)+sizeof(Length);
if(Length>16 )
throw dicom::exception("Implementation Version Length too long.");
Name.assign(Length,' ');
socket.Read(Name);
byteread+=Length;
return byteread;
}
void ImplementationVersion::Write(Network::Socket& socket)
{
if(Name.empty())//message doesn't get sent if it's empty.
return;
socket << ItemType_;
socket << Reserved_;
socket << UINT16(Name.size());
socket.Send(Name);
}
/************************************************************************
*
* Presentation Context
*
************************************************************************/
PresentationContext::PresentationContext(
const AbstractSyntax& Abs
,const std::vector<TransferSyntax>& AcceptableTransferSyntaxes
,BYTE id)
:
ID_(id)
,AbsSyntax_(Abs)
,TransferSyntaxes_(AcceptableTransferSyntaxes)
{
}
/*!
This form defaults to ImplicitVR/Little Endian
*/
PresentationContext::PresentationContext(
const AbstractSyntax& Abs
,BYTE id)
:ID_(id)
,AbsSyntax_(Abs)
{
TransferSyntaxes_.push_back(TransferSyntax(IMPL_VR_LE_TRANSFER_SYNTAX));
}
PresentationContext::PresentationContext()
:ID_(0x00)
,AbsSyntax_(UID(""))
{
}
void PresentationContext::AddTransferSyntax(TransferSyntax& Tran)
{
TransferSyntaxes_.push_back (Tran);
}
void PresentationContext::Write(Network::Socket& socket)
{
socket << ItemType_;
socket << Reserved1_;
socket << Length_;
socket << ID_;
socket << Reserved2_;
socket << Reserved3_;
socket << Reserved4_;
AbsSyntax_.Write(socket);
for_each(TransferSyntaxes_.begin(),TransferSyntaxes_.end(),WriteToSocket(socket));
}
UINT32 PresentationContext::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
TransferSyntax Tran(UID(""));
socket >> tmpBYTE;
socket >> Length_;
socket >> ID_;
socket >> tmpBYTE;
socket >> tmpBYTE;
socket >> tmpBYTE;
byteread+=4*sizeof(tmpBYTE)+sizeof(Length_)+sizeof(ID_);
int Count = Length_ - sizeof(BYTE) - sizeof(BYTE) - sizeof(BYTE) - sizeof(BYTE);
UINT32 tmp_read=AbsSyntax_.Read(socket);
byteread+=tmp_read;
Count = Count - tmp_read;//AbsSyntax_.Size();
//Read in the transfer syntaxes...
while ( Count > 0)
{
//Read<TransferSyntax>(Tran,socket);
tmp_read=Tran.Read(socket);
byteread+=tmp_read;
Count = Count - tmp_read;//Tran.Size();
TransferSyntaxes_.push_back ( Tran );
}
Enforce(0==Count);
return byteread;
}
UINT32 PresentationContext::Size()
{
Length_ = sizeof(BYTE) + sizeof(BYTE) + sizeof(BYTE) + sizeof(BYTE);
Length_ += AbsSyntax_.Size();
size_t Index = 0;
while(Index < TransferSyntaxes_.size() )
{
Length_ += TransferSyntaxes_.at(Index).Size();
++Index;
}
return ( Length_ + sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16));
}
/************************************************************************
*
* Maximum Sub Length
*
************************************************************************/
MaximumSubLength::MaximumSubLength()
{
}
MaximumSubLength::MaximumSubLength(UINT32 Max)
{
MaximumLength_ = Max;
}
void MaximumSubLength::Set(UINT32 Max)
{
MaximumLength_ = Max;
}
UINT32 MaximumSubLength::Get()
{
return ( MaximumLength_ );
}
void MaximumSubLength::Write(Network::Socket& socket)
{
socket << ItemType_;
socket << Reserved1_;
socket << Length_;
socket << MaximumLength_;
//socket.Flush
}
UINT32 MaximumSubLength::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
socket >> tmpBYTE;
UINT16 Length;
socket >> Length;
byteread+=sizeof(tmpBYTE)+sizeof(Length);
if(Length!=Length_)
throw dicom::exception("itemlength of MaximumSubLength must be 0x04");
socket >> MaximumLength_;
byteread+=sizeof(MaximumLength_);
return byteread;
}
UINT32 MaximumSubLength::Size()
{
return ( Length_ + sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16));
}
/******** SCP / SCU Role Select ***********/
SCPSCURoleSelect::SCPSCURoleSelect()
:UID_("")
{}
void SCPSCURoleSelect::Write(Network::Socket& socket)
{
socket << ItemType_;
socket << Reserved_;
socket << ItemLength;
socket << static_cast<UINT16>(UID_.str().size());
socket.Send(UID_.str());
socket << SCURole_;
socket << SCPRole_;
}
UINT32 SCPSCURoleSelect::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
socket >> tmpBYTE;
socket >> ItemLength;
byteread+=sizeof(tmpBYTE)+sizeof(ItemLength);
UINT16 UIDLength;
socket >> UIDLength;
byteread+=sizeof(UIDLength);
std::string s(UIDLength,' ');
socket.Read(s);
byteread+=UIDLength;
UID_=UID(s);
socket >> SCURole_;
socket >> SCPRole_;
byteread+=sizeof(SCURole_)+sizeof(SCPRole_);
return byteread;
}
//this should be fixed too FIXME
UINT32 SCPSCURoleSelect::Size()
{
ItemLength = sizeof(UINT16) + UID_.str().size() + sizeof(BYTE) + sizeof(BYTE);
return ( ItemLength + sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16) );
}
/************************************************************************
*
* User Information
*
************************************************************************/
UserInformation::UserInformation()
:ImpClass_(UID(""))
,UserInfoBaggage_ (0)
{
}
//do we ever use this?
void UserInformation::SetMax(MaximumSubLength &Max)
{
MaxSubLength_ = Max;
}
//UINT32 UserInformation::GetMax()
//{
// return(MaxSubLength.Get());
//}
void UserInformation::Write(Network::Socket& socket)
{
socket << ItemType_;
socket << Reserved_;
socket << UINT16 (Size()-4);
//socket << Length_;//I think this should be a call to Size()
MaxSubLength_.Write(socket);
ImpClass_.Write(socket);
//should only send this if it really exists...
ImpVersion_.Write(socket);
/*
Note that we don't currently support writing the
SCPSCURoleSelect Sub-item (which is optional.)
*/
}
UINT32 UserInformation::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
UINT32 tmp_read=0;
UserInfoBaggage_ = 0;
UINT16 length;
socket >> tmpBYTE;
socket >> length;
byteread+=sizeof(tmpBYTE)+sizeof(length);
int BytesLeftToRead = length;
while(BytesLeftToRead > 0)
{
socket >> tmpBYTE;
BytesLeftToRead-=sizeof(tmpBYTE);
byteread+=sizeof(tmpBYTE);
switch ( tmpBYTE )
{
case 0x51:
tmp_read=MaxSubLength_.ReadDynamic(socket);
byteread+=tmp_read;
BytesLeftToRead = BytesLeftToRead - tmp_read;//MaxSubLength_.Size();
break;
case 0x52:
tmp_read=ImpClass_.ReadDynamic(socket);
byteread+=tmp_read;
BytesLeftToRead=BytesLeftToRead-tmp_read;/*ImpClass_.Size()*/;
break;
case 0x54: // Role selection
/*
This is very ugly, the use of UserInfoBaggage_ is not a nice idea.
It would be better to do some clever trick in SCPSCURole to manage
optionality, maybe.
*/
tmp_read=SCPSCURole_.ReadDynamic(socket);
byteread+=tmp_read;
BytesLeftToRead = BytesLeftToRead - tmp_read;//SCPSCURole_.Size();
UserInfoBaggage_ += SCPSCURole_.Size();
break;
case 0x55:
tmp_read=ImpVersion_.ReadDynamic(socket);//optional!
byteread+=tmp_read;
BytesLeftToRead = BytesLeftToRead - tmp_read;//ImpVersion_.Size();
break;
default:
throw BadItemType(tmpBYTE,0);
}
}
//Sam comments out these two enforce because the first never happens(or trapped in the while loop
//The second one may not be true because the UID in ImpClass_ can be odd number. -Sam 28Nov2007
//Enforce(0==BytesLeftToRead,"0!=BytesLeftToRead");
//Enforce((Size()-4)==length,"bad size in UserInformation");
return byteread;
}
/*
This has caused us some serious headaches. The Size() member function
of our various message classes MUST be re-thought, it's unmaintainable
in it's current form and has been the root of a number of problems.
We need a more intelligent, unified way of creating and serializing these
message classes.
____ ____________ ____ ____________ ____
|data| <--> |MessageClass| <--> |Wire| <--> |MessageClass| <--> |data|
---- ------------ ---- ------------ ----
One approach would be to insist that each message has two constructors.
One taking the input data, one taking a socket.
class AAssociateRQ
{
AAssociateRQ(socket& s);
AAssociateRQ(some data);
Write(socket);
}
Alternatively, the messages would be data only, and the read/write
functions would be seperate. This might actually make more sense!
struct AAssociateRQ;
Read(socket,AAssociateRQ);
Write(socket,AAssociateRQ);
Most writes require size information, which needs to be either available
or calculatable. I'm not sure that we should ever hold onto this.
Sometimes the information is redundant, or always a constant.
Currently the Size() member function both modifies state and returns
information. This seriously violates the no-side-effects rule.
In determining size we could either call recursive Size() functions
on the objects, or write all the data to some kind of a buffer
and get the size of that???
1)
AAssociateRQ request(data);
request.Write(socket);
and Size() never modifies data?
We should maybe branch to try this out
*/
UINT16 UserInformation::Size()
{
UINT16 length = MaxSubLength_.Size();
length += ImpClass_.Size();
/*
now ImpVersion_ and SCPSCURoleSelect are both optional. This fact needs to be
taken into account when sending and receiving.
*/
length += ImpVersion_.Size();
// Length_=length;//should this be before previous line?
length+=UserInfoBaggage_;//need to do this better.
//problem is, SCP/SCU role is an optional sub-item
return length+4;
//return ( Length_ + UserInfoBaggage + sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT16));
}
/************************************************************************
*
* AAssociateRQ Packet
*
************************************************************************/
AAssociateRQ::AAssociateRQ()
:AppContext_(APPLICATION_CONTEXT)
{
std::fill(Reserved3_,Reserved3_+32,0);
}
AAssociateRQ::AAssociateRQ(const std::string& CallingAp, const std::string& CalledAp)
:CalledAppTitle_(CalledAp)
,CallingAppTitle_(CallingAp)
,AppContext_(APPLICATION_CONTEXT)
{
std::fill(Reserved3_,Reserved3_+32,0);
}
//!This should be in the constructor I think
void AAssociateRQ::SetUserInformation(UserInformation& User)
{
UserInfo_ = User;
}
void AAssociateRQ::Write(Network::Socket& socket)
{
Size();
socket << ItemType_;
socket << Reserved1_;
socket << UINT32(Size()-6);//Length_;
socket << ProtocolVersion_;
socket << Reserved2_;
char StringBuffer[16];
std::fill(StringBuffer,StringBuffer+16,' ');
std::copy(CalledAppTitle_.begin(),CalledAppTitle_.end(),StringBuffer);
socket.Send( StringBuffer, StringBuffer+16);
std::fill(StringBuffer,StringBuffer+16,' ');
std::copy(CallingAppTitle_.begin(),CallingAppTitle_.end(),StringBuffer);
socket.Send( StringBuffer,StringBuffer+ 16);
socket.Send( Reserved3_,Reserved3_+ 32);
AppContext_.Write(socket);
for_each(ProposedPresentationContexts_.begin(),ProposedPresentationContexts_.end(),WriteToSocket(socket));
UserInfo_.Write(socket);
}
UINT32 AAssociateRQ::Read(Network::Socket& socket)
{
UINT32 byteread=0;
BYTE b;
socket >> b;
byteread+=sizeof(b);
EnforceItemType(b,ItemType_);
byteread+=ReadDynamic(socket);
return byteread;
}
UINT32 AAssociateRQ::ReadDynamic(Network::Socket& socket)
{
UINT32 byteread=0;
UINT32 tmp_read=0;
BYTE TempByte;
socket >> tmpBYTE;
UINT32 length;
socket >> length;
socket >> tmpUINT16;
//check protocol version...
socket >> tmpUINT16;
byteread+=sizeof(tmpBYTE)+sizeof(length)+2*sizeof(tmpUINT16);
char StringBuffer[17];
StringBuffer[16] = '\0';
socket.Read(StringBuffer,StringBuffer+16);
byteread+=16*sizeof(char);
//socket.Readn(StringBuffer,16);
CalledAppTitle_=std::string(StringBuffer);
StripTrailingWhitespace(CalledAppTitle_);
//socket.Readn((BYTE *) CallingApTitle, 16);
//socket.Readn(StringBuffer,16);
socket.Read(StringBuffer,StringBuffer+16);
byteread+=16*sizeof(char);
CallingAppTitle_=std::string(StringBuffer);
StripTrailingWhitespace(CallingAppTitle_);
//socket.Readn(/*(BYTE *)*/ Reserved3_, 32);
socket.Read(Reserved3_,Reserved3_+32);
byteread+=32*sizeof(Reserved3_);
//CalledApTitle[16] = '\0';
//CallingApTitle[16] = '\0';
int BytesLeftToRead = length - sizeof(UINT16) - sizeof(UINT16) - 16 - 16 - 32;
while(BytesLeftToRead > 0)
{
socket >> TempByte;
BytesLeftToRead-=sizeof(TempByte);
byteread+=sizeof(TempByte);
switch(TempByte)
{
case 0x50: // user information
tmp_read=UserInfo_.ReadDynamic(socket);
byteread+=tmp_read;
BytesLeftToRead = BytesLeftToRead - tmp_read;//UserInfo_.Size();// - UserInfo_.UserInfoBaggage;
break;
case 0x20:
{
PresentationContext PresContext;//I'd rather these two lines were compacted to one
tmp_read=PresContext.ReadDynamic(socket);//using a constructor.
byteread+=tmp_read;
BytesLeftToRead = BytesLeftToRead - tmp_read;//PresContext.Size();
ProposedPresentationContexts_.push_back(PresContext);
}
break;
case 0x10:
{
ApplicationContext app_context(APPLICATION_CONTEXT);
tmp_read=app_context.ReadDynamic(socket);
byteread+=tmp_read;
BytesLeftToRead = BytesLeftToRead - tmp_read;//app_context.Size();
if(app_context.UID_!=APPLICATION_CONTEXT)
throw exception("Unsupported Application Context");//this should be throw AssociationException(...)
}
break;
default:
throw BadItemType(tmpBYTE,0);
}
}
if(BytesLeftToRead)
throw dicom::exception("BytesLeftToRead!=0 at end of aarq read." );
return byteread;
}
UINT32 AAssociateRQ::Size()
{
UINT32 length;
length = sizeof(UINT16) + sizeof(UINT16) + 16 + 16 + 32;
length += AppContext_.Size();
size_t Index = 0;
while(Index < ProposedPresentationContexts_.size())
{
length += ProposedPresentationContexts_[Index].Size();
++Index;
}
length += UserInfo_.Size();
return ( length + sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT32) );
}
}//namespace primitive
}//namespace dicom