-
Notifications
You must be signed in to change notification settings - Fork 1
/
DicomNet.cpp
199 lines (165 loc) · 4.21 KB
/
DicomNet.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
#include "stdafx.h"
#include "DicomNet.h"
string DicomNet::logfile = _T("DcmNetlog.log");
DicomNet::DicomNet()
{
timeout = 1000;
m_bConnected = false;
sprintf(localhost, "127.0.0.1");
}
DicomNet::~DicomNet()
{
}
// 连接服务器
bool DicomNet::Connect()
{
if (m_bConnected)
{
return true;
//DisConnect();
}
//Get the parameters of the connect
strcpy(opt_peertitle, connectCfg.PeerAE.c_str());
strcpy(opt_ourtitle, connectCfg.OurAE.c_str());
strcpy(opt_peerip, connectCfg.IP.c_str());
opt_port = connectCfg.Port;
char tips[MAX_BUFFER];
sprintf(tips, "ASC_initializeNetwork…… timeout limit %d ms", timeout);
LogMsg(logfile, tips);
// network struct, contains DICOM upper layer FSM etc.
OFCondition cond=ASC_initializeNetwork(NET_REQUESTOR, 0, timeout, &net);
if (cond.bad())
{
ResultHandle(cond);
return false;
}
sprintf(tips, "ASC_createAssociationParameters…… MAXPDU %d", ASC_DEFAULTMAXPDU);
LogMsg(logfile, tips);
// parameters of association request
cond =ASC_createAssociationParameters(¶ms, ASC_DEFAULTMAXPDU);
if (cond.bad())
{
ResultHandle(cond);
return false;
}
sprintf(tips, "ASC_setAPTitles peerAE:%s outAE:%s", opt_peertitle,opt_ourtitle);
LogMsg(logfile, tips);
// set calling and called AE titles
cond =ASC_setAPTitles(params, opt_peertitle, opt_ourtitle, NULL);
if (cond.bad())
{
ResultHandle(cond);
return false;
}
sprintf(peerhost, "%s:%d", opt_peerip, opt_port);
sprintf(tips, "ASC_setPresentationAddresses: loaclhost:%s peerhost:%s", localhost, peerhost);
LogMsg(logfile, tips);
// the DICOM server accepts connections at server.nowhere.com port 104
cond=ASC_setPresentationAddresses(params, localhost, peerhost);//这里要替换SCP_IP
if (cond.bad())
{
ResultHandle(cond);
return false;
}
// list of transfer syntaxes, only a single entry here
const char* ts[] = { UID_LittleEndianImplicitTransferSyntax };
// add presentation context to association request
ASC_addPresentationContext(params, 1, UID_VerificationSOPClass, ts, 1);
// request DICOM association
cond = ASC_requestAssociation(net, params, &assoc);
if (cond.good())
{
if (ASC_countAcceptedPresentationContexts(params) == 1)
{
// the remote SCP has accepted the Verification Service Class
DIC_US id = assoc->nextMsgID++; // generate next message ID
DIC_US status; // DIMSE status of C-ECHO-RSP will be stored here
DcmDataset *sd = NULL; // status detail will be stored here
// send C-ECHO-RQ and handle response
DIMSE_echoUser(assoc, id, DIMSE_BLOCKING, 0, &status, &sd);
delete sd; // we don't care about status detail
}
//Connected
m_bConnected = true;
}
else
{
ResultHandle(cond,true);
return false;
}
return true;
}
void DicomNet::ResultHandle(OFCondition cond, bool bMsgBox/*=false*/)
{
char msg[MAX_BUFFER];
sprintf(msg, "%04x:%04x %s\n", cond.module(), cond.code(), cond.text());
DicomNet::LogMsg(logfile, msg);
if (bMsgBox)
{
::AfxMessageBox(msg);
}
}
// 记录日志
void DicomNet::LogMsg(string logFile, string msg)
{
FILE *fp;
fp = fopen(logFile.c_str(), "at+");
if (fp != NULL)
{
time_t tNowTime;
time(&tNowTime);
tm* tLocalTime = localtime(&tNowTime);
char szTime[30] = { '\0' };
strftime(szTime, 30, "[%Y-%m-%d %H:%M:%S] ", tLocalTime);
string str_log = szTime;
str_log += msg;
str_log += _T("\n");
fwrite(str_log.c_str(), sizeof(char), str_log.size(), fp);
fclose(fp);
}
return;
}
// 断开连接
bool DicomNet::DisConnect()
{
OFCondition cond;
char tips[MAX_BUFFER];
sprintf(tips, "Diconnect…… ");
LogMsg(logfile, tips);
if (assoc != nullptr)
{
sprintf(tips, "ASC_releaseAssociation…… ");
LogMsg(logfile, tips);
cond = ASC_releaseAssociation(assoc);
if (cond.bad())
{
ResultHandle(cond);
return false;
}
sprintf(tips, "ASC_destroyAssociation…… ");
LogMsg(logfile, tips);
cond = ASC_destroyAssociation(&assoc);
if (cond.bad())
{
ResultHandle(cond);
return false;
}
}
if (net != nullptr)
{
sprintf(tips, "ASC_dropNetwork…… ");
LogMsg(logfile, tips);
cond = ASC_dropNetwork(&net);
if (cond.bad())
{
ResultHandle(cond);
return false;
}
}
m_bConnected = false;
return true;
}
T_ASC_Association * DicomNet::GetCurrentAssoc()
{
return assoc;
}