LCOV - code coverage report
Current view: top level - src/net - Socket.hpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 1 1 100.0 %
Date: 2025-04-27 01:14:20 Functions: 1 1 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : ** Copyright (C) 2023 Sylvain Fargier
       3                 :            : **
       4                 :            : ** This software is provided 'as-is', without any express or implied
       5                 :            : ** warranty.  In no event will the authors be held liable for any damages
       6                 :            : ** arising from the use of this software.
       7                 :            : **
       8                 :            : ** Permission is granted to anyone to use this software for any purpose,
       9                 :            : ** including commercial applications, and to alter it and redistribute it
      10                 :            : ** freely, subject to the following restrictions:
      11                 :            : **
      12                 :            : ** 1. The origin of this software must not be misrepresented; you must not
      13                 :            : **    claim that you wrote the original software. If you use this software
      14                 :            : **    in a product, an acknowledgment in the product documentation would be
      15                 :            : **    appreciated but is not required.
      16                 :            : ** 2. Altered source versions must be plainly marked as such, and must not be
      17                 :            : **    misrepresented as being the original software.
      18                 :            : ** 3. This notice may not be removed or altered from any source distribution.
      19                 :            : **
      20                 :            : ** Created on: 2023-12-16T12:03:48
      21                 :            : ** Author: Sylvain Fargier <fargier.sylvain@gmail.com>
      22                 :            : */
      23                 :            : 
      24                 :            : #ifndef IPSOCKET_HPP__
      25                 :            : #define IPSOCKET_HPP__
      26                 :            : 
      27                 :            : #include <cstdint>
      28                 :            : #include <string>
      29                 :            : 
      30                 :            : #include "../Buffer.hpp"
      31                 :            : #include "../Cow.hpp"
      32                 :            : #include "../Pipe.hpp"
      33                 :            : #include "../Signal.hpp"
      34                 :            : #include "../Thread.hpp"
      35                 :            : #include "Addr.hpp"
      36                 :            : 
      37                 :            : namespace ccut {
      38                 :            : 
      39                 :            : namespace net {
      40                 :            : 
      41                 :            : /**
      42                 :            :  * @brief base class for sockets
      43                 :            :  * @details do use the `Socket<Message>` template helper to implement your own
      44                 :            :  * socket
      45                 :            :  */
      46                 :            : class BaseSocket : public ccut::Thread
      47                 :            : {
      48                 :            : public:
      49                 :            :     BaseSocket(const std::string &name = "BaseSocket");
      50                 :            : 
      51                 :            :     void wake() override;
      52                 :            : 
      53                 :            :     /**
      54                 :            :      * @brief bind the ICMPSocket
      55                 :            :      * @details starts the processing thread on success
      56                 :            :      *
      57                 :            :      * @param port port to bind to (default: 0 == random)
      58                 :            :      * @param localhost host to bind to (default loopback)
      59                 :            :      * @throws ccut::Exception if already listening or on error
      60                 :            :      */
      61                 :            :     virtual void bind(uint16_t port = 0, bool localhost = true);
      62                 :            :     virtual void bind6(uint16_t port = 0, bool localhost = true);
      63                 :            : 
      64                 :            :     /**
      65                 :            :      * @brief current server address
      66                 :            :      *
      67                 :            :      * @return listening address, not valid if not listening
      68                 :            :      */
      69                 :            :     net::address_t addr() const;
      70                 :            : 
      71                 :            :     void setBufferSize(size_t size);
      72                 :            :     size_t getBufferSize() const;
      73                 :          3 :     int getSocket() const { return m_socket; }
      74                 :            : 
      75                 :            :     static constexpr size_t defaultBufferSize = 2048;
      76                 :            : 
      77                 :            : protected:
      78                 :            :     virtual int makeSocket(bool v6 = false) = 0;
      79                 :            : 
      80                 :            :     ccut::Pipe m_wakePipe;
      81                 :            :     int m_socket;
      82                 :            :     net::address_t m_addr;
      83                 :            :     size_t m_bufferSize;
      84                 :            : };
      85                 :            : 
      86                 :            : /**
      87                 :            :  * @brief Socket template helper
      88                 :            :  */
      89                 :            : template<typename M>
      90                 :            : class Socket : public BaseSocket
      91                 :            : {
      92                 :            : public:
      93                 :            :     explicit Socket(const std::string &name = "Socket");
      94                 :            :     virtual ~Socket();
      95                 :            : 
      96                 :            :     typedef M Message;
      97                 :            : 
      98                 :            :     virtual void send(const Message &msg);
      99                 :            : 
     100                 :            :     ccut::Signal<const Message &> message;
     101                 :            : 
     102                 :            : protected:
     103                 :            :     void thread_func() override;
     104                 :            : 
     105                 :            :     virtual Message processMessage(const address_t &from,
     106                 :            :                                    const address_t &to,
     107                 :            :                                    const cow_ptr<buffer_t> &buffer);
     108                 :            : };
     109                 :            : 
     110                 :            : } // namespace net
     111                 :            : } // namespace ccut
     112                 :            : 
     113                 :            : #include "Socket.hxx"
     114                 :            : 
     115                 :            : #endif

Generated by: LCOV version 1.14