TCP socket server works and test_message works.
This commit is contained in:
parent
b438e44aff
commit
e2c7983a42
|
@ -36,6 +36,7 @@ class State {
|
|||
uint64_t y;
|
||||
uint64_t width;
|
||||
uint64_t height;
|
||||
bool active = false;
|
||||
bool ERROR = false;
|
||||
|
||||
void error();
|
||||
|
@ -56,6 +57,8 @@ class State {
|
|||
vector<uint64_t> getExposure() { return exposure; }
|
||||
vector<uint64_t> getPosition() { return { x, y, width, height }; }
|
||||
bool isError () { return ERROR; }
|
||||
void setActive () { active = true; }
|
||||
bool isActive () { return active; }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
28
src/main.cpp
28
src/main.cpp
|
@ -14,7 +14,7 @@
|
|||
using namespace std;
|
||||
|
||||
uint16_t PORT = 8081;
|
||||
int BUFFER_SIZE = 1024;
|
||||
uint16_t BUFFER_SIZE = 2048;
|
||||
|
||||
Image img;
|
||||
State state;
|
||||
|
@ -30,11 +30,16 @@ condition_variable receivedCondVar;
|
|||
mutex sendMutex;
|
||||
condition_variable sendCondVar;
|
||||
|
||||
void performAction () {
|
||||
void processMessage () {
|
||||
if (receivedMessage) {
|
||||
cout << "RECEIVED" << endl;
|
||||
state.receiveMessage(incomingMessage);
|
||||
receivedMessage = false;
|
||||
|
||||
while (state.isActive()) {
|
||||
//
|
||||
}
|
||||
|
||||
if (state.isError()) {
|
||||
outgoingMessage = state.createMessage(false);
|
||||
} else {
|
||||
|
@ -51,20 +56,21 @@ void handleTCPConnection(int clientSocket) {
|
|||
while (true) {
|
||||
int bytesRead = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
|
||||
if (bytesRead <= 0) {
|
||||
// Client disconnected or error occurred
|
||||
cerr << "Client disconnected or errored out" << endl;
|
||||
break;
|
||||
}
|
||||
|
||||
std::string iMessage(buffer, bytesRead);
|
||||
string localIncomingMessage(buffer, bytesRead);
|
||||
|
||||
{
|
||||
lock_guard<mutex> lock(receivedMutex);
|
||||
receivedMessage = true;
|
||||
incomingMessage = iMessage;
|
||||
sendMessage = false;
|
||||
incomingMessage = localIncomingMessage;
|
||||
receivedCondVar.notify_all();
|
||||
}
|
||||
|
||||
performAction();
|
||||
processMessage();
|
||||
|
||||
bool localSendMessage;
|
||||
string localOutgoingMessage;
|
||||
|
@ -80,7 +86,7 @@ void handleTCPConnection(int clientSocket) {
|
|||
const char* msg = localOutgoingMessage.c_str();
|
||||
int bytesSent = send(clientSocket, msg, strlen(msg), 0);
|
||||
if (bytesSent < 0) {
|
||||
std::cerr << "Error sending ACK to client" << std::endl;
|
||||
cerr << "Error sending ACK to client" << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +102,7 @@ void runTCPServer (int serverSocket) {
|
|||
|
||||
int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
|
||||
if (clientSocket < 0) {
|
||||
std::cerr << "Error accepting client connection" << std::endl;
|
||||
cerr << "Error accepting client connection" << endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -120,7 +126,7 @@ int main (int argc, char** argv) {
|
|||
|
||||
int bindResult = ::bind(serverSocket, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr));
|
||||
if (bindResult < 0) {
|
||||
std::cerr << "Error binding socket" << std::endl;
|
||||
cerr << "Error binding socket" << endl;
|
||||
close(serverSocket);
|
||||
return 1;
|
||||
}
|
||||
|
@ -133,8 +139,10 @@ int main (int argc, char** argv) {
|
|||
cout << "TCP server listening on port " << PORT << endl;
|
||||
|
||||
thread serverThread(runTCPServer, serverSocket);
|
||||
|
||||
serverThread.join();
|
||||
|
||||
|
||||
|
||||
close(serverSocket);
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -66,7 +66,7 @@ void State::receiveMessage (string msgString) {
|
|||
return;
|
||||
}
|
||||
|
||||
if (msgData.contains("image")) {
|
||||
if (action == LOAD && msgData.contains("image")) {
|
||||
setImage(msgData);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,15 +18,14 @@ client.connect(serverPort, serverAddress, async () => {
|
|||
action : 1,
|
||||
image: 'filename.tif',
|
||||
};
|
||||
const jsonData = JSON.stringify(data);
|
||||
console.log('SENDING');
|
||||
console.log(jsonData);
|
||||
client.write(jsonData);
|
||||
console.log(data);
|
||||
client.write(JSON.stringify(data));
|
||||
await delay(2000);
|
||||
jsonData.action = 2;
|
||||
data.action = 2;
|
||||
console.log('SENDING');
|
||||
console.log(jsonData);
|
||||
client.write(jsonData);
|
||||
console.log(data);
|
||||
client.write(JSON.stringify(data));
|
||||
});
|
||||
|
||||
client.on('data', (data) => {
|
||||
|
|
Loading…
Reference in New Issue