TCP socket server works and test_message works.

This commit is contained in:
mmcwilliams 2024-04-19 20:33:28 -06:00
parent b438e44aff
commit e2c7983a42
4 changed files with 27 additions and 17 deletions

View File

@ -36,6 +36,7 @@ class State {
uint64_t y; uint64_t y;
uint64_t width; uint64_t width;
uint64_t height; uint64_t height;
bool active = false;
bool ERROR = false; bool ERROR = false;
void error(); void error();
@ -56,6 +57,8 @@ class State {
vector<uint64_t> getExposure() { return exposure; } vector<uint64_t> getExposure() { return exposure; }
vector<uint64_t> getPosition() { return { x, y, width, height }; } vector<uint64_t> getPosition() { return { x, y, width, height }; }
bool isError () { return ERROR; } bool isError () { return ERROR; }
void setActive () { active = true; }
bool isActive () { return active; }
}; };
#endif #endif

View File

@ -14,7 +14,7 @@
using namespace std; using namespace std;
uint16_t PORT = 8081; uint16_t PORT = 8081;
int BUFFER_SIZE = 1024; uint16_t BUFFER_SIZE = 2048;
Image img; Image img;
State state; State state;
@ -30,11 +30,16 @@ condition_variable receivedCondVar;
mutex sendMutex; mutex sendMutex;
condition_variable sendCondVar; condition_variable sendCondVar;
void performAction () { void processMessage () {
if (receivedMessage) { if (receivedMessage) {
cout << "RECEIVED" << endl; cout << "RECEIVED" << endl;
state.receiveMessage(incomingMessage); state.receiveMessage(incomingMessage);
receivedMessage = false; receivedMessage = false;
while (state.isActive()) {
//
}
if (state.isError()) { if (state.isError()) {
outgoingMessage = state.createMessage(false); outgoingMessage = state.createMessage(false);
} else { } else {
@ -51,20 +56,21 @@ void handleTCPConnection(int clientSocket) {
while (true) { while (true) {
int bytesRead = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0); int bytesRead = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
if (bytesRead <= 0) { if (bytesRead <= 0) {
// Client disconnected or error occurred cerr << "Client disconnected or errored out" << endl;
break; break;
} }
std::string iMessage(buffer, bytesRead); string localIncomingMessage(buffer, bytesRead);
{ {
lock_guard<mutex> lock(receivedMutex); lock_guard<mutex> lock(receivedMutex);
receivedMessage = true; receivedMessage = true;
incomingMessage = iMessage; sendMessage = false;
incomingMessage = localIncomingMessage;
receivedCondVar.notify_all(); receivedCondVar.notify_all();
} }
performAction(); processMessage();
bool localSendMessage; bool localSendMessage;
string localOutgoingMessage; string localOutgoingMessage;
@ -80,7 +86,7 @@ void handleTCPConnection(int clientSocket) {
const char* msg = localOutgoingMessage.c_str(); const char* msg = localOutgoingMessage.c_str();
int bytesSent = send(clientSocket, msg, strlen(msg), 0); int bytesSent = send(clientSocket, msg, strlen(msg), 0);
if (bytesSent < 0) { if (bytesSent < 0) {
std::cerr << "Error sending ACK to client" << std::endl; cerr << "Error sending ACK to client" << endl;
break; break;
} }
} }
@ -96,7 +102,7 @@ void runTCPServer (int serverSocket) {
int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen); int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket < 0) { if (clientSocket < 0) {
std::cerr << "Error accepting client connection" << std::endl; cerr << "Error accepting client connection" << endl;
continue; continue;
} }
@ -120,7 +126,7 @@ int main (int argc, char** argv) {
int bindResult = ::bind(serverSocket, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr)); int bindResult = ::bind(serverSocket, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr));
if (bindResult < 0) { if (bindResult < 0) {
std::cerr << "Error binding socket" << std::endl; cerr << "Error binding socket" << endl;
close(serverSocket); close(serverSocket);
return 1; return 1;
} }
@ -133,8 +139,10 @@ int main (int argc, char** argv) {
cout << "TCP server listening on port " << PORT << endl; cout << "TCP server listening on port " << PORT << endl;
thread serverThread(runTCPServer, serverSocket); thread serverThread(runTCPServer, serverSocket);
serverThread.join(); serverThread.join();
close(serverSocket); close(serverSocket);
return 0; return 0;

View File

@ -66,7 +66,7 @@ void State::receiveMessage (string msgString) {
return; return;
} }
if (msgData.contains("image")) { if (action == LOAD && msgData.contains("image")) {
setImage(msgData); setImage(msgData);
} }

View File

@ -18,15 +18,14 @@ client.connect(serverPort, serverAddress, async () => {
action : 1, action : 1,
image: 'filename.tif', image: 'filename.tif',
}; };
const jsonData = JSON.stringify(data);
console.log('SENDING'); console.log('SENDING');
console.log(jsonData); console.log(data);
client.write(jsonData); client.write(JSON.stringify(data));
await delay(2000); await delay(2000);
jsonData.action = 2; data.action = 2;
console.log('SENDING'); console.log('SENDING');
console.log(jsonData); console.log(data);
client.write(jsonData); client.write(JSON.stringify(data));
}); });
client.on('data', (data) => { client.on('data', (data) => {