Skip to content

Commit

Permalink
Fixed TCPLink putting multiple packet's data in one packet. LibXBee c…
Browse files Browse the repository at this point in the history
…opied into TCPTest.
  • Loading branch information
Jester565 committed Apr 10, 2017
1 parent 28d1ccf commit a63bb2a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CommProto/include/CommProto/network/tcplink.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ namespace comnet {
static const uint8_t ADD_QUEUE_EVENT;
static const uint8_t REMOVE_QUEUE_EVENT;

static const uint8_t PACKET_SIZE_NUM_BYTES;

typedef uint32_t PacketSizeInt;

TCPLink()
:connectThread(nullptr)
{
Expand Down
24 changes: 21 additions & 3 deletions CommProto/src/network/tcplink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace comnet {
const uint8_t TCPLink::PORT_REPLY_SIZE = 1;
const uint8_t TCPLink::ADD_QUEUE_EVENT = 1;
const uint8_t TCPLink::REMOVE_QUEUE_EVENT = 2;
const uint8_t TCPLink::PACKET_SIZE_NUM_BYTES = 4;

TCPLink::~TCPLink()
{
Expand Down Expand Up @@ -98,8 +99,16 @@ namespace comnet {
if (it != clients.end()) {
TcpPtr tcp = it->second;
clientsMutex.Unlock();

if (tcp->socket != nullptr) {
if (tcp->socket->SockSend((const char*)tx_data, tx_length) != 0) //if SockSend returns a nonzero, there was an error
std::vector <uint8_t> tx_data_extended;
PacketSizeInt networkLength = htonl((PacketSizeInt)tx_length);
tx_data_extended.resize(PACKET_SIZE_NUM_BYTES);
for (int i = 0; i < PACKET_SIZE_NUM_BYTES; i++) {
tx_data_extended.at(i) = (networkLength >> (8 * i)) & 0xff;
}
tx_data_extended.insert(tx_data_extended.end(), &tx_data[0], &tx_data[tx_length]);
if (tcp->socket->SockSend((const char*)tx_data_extended.data(), tx_data_extended.size()) != 0) //if SockSend returns a nonzero, there was an error
{
std::string msg = "Delete TcpSocket for client with ID ";
msg += std::to_string(dest_id);
Expand Down Expand Up @@ -129,9 +138,18 @@ namespace comnet {
it++;
clientsMutex.Unlock();
if (tcp->socket != nullptr) {
packet_data_status_t recvStatus = tcp->socket->SockReceive((const char*)rx_data, MAX_BUFFER_SIZE, *rx_length);
uint8_t packet_size_data[PACKET_SIZE_NUM_BYTES];
packet_data_status_t recvStatus = tcp->socket->SockReceive((const char*)packet_size_data, PACKET_SIZE_NUM_BYTES, *rx_length);
if (recvStatus == PACKET_SUCCESSFUL) {
return true;
PacketSizeInt packSize = 0;
for (int i = 0; i < PACKET_SIZE_NUM_BYTES; i++) {
packSize |= (packet_size_data[i] & 0xff) << (8 * i);
}
packSize = ntohl(packSize);
recvStatus = tcp->socket->SockReceive((const char*)rx_data, packSize, *rx_length);
if (recvStatus == PACKET_SUCCESSFUL) {
return true;
}
}
}
clientsMutex.Lock();
Expand Down
2 changes: 1 addition & 1 deletion CommProto/src/network/tcpsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace comnet {
_socket.id = -1;
}
/**
Closes the socket and destroyes the tcp node, along with the mutex.
Default destructor
*/
~TcpSocket() {
}
Expand Down
3 changes: 2 additions & 1 deletion libxbee3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ if (test)
COMMAND cd \"${DYNLIB_DIR}\"
COMMAND ${COPY_CMD} \"${DYNLIB_NAME}\" \"${CMAKE_CURRENT_BINARY_DIR}/../tests/classes/cpp/udp/${DYNLIB_NAME}\" ${COPY_OPTION}
COMMAND ${COPY_CMD} \"${DYNLIB_NAME}\" \"${CMAKE_CURRENT_BINARY_DIR}/../tests/classes/cpp/xbee/${DYNLIB_NAME}\" ${COPY_OPTION}
COMMAND ${COPY_CMD} \"${DYNLIB_NAME}\" \"${CMAKE_CURRENT_BINARY_DIR}/../tests/classes/cpp/serial/${DYNLIB_NAME}\" ${COPY_OPTION})
COMMAND ${COPY_CMD} \"${DYNLIB_NAME}\" \"${CMAKE_CURRENT_BINARY_DIR}/../tests/classes/cpp/serial/${DYNLIB_NAME}\" ${COPY_OPTION}
COMMAND ${COPY_CMD} \"${DYNLIB_NAME}\" \"${CMAKE_CURRENT_BINARY_DIR}/../tests/classes/cpp/tcp/${DYNLIB_NAME}\" ${COPY_OPTION})
endif()
endif()
25 changes: 21 additions & 4 deletions modules/csharp/network/TCPLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ namespace Comnet
TCPHolder^ tcp;
if (clients->TryGetValue(destID, tcp)) {
if (tcp->socket != nullptr) {
if (tcp->socket->SockSend((const char*)txData, txLength) != 0) //if SockSend returns a nonzero, there was an error
std::vector <uint8_t> tx_data_extended;
PacketSizeInt networkLength = htonl((PacketSizeInt)txLength);
tx_data_extended.resize(PACKET_SIZE_NUM_BYTES);
for (int i = 0; i < PACKET_SIZE_NUM_BYTES; i++) {
tx_data_extended.at(i) = (networkLength >> (8 * i)) & 0xff;
}
tx_data_extended.insert(tx_data_extended.end(), &txData[0], &txData[txLength]);
if (tcp->socket->SockSend((const char*)tx_data_extended.data(), tx_data_extended.size()) != 0) //if SockSend returns a nonzero, there was an error
{
String^ msg = gcnew String("Delete TcpSocket for client with ID ");
msg->Concat(System::Convert::ToString((int)destID));
Expand All @@ -141,10 +148,20 @@ namespace Comnet
if (tcp->socket != nullptr) //Check if a socket is open
{
uint32_t recvSize = 0;
comnet::network::packet_data_status_t recvStatus = tcp->socket->SockReceive((const char*)rxData, MAX_BUFFER_SIZE, recvSize);

uint8_t packet_size_data[PACKET_SIZE_NUM_BYTES];
comnet::network::packet_data_status_t recvStatus = tcp->socket->SockReceive((const char*)packet_size_data, PACKET_SIZE_NUM_BYTES, recvSize);
if (recvStatus == comnet::network::PACKET_SUCCESSFUL) {
rxLength = recvSize;
return true;
PacketSizeInt packSize = 0;
for (int i = 0; i < PACKET_SIZE_NUM_BYTES; i++) {
packSize |= (packet_size_data[i] & 0xff) << (8 * i);
}
packSize = ntohl(packSize);
recvStatus = tcp->socket->SockReceive((const char*)rxData, packSize, recvSize);
if (recvStatus == comnet::network::PACKET_SUCCESSFUL) {
rxLength = recvSize;
return true;
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions modules/csharp/network/TCPLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ namespace Comnet {

static const uint8_t ADD_QUEUE_EVENT = 1;
static const uint8_t REMOVE_QUEUE_EVENT = 2;

static const uint8_t PACKET_SIZE_NUM_BYTES = 4;

typedef uint32_t PacketSizeInt;

TCPLink();
~TCPLink();
Expand Down

0 comments on commit a63bb2a

Please sign in to comment.