init [skip ci]
This commit is contained in:
12
.codecrafters/compile.sh
Executable file
12
.codecrafters/compile.sh
Executable file
@@ -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
|
11
.codecrafters/run.sh
Executable file
11
.codecrafters/run.sh
Executable file
@@ -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 "$@"
|
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
||||
* text=auto
|
49
.gitignore
vendored
Normal file
49
.gitignore
vendored
Normal file
@@ -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
|
16
CMakeLists.txt
Normal file
16
CMakeLists.txt
Normal file
@@ -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)
|
33
README.md
Normal file
33
README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
[](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.
|
11
codecrafters.yml
Normal file
11
codecrafters.yml
Normal file
@@ -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
|
61
src/Server.cpp
Normal file
61
src/Server.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
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;
|
||||
}
|
14
vcpkg-configuration.json
Normal file
14
vcpkg-configuration.json
Normal file
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
6
vcpkg.json
Normal file
6
vcpkg.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"dependencies": [
|
||||
"asio",
|
||||
"pthreads"
|
||||
]
|
||||
}
|
25
your_program.sh
Executable file
25
your_program.sh
Executable file
@@ -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 "$@"
|
Reference in New Issue
Block a user