Skip to content

Commit

Permalink
Support multitple connections using multithreading
Browse files Browse the repository at this point in the history
  • Loading branch information
mleleszi committed Jun 14, 2024
1 parent c53d4bc commit 665f4b9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# TODO

- allow multiple connections
- multi hreaded: implement thread pool from scratch
- look into std::shared_mutex
- event loop, non blocking IO
- support more data structures
- lists (implement ziplists)
- sets
- hashes
- implement hasmap from scratch
- expiry
- support more commands
13 changes: 11 additions & 2 deletions src/datastore.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
Expand All @@ -8,16 +9,24 @@ class DataStore {

public:
std::optional<std::string> get(const std::string &key) {
std::lock_guard<std::mutex> lock(mtx);
if (store.find(key) == store.end()) return {};

return store[key];
}

void set(const std::string &key, const std::string &val) { store[key] = val; }
void set(const std::string &key, const std::string &val) {
std::lock_guard<std::mutex> lock(mtx);
store[key] = val;
}

bool exists(const std::string &key) { return store.contains(key); }
bool exists(const std::string &key) {
std::lock_guard<std::mutex> lock(mtx);
return store.contains(key);
}


private:
std::unordered_map<std::string, std::string> store;
std::mutex mtx;
};
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "tcp_server.h"

int main() {
Controller controller;
TCPServer server{controller};
TCPServer server;
server.start("0.0.0.0", 6379);
}
28 changes: 19 additions & 9 deletions src/tcp_server.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <algorithm>
#include <arpa/inet.h>
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
#include <vector>

Expand All @@ -14,7 +14,7 @@
#include "protocol.h"
#include "tcp_server.h"

TCPServer::TCPServer(const Controller &controller) : controller{controller} {
TCPServer::TCPServer() : controller{} {
m_serverFD = socket(AF_INET, SOCK_STREAM, 0);

if (m_serverFD < 0) { throw std::runtime_error("Failed to create server socket!"); }
Expand Down Expand Up @@ -58,9 +58,7 @@ TCPServer::TCPServer(const Controller &controller) : controller{controller} {
spdlog::info("Client connected from {}:{}", clientIP, clientPort);
}

handleRequest(connFD);

close(connFD);
std::thread(&TCPServer::handleRequest, this, connFD).detach();
}
}

Expand All @@ -73,21 +71,30 @@ void TCPServer::handleRequest(int connFD) {
// Read from socket
ssize_t bytes_received = recv(connFD, data.data(), RECV_SIZE, 0);

if (bytes_received <= 0) { break; }
if (bytes_received <= 0) {
close(connFD);
break;
}

buffer.insert(buffer.end(), data.begin(), data.begin() + bytes_received);

// Parse message
auto [message, length] = *parseMessage(buffer);

if (!std::holds_alternative<RedisType::Array>(message)) { break; }
if (!std::holds_alternative<RedisType::Array>(message)) {
close(connFD);
break;
}

// If successfully parsed message, then erase
buffer.erase(buffer.begin(), buffer.begin() + static_cast<long>(length));

auto array = std::get<RedisType::Array>(message).data;

if (!array) { break; }
if (!array) {
close(connFD);
break;
}

// Convert message to internal command format
std::vector<RedisType::BulkString> command;
Expand All @@ -101,7 +108,10 @@ void TCPServer::handleRequest(int connFD) {
command.push_back(std::get<RedisType::BulkString>(item));
}

if (err) { break; }
if (err) {
close(connFD);
break;
}

// Handle command
RedisType::RedisValue res = controller.handleCommand(command);
Expand Down
2 changes: 1 addition & 1 deletion src/tcp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TCPServer {
public:
explicit TCPServer(const Controller &controller);
explicit TCPServer();
[[noreturn]] void start(const std::string &address, int port);
void handleRequest(int conn_fd);

Expand Down

0 comments on commit 665f4b9

Please sign in to comment.