-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathip_interface.cpp
More file actions
203 lines (185 loc) · 4.53 KB
/
Copy pathip_interface.cpp
File metadata and controls
203 lines (185 loc) · 4.53 KB
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
#include "ip_interface.h"
#include <QFile>
#include <QWaitCondition>
#include <QMutex>
#include <QThread>
struct thread_sync
{
QString file;
QByteArray mem;
QMutex mutex;
QWaitCondition cond;
};
struct send_thread : public QThread
{
volatile bool flag;
QQueue<QByteArray> queue;
ip_slice slice;
thread_sync *sync;
send_thread(const QString &addr, IP::Protocol protl,
IP::ToSPriority prio, IP::ToSSubfield subf)
{
slice.set_mtu(IP::DefaultMTU);
slice.set_addr_src(IP::DefaultSourceAddr);
slice.set_addr_dst(addr);
slice.set_protocol(protl);
slice.set_tos(prio, subf);
flag = true;
this->start();
}
~send_thread()
{
flag = false;
this->wait(300);
this->terminate();
}
//!
//! \brief write 向底层写入数据
//! \param data
//!
void write (const QByteArray &data)
{
queue.enqueue(data);
}
void run() override
{
while (flag)
{
// 队列内有数据则进行分片模拟
if (!queue.isEmpty())
{
QByteArray data = queue.dequeue();
// 分片并打包为IP层数据
QByteArray raw = ip_slice::pack(slice.fragment(data));
// 使用文件作为管道则写入文件中
if (!sync->file.isEmpty())
{
sync->mutex.lock();
QFile fio(sync->file);
if (fio.open(QIODevice::WriteOnly))
{
fio.write(raw);
fio.close();
}
sync->mutex.unlock();
sync->cond.wakeAll();
}
// 写入内存中
else
{
sync->mem = raw;
// 通知重组线程,进行重组
sync->cond.wakeAll();
}
}
else
msleep(100);
}
}
};
struct recv_thread : public QThread
{
volatile bool flag;
ip_frag frag;
ip_interface *iface;
recv_thread(ip_interface *i)
{
iface = i;
flag = true;
this->start();
}
~recv_thread()
{
flag = false;
iface->sync->cond.wakeAll();
this->wait(300);
this->terminate();
}
void run() override
{
while (flag)
{
// 等待是否有重组数据
iface->sync->mutex.lock();
iface->sync->cond.wait(&iface->sync->mutex);
iface->sync->mutex.unlock();
// 读取IP层数据
QByteArray raw;
if (iface->sync->file.isEmpty())
{
if (iface->sync->mem.isEmpty())
continue;
raw = iface->sync->mem;
}
else
{
iface->sync->mutex.lock();
QFile fio(iface->sync->file);
if (fio.open(QIODevice::ReadOnly))
{
raw = fio.readAll();
fio.close();
}
iface->sync->mutex.unlock();
}
// 重组数据
QByteArray buff = frag.reassemble(ip_frag::unpack(raw));
if (!buff.isEmpty())
emit iface->recv(buff);
}
}
};
ip_interface::ip_interface(const QString &frag_file)
{
sync = new thread_sync;
if (!frag_file.isEmpty())
sync->file = frag_file;
sthrd = 0;
rthrd = new recv_thread(this);
}
ip_interface::~ip_interface()
{
if (rthrd)
delete rthrd;
cancel();
}
//!
//! \brief ip_interface::create 根据传输层给出内容,创建一个模拟器
//! \param addr
//! \param protl
//! \param prio
//! \param subf
//! \return
//!
bool ip_interface::create(const QString &addr, IP::Protocol protl,
IP::ToSPriority prio, IP::ToSSubfield subf)
{
if (sthrd)
return false;
sthrd = new send_thread(addr, protl, prio, subf);
sthrd->sync = sync;
return true;
}
//!
//! \brief ip_interface::cancel 取消模拟器
//!
void ip_interface::cancel()
{
if (sthrd)
delete sthrd;
sthrd = 0;
}
//!
//! \brief ip_interface::send 发送数据
//! \param data
//! \return
//!
bool ip_interface::send(const QByteArray &data)
{
if (sthrd)
{
sthrd->write(data);
return true;
}
return false;
}