Skip to content

Commit

Permalink
add network stuff, doesn't work yet
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosentino11 committed Jun 2, 2024
1 parent 9f26593 commit 5737c82
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include "Context.h"
#include <iostream>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

namespace MqttClient {

Command::Command(Context context) : mContext(context) {}
Expand All @@ -13,6 +18,8 @@ void Command::execute() {

if (mContext.command == "pub") {

// TODO move connect packet to separate class

char variableHeaderLength = 10; // TODO const

// client id length needs to fit in the second byte of the connect
Expand Down Expand Up @@ -49,7 +56,31 @@ void Command::execute() {
connectPacket[i++] = mContext.clientId[c];
}

// TODO send the packet
// TODO move network stuff to separate class

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
return; // TODO error handling
}
sockaddr_in server_addr;
std::memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
if (inet_pton(AF_INET, mContext.address.data(),
&server_addr.sin_addr) <= 0) {
close(sock);
return; // TODO error handling
}
if (connect(sock, (struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
close(sock);
return; // TODO error handling
}
if (send(sock, mContext.message.data(), mContext.message.size(), 0) <
0) {
close(sock);
return; // TODO error handling
}
}
}

Expand Down

0 comments on commit 5737c82

Please sign in to comment.