commit ccd25722f19e801b0ca31f3c66a0d757c0bedc87 Author: codecrafters-bot Date: Thu Jul 17 06:55:26 2025 +0000 init [skip ci] diff --git a/.codecrafters/compile.sh b/.codecrafters/compile.sh new file mode 100755 index 0000000..9da226b --- /dev/null +++ b/.codecrafters/compile.sh @@ -0,0 +1,12 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake +cmake --build ./build diff --git a/.codecrafters/run.sh b/.codecrafters/run.sh new file mode 100755 index 0000000..afe2286 --- /dev/null +++ b/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec ./build/server "$@" diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d66be1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,49 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app +server + +# CMake +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +build/ +vcpkg_installed \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4993f2b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.13) + +project(redis-starter-cpp) + +file(GLOB_RECURSE SOURCE_FILES src/*.cpp) + +set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard +set(THREADS_PREFER_PTHREAD_FLAG ON) + +find_package(Threads REQUIRED) +find_package(asio CONFIG REQUIRED) + +add_executable(server ${SOURCE_FILES}) + +target_link_libraries(server PRIVATE asio asio::asio) +target_link_libraries(server PRIVATE Threads::Threads) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..54ecdd9 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +[![progress-banner](https://backend.codecrafters.io/progress/redis/f0888919-26c3-46fb-9854-4f2e891af515)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for C++ solutions to the +["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis). + +In this challenge, you'll build a toy Redis clone that's capable of handling +basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about +event loops, the Redis protocol and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your Redis implementation is in `src/Server.cpp`. Study and +uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +That's all! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `cmake` installed locally +1. Run `./your_program.sh` to run your Redis server, which is implemented in + `src/Server.cpp`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/codecrafters.yml b/codecrafters.yml new file mode 100644 index 0000000..ad26fa0 --- /dev/null +++ b/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the C++ version used to run your code +# on Codecrafters. +# +# Available versions: cpp-23 +language_pack: cpp-23 diff --git a/src/Server.cpp b/src/Server.cpp new file mode 100644 index 0000000..0fce76b --- /dev/null +++ b/src/Server.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + // Flush after every std::cout / std::cerr + std::cout << std::unitbuf; + std::cerr << std::unitbuf; + + int server_fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_fd < 0) { + std::cerr << "Failed to create server socket\n"; + return 1; + } + + // Since the tester restarts your program quite often, setting SO_REUSEADDR + // ensures that we don't run into 'Address already in use' errors + int reuse = 1; + if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) { + std::cerr << "setsockopt failed\n"; + return 1; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(6379); + + if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) { + std::cerr << "Failed to bind to port 6379\n"; + return 1; + } + + int connection_backlog = 5; + if (listen(server_fd, connection_backlog) != 0) { + std::cerr << "listen failed\n"; + return 1; + } + + struct sockaddr_in client_addr; + int client_addr_len = sizeof(client_addr); + std::cout << "Waiting for a client to connect...\n"; + + // You can use print statements as follows for debugging, they'll be visible when running tests. + std::cout << "Logs from your program will appear here!\n"; + + // Uncomment this block to pass the first stage + // + // accept(server_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_len); + // std::cout << "Client connected\n"; + // + // close(server_fd); + + return 0; +} diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json new file mode 100644 index 0000000..16b40d6 --- /dev/null +++ b/vcpkg-configuration.json @@ -0,0 +1,14 @@ +{ + "default-registry": { + "kind": "git", + "baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d", + "repository": "https://github.com/microsoft/vcpkg" + }, + "registries": [ + { + "kind": "artifact", + "location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip", + "name": "microsoft" + } + ] +} diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..c4ec5d2 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,6 @@ +{ + "dependencies": [ + "asio", + "pthreads" + ] +} diff --git a/your_program.sh b/your_program.sh new file mode 100755 index 0000000..c493a3e --- /dev/null +++ b/your_program.sh @@ -0,0 +1,25 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/compile.sh +# +# - Edit this to change how your program compiles locally +# - Edit .codecrafters/compile.sh to change how your program compiles remotely +( + cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory + cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake + cmake --build ./build +) + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec ./build/server "$@"