Create project in current state. Send works. Threading is a little sloppy

This commit is contained in:
Matt McWilliams 2024-04-05 09:21:41 -04:00
commit 1cbf34c7f1
7 changed files with 209 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "json"]
path = json
url = https://github.com/nlohmann/json.git

24
CMakeLists.txt Normal file
View File

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.9)
project(OpengGL_Timing_Example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
set( NAME_SRC
main.cpp
)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( opengl_timing_example ${NAME_SRC} ${NAME_HEADERS} )
if (APPLE)
# Equivalent to pass flags -framework OpenGL
target_link_libraries(opengl_timing_example OpenGL::GL GLUT::GLUT)
else()
# Equivalent to pass flags -lGL, -lGLU and -lglut
target_link_libraries(opengl_timing_example GL GLU glut )
endif()

9
compile.sh Normal file
View File

@ -0,0 +1,9 @@
#!/bin/bash
#gcc -o opengl_timing_example main.cpp -lGL -lGLU -lglut -lm
rm -rf build
mkdir -p build
cd build
cmake ..
make -j4

1
json Submodule

@ -0,0 +1 @@
Subproject commit 199dea11b17c533721b26249e2dcaee6ca1d51d3

147
main.cpp Normal file
View File

@ -0,0 +1,147 @@
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/gl3.h>
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <string>
#include <arpa/inet.h>
#include <unistd.h>
#include "json/single_include/nlohmann/json.hpp"
#include <mutex>
#include <thread>
#include <condition_variable>
using namespace std;
using namespace std::chrono;
const int DISPLAY_DURATION = 2000;
steady_clock::time_point startTime;
bool shouldDisplayQuad = false;
mutex quadMutex;
condition_variable quadCondVar;
void displayQuad() {
glClear(GL_COLOR_BUFFER_BIT);
auto currentTime = steady_clock::now();
auto elapsedTime = duration_cast<milliseconds>(currentTime - startTime).count();
bool localShouldDisplayQuad;
{
unique_lock<mutex> lock(quadMutex);
quadCondVar.wait(lock, [&] { return shouldDisplayQuad; });
localShouldDisplayQuad = shouldDisplayQuad;
}
if (localShouldDisplayQuad && elapsedTime < DISPLAY_DURATION) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(-0.5f, -0.5f);
glVertex2f(-0.5f, 0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
} else {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
glutSwapBuffers();
}
void timer(int value) {
glutPostRedisplay();
glutTimerFunc(1, timer, 0);
}
void handleTCPConnection(int serverSocket) {
struct sockaddr_in clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket < 0) {
cerr << "Error accepting client connection" << endl;
return;
}
char buffer[1024] = {0};
int bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesRead < 0) {
cerr << "Error reading from client socket" << endl;
close(clientSocket);
return;
}
string jsonString(buffer, bytesRead);
try {
nlohmann::json jsonData = nlohmann::json::parse(jsonString);
cout << "Received JSON: " << jsonData << endl;
{
lock_guard<mutex> lock(quadMutex);
shouldDisplayQuad = true;
startTime = steady_clock::now();
quadCondVar.notify_all();
}
} catch (const exception& e) {
cerr << "Error parsing JSON: " << e.what() << endl;
}
close(clientSocket);
}
void runTCPServer(int serverSocket) {
while (true) {
handleTCPConnection(serverSocket);
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Red Quad");
glutDisplayFunc(displayQuad);
glutTimerFunc(0, timer, 0);
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0) {
cerr << "Error creating socket" << endl;
return 1;
}
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(8080);
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
cerr << "Error binding socket" << endl;
return 1;
}
if (listen(serverSocket, 5) < 0) {
cerr << "Error listening on socket" << endl;
return 1;
}
cout << "TCP server listening on port 8080" << endl;
thread serverThread(runTCPServer, serverSocket);
glutMainLoop();
serverThread.join();
close(serverSocket);
return 0;
}

24
send.py Normal file
View File

@ -0,0 +1,24 @@
import socket
import json
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 8080)
print(f'Connecting to {server_address}...')
client_socket.connect(server_address)
try:
data = {
'message': 'Hello from Python',
'value': 42,
'enabled': True
}
json_data = json.dumps(data)
print(f'Sending: {json_data}')
client_socket.sendall(json_data.encode())
finally:
print('Closing connection')
client_socket.close()