mirror of
				https://git.sfja.dk/Mikkel/slige.git
				synced 2025-11-04 07:58:16 +00:00 
			
		
		
		
	refactor slige_rpc -> sliger::rpc
This commit is contained in:
		
							parent
							
								
									51d7ba7a33
								
							
						
					
					
						commit
						4dccbcb17a
					
				@ -91,7 +91,7 @@ auto compile_asm(const std::vector<AsmLine>& lines) -> std::vector<uint32_t>
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
auto execute_action(std::unique_ptr<sliger::json::Value> req,
 | 
					auto execute_action(std::unique_ptr<sliger::json::Value> req,
 | 
				
			||||||
    std::unique_ptr<slige_rpc::BufferedWriter> writer) -> void
 | 
					    std::unique_ptr<sliger::rpc::BufferedWriter> writer)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -101,9 +101,9 @@ int main()
 | 
				
			|||||||
    using L = Loc;
 | 
					    using L = Loc;
 | 
				
			||||||
    using enum sliger::Op;
 | 
					    using enum sliger::Op;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auto rpc = slige_rpc::RpcServer(
 | 
					    auto rpc = sliger::rpc::RpcServer(
 | 
				
			||||||
        [&](std::unique_ptr<sliger::json::Value> req,
 | 
					        [&](std::unique_ptr<sliger::json::Value> req,
 | 
				
			||||||
            std::unique_ptr<slige_rpc::BufferedWriter> writer) {
 | 
					            std::unique_ptr<sliger::rpc::BufferedWriter> writer) {
 | 
				
			||||||
            execute_action(std::move(req), std::move(writer));
 | 
					            execute_action(std::move(req), std::move(writer));
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    rpc.listen();
 | 
					    rpc.listen();
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,7 @@
 | 
				
			|||||||
#include <sys/socket.h>
 | 
					#include <sys/socket.h>
 | 
				
			||||||
#include <unistd.h>
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
auto slige_rpc::BufferedWriter::write(std::string message) -> Res<Unit>
 | 
					auto sliger::rpc::BufferedWriter::write(std::string message) -> Res<Unit>
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    for (size_t i = 0; i < message.length(); ++i) {
 | 
					    for (size_t i = 0; i < message.length(); ++i) {
 | 
				
			||||||
        auto res = this->write((uint8_t)message[i]);
 | 
					        auto res = this->write((uint8_t)message[i]);
 | 
				
			||||||
@ -14,7 +14,7 @@ auto slige_rpc::BufferedWriter::write(std::string message) -> Res<Unit>
 | 
				
			|||||||
    return Unit {};
 | 
					    return Unit {};
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
auto slige_rpc::BufferedWriter::write(uint8_t byte) -> Res<Unit>
 | 
					auto sliger::rpc::BufferedWriter::write(uint8_t byte) -> Res<Unit>
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (this->occupied >= length) {
 | 
					    if (this->occupied >= length) {
 | 
				
			||||||
        auto res = this->flush();
 | 
					        auto res = this->flush();
 | 
				
			||||||
@ -26,7 +26,7 @@ auto slige_rpc::BufferedWriter::write(uint8_t byte) -> Res<Unit>
 | 
				
			|||||||
    return Unit {};
 | 
					    return Unit {};
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
auto slige_rpc::BufferedWriter::flush() -> Res<size_t>
 | 
					auto sliger::rpc::BufferedWriter::flush() -> Res<size_t>
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    auto result = ::write(this->fd, this->buffer, this->occupied);
 | 
					    auto result = ::write(this->fd, this->buffer, this->occupied);
 | 
				
			||||||
    if (result < 0) {
 | 
					    if (result < 0) {
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,7 @@
 | 
				
			|||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <unistd.h>
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace slige_rpc {
 | 
					namespace sliger::rpc {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Err {
 | 
					struct Err {
 | 
				
			||||||
    std::string msg;
 | 
					    std::string msg;
 | 
				
			||||||
@ -112,31 +112,38 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    auto listen() -> Res<Unit>
 | 
					    auto listen() -> Res<Unit>
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        int socket;
 | 
				
			||||||
        int socket_fd = ::socket(AF_INET, SOCK_STREAM, 0);
 | 
					 | 
				
			||||||
        if (socket_fd < 0) {
 | 
					 | 
				
			||||||
            return Err { "could not get socket" };
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            auto address = create_address(13370);
 | 
					            socket = ::socket(AF_INET, SOCK_STREAM, 0);
 | 
				
			||||||
            if (::bind(socket_fd, (struct sockaddr*)&address, sizeof(address))
 | 
					            if (socket < 0) {
 | 
				
			||||||
                < 0) {
 | 
					                return Err { .msg
 | 
				
			||||||
                return Err { "could not bind" };
 | 
					                    = std::format("could not get socket ({})", socket) };
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
        if (::listen(socket_fd, 0) < 0) {
 | 
					            auto address = create_address(13370);
 | 
				
			||||||
            return Err { "could not listen" };
 | 
					            auto err
 | 
				
			||||||
 | 
					                = ::bind(socket, (struct sockaddr*)&address, sizeof(address));
 | 
				
			||||||
 | 
					            if (err < 0) {
 | 
				
			||||||
 | 
					                return Err { .msg = std::format("could not bind ({})", err) };
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					            return Unit {};
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            auto err = ::listen(socket, 0);
 | 
				
			||||||
 | 
					            if (err < 0) {
 | 
				
			||||||
 | 
					                return Err { .msg = std::format("could not listen ({})", err) };
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        while (true) {
 | 
					        while (true) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            auto client_address = create_address(13370);
 | 
					            auto client_address = create_address(13370);
 | 
				
			||||||
            socklen_t address_size = sizeof(client_address);
 | 
					            socklen_t address_size = sizeof(client_address);
 | 
				
			||||||
            int client = ::accept(
 | 
					            int client = ::accept(
 | 
				
			||||||
                socket_fd, (struct sockaddr*)&client_address, &address_size);
 | 
					                socket, (struct sockaddr*)&client_address, &address_size);
 | 
				
			||||||
            if (client < 0) {
 | 
					            if (client < 0) {
 | 
				
			||||||
                return Err { "could not accept" };
 | 
					                return Err { .msg
 | 
				
			||||||
 | 
					                    = std::format("could not accept ({})", client) };
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            const size_t buf_len = 1024;
 | 
					            const size_t buf_len = 1024;
 | 
				
			||||||
            int8_t buffer[buf_len] = {};
 | 
					            int8_t buffer[buf_len] = {};
 | 
				
			||||||
@ -145,11 +152,12 @@ public:
 | 
				
			|||||||
            while (true) {
 | 
					            while (true) {
 | 
				
			||||||
                ssize_t bytes_read = read(client, buffer, buf_len);
 | 
					                ssize_t bytes_read = read(client, buffer, buf_len);
 | 
				
			||||||
                if (bytes_read < 0) {
 | 
					                if (bytes_read < 0) {
 | 
				
			||||||
                    return Err { "could not read" };
 | 
					                    return Err { .msg
 | 
				
			||||||
 | 
					                        = std::format("could not read ({})", bytes_read) };
 | 
				
			||||||
                } else if (bytes_read == 0) {
 | 
					                } else if (bytes_read == 0) {
 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                for (size_t i; i < (size_t)bytes_read; ++i) {
 | 
					                for (size_t i = 0; i < (size_t)bytes_read; ++i) {
 | 
				
			||||||
                    message += buffer[i];
 | 
					                    message += buffer[i];
 | 
				
			||||||
                    bracket_finder.feed(buffer[i]);
 | 
					                    bracket_finder.feed(buffer[i]);
 | 
				
			||||||
                    if (!bracket_finder.bracket_closed()) {
 | 
					                    if (!bracket_finder.bracket_closed()) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user