Fixed uploading images. Implemented a threadsafe queue and simple IP tracking.

This commit is contained in:
2025-05-11 10:59:15 +02:00
parent 87d8afda4d
commit 18581ed9ac
12 changed files with 255 additions and 56 deletions
+54
View File
@@ -0,0 +1,54 @@
#ifndef CONCURRENTQUEUE_H
#define CONCURRENTQUEUE_H
#include <queue>
#include <mutex>
#include <condition_variable>
using namespace std;
template <typename T>
class ConcurrentQueue {
private:
std::queue<T> queue_;
mutable std::mutex mutex_;
std::condition_variable condition_;
public:
// Apparently, if you have a mutex in a class, you can't copy or assign the class to any other class.
ConcurrentQueue() = default;
ConcurrentQueue(const ConcurrentQueue&) = delete; // Prevent copying
ConcurrentQueue& operator=(const ConcurrentQueue&) = delete; // Prevent assignment
void push(T value) {
lock_guard<mutex> lock(mutex_);
queue_.push(move(value));
condition_.notify_one();
}
bool try_pop(T& value) {
lock_guard<mutex> lock(mutex_);
if (queue_.empty()) {
return false;
}
value = move(queue_.front());
queue_.pop();
return true;
}
T wait_and_pop() {
unique_lock<mutex> lock(mutex_);
condition_.wait(lock, [this] { return !queue_.empty(); });
T value = move(queue_.front());
queue_.pop();
return value;
}
bool empty() const {
lock_guard<mutex> lock(mutex_);
return queue_.empty();
}
};
#endif
+1
View File
@@ -10,6 +10,7 @@ struct FileUtils {
static bool fileExists(const char *path);
static vector<unsigned char> open_image(const string& path);
static vector<string> get_image_list(const string& path);
static vector<string> get_wordlists(const string& path);
};
#endif
+5 -2
View File
@@ -4,12 +4,14 @@
#include <string>
#include <chrono>
#include "../include/DataType.h"
#include "../include/Track.h"
#include "../include/ConcurrentQueue.h"
using namespace std;
class ServerUtils {
public:
static void serve();
static void serve(shared_ptr<ConcurrentQueue<Track>> t_test);
private:
static void process_request(int client_fd);
static void send_header(int client_fd, data_type type);
@@ -17,7 +19,8 @@ class ServerUtils {
static void send_chunked_css(int client_fd);
static void send_data(int client_fd, const string& data);
static void send_image(int client_fd, const string& path, image_type type);
static size_t send_all(int sockfd, const char* data, size_t length);
static size_t send_all(int client_fd, const char* data, size_t length);
static string get_ip(int client_fd);
};
const string HTML_RESPONSE_HEADER =
+12
View File
@@ -0,0 +1,12 @@
#ifndef TRACK_H
#define TRACK_H
#include <string>
using namespace std;
struct Track {
string Ip;
string UserAgent;
};
#endif //TRACK_H
+16
View File
@@ -0,0 +1,16 @@
#ifndef TRACKERUTILS_H
#define TRACKERUTILS_H
#include <unordered_map>
#include "../include/ConcurrentQueue.h"
#include "../include/Track.h"
using namespace std;
struct TrackerUtils {
static void track(const shared_ptr<ConcurrentQueue<Track>>& t_test);
static void print(unordered_map<string, int> tracks);
};
#endif