LCOV - code coverage report
Current view: top level - include/hpactor/net - acceptor.hpp (source / functions) Coverage Total Hit
Test: HPActor Coverage Lines: 100.0 % 6 6
Test Date: 2026-05-20 02:24:49 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright 2026 HPActor Contributors
       2                 :             : //
       3                 :             : // Licensed under the Apache License, Version 2.0 (the "License");
       4                 :             : // you may not use this file except in compliance with the License.
       5                 :             : // You may obtain a copy of the License at
       6                 :             : //
       7                 :             : //     http://www.apache.org/licenses/LICENSE-2.0
       8                 :             : //
       9                 :             : // Unless required by applicable law or agreed to in writing, software
      10                 :             : // distributed under the License is distributed on an "AS IS" BASIS,
      11                 :             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12                 :             : // See the License for the specific language governing permissions and
      13                 :             : // limitations under the License.
      14                 :             : 
      15                 :             : #pragma once
      16                 :             : 
      17                 :             : #include <hpactor/net/event_loop.hpp>
      18                 :             : #include <hpactor/types/types.hpp>
      19                 :             : 
      20                 :             : #include <cstdint>
      21                 :             : #include <functional>
      22                 :             : #include <memory>
      23                 :             : #include <string>
      24                 :             : 
      25                 :             : namespace hpactor {
      26                 :             : 
      27                 :             : namespace net {
      28                 :             : 
      29                 :             : // -----------------------------------------------------------------------------
      30                 :             : // AcceptorInfo - information about a server acceptor
      31                 :             : // -----------------------------------------------------------------------------
      32                 :             : struct AcceptorInfo {
      33                 :             :     uint16_t port = 0;
      34                 :             :     uint8_t handshake_version = 0;
      35                 :             :     uint8_t protocol_version = 0;
      36                 :             :     bool tls_required = false;
      37                 :             : };
      38                 :             : 
      39                 :             : // -----------------------------------------------------------------------------
      40                 :             : // Acceptor - abstract base for server socket listeners
      41                 :             : // -----------------------------------------------------------------------------
      42                 :             : class Acceptor {
      43                 :             :   public:
      44                 :             :     using accept_handler =
      45                 :             :         std::function<void(int client_fd, EndPoint /*remote_endpoint_hint*/)>;
      46                 :             : 
      47                 :             :     explicit Acceptor(EventLoop* loop);
      48                 :             :     virtual ~Acceptor();
      49                 :             : 
      50                 :             :     // Non-copyable
      51                 :             :     Acceptor(const Acceptor&) = delete;
      52                 :             :     Acceptor& operator=(const Acceptor&) = delete;
      53                 :             : 
      54                 :             :     // Stop listening and close the socket
      55                 :             :     virtual void close();
      56                 :             : 
      57                 :             :     // Set handler for accepted connections
      58                 :             :     void set_accept_handler(accept_handler handler);
      59                 :             : 
      60                 :             :     // Check if listening
      61                 :           7 :     bool is_listening() const {
      62                 :           7 :         return listening_fd_ >= 0;
      63                 :             :     }
      64                 :             : 
      65                 :             :   protected:
      66                 :             :     virtual void handle_read() = 0;
      67                 :             : 
      68                 :             :     EventLoop* loop_;
      69                 :             :     int listening_fd_ = -1;
      70                 :             :     accept_handler accept_handler_;
      71                 :             : };
      72                 :             : 
      73                 :             : // -----------------------------------------------------------------------------
      74                 :             : // TcpAcceptor - TCP socket acceptor
      75                 :             : // -----------------------------------------------------------------------------
      76                 :             : class TcpAcceptor : public Acceptor {
      77                 :             :   public:
      78                 :             :     using Acceptor::Acceptor;
      79                 :             : 
      80                 :             :     // Start listening on the specified port.
      81                 :             :     // bind_address: IPv4 address to bind to (default "0.0.0.0" = INADDR_ANY).
      82                 :             :     // Returns true on success, false on failure.
      83                 :             :     bool listen(uint16_t port, uint16_t port_range = 0,
      84                 :             :                 const std::string& bind_address = "0.0.0.0");
      85                 :             : 
      86                 :             :     // Get the bound port
      87                 :           2 :     uint16_t port() const {
      88                 :           2 :         return bound_port_;
      89                 :             :     }
      90                 :             : 
      91                 :             :   protected:
      92                 :             :     void handle_read() override;
      93                 :             : 
      94                 :             :   private:
      95                 :             :     uint16_t bound_port_ = 0;
      96                 :             : };
      97                 :             : 
      98                 :             : // -----------------------------------------------------------------------------
      99                 :             : // UnixDomainAcceptor - Unix domain socket acceptor
     100                 :             : // -----------------------------------------------------------------------------
     101                 :             : class UnixDomainAcceptor : public Acceptor {
     102                 :             :   public:
     103                 :             :     using Acceptor::Acceptor;
     104                 :             : 
     105                 :             :     // Start listening on a UNIX domain socket
     106                 :             :     // Returns true on success, false on failure
     107                 :             :     bool listen(const std::string& path);
     108                 :             : 
     109                 :             :     // Get UDS socket path if listening on UDS
     110                 :          23 :     std::string uds_path() const {
     111                 :          23 :         return uds_path_;
     112                 :             :     }
     113                 :             : 
     114                 :             :     // Stop listening, unlink socket file, and close the fd
     115                 :             :     void close() override;
     116                 :             : 
     117                 :             :   protected:
     118                 :             :     void handle_read() override;
     119                 :             : 
     120                 :             :   private:
     121                 :             :     std::string uds_path_;
     122                 :             : };
     123                 :             : 
     124                 :             : } // namespace net
     125                 :             : } // namespace hpactor
        

Generated by: LCOV version 2.0-1