Repository navigation
Expand file tree
/
Copy pathcipchannel.cpp
More file actions
68 lines (59 loc) · 1.53 KB
/
Copy pathcipchannel.cpp
File metadata and controls
68 lines (59 loc) · 1.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
#include "cipchannel.h"
#include <QHostAddress>
#include "cip_protocol.h"
CipChannel::CipChannel(QString ip, quint16 port) : QObject()
{
this->ip = ip;
this->port = port;
this->socket = new QTcpSocket();
}
int CipChannel::connectServer()
{
QHostAddress addr;
addr.setAddress(this->ip);
this->socket->connectToHost(addr, this->port);
if(!this->socket->waitForConnected()){
qDebug("cannot establish channel on port: %d\n", this->port);
return -1;
}
return 0;
}
qint64 CipChannel::write(const char *data, qint64 len)
{
qint64 res = this->socket->write(data, len);
//this->socket->waitForBytesWritten(-1);
return res;
}
qint64 CipChannel::read(char *data, qint64 maxlen)
{
int completed = 0;
int res;
while (completed < maxlen) {
// res = this->socket->waitForReadyRead();
// if (res < 0)
// continue;
res = this->socket->read(&data[completed], maxlen - completed);
if (res <= 0) {
qDebug("read error");
return -1;
}
completed += res;
}
return completed;
}
int CipChannel::init(const char *session, int length)
{
cip_message_connect_t conn;
conn.version = 0;
conn.channel_type = this->type;
conn.session_length = length;
this->write((char*)&conn, sizeof(conn));
this->write(session, length);
cip_message_connect_reply_t reply;
this->read((char*)&reply, sizeof(reply));
if (!reply.success) { /* success */
return 0;
} else {
return -1;
}
}