1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

/*
SOCOP - SOcket COntrol Protocol
===============================

*/

#ifndef SOCOP_HPP_INCLUDED
#define SOCOP_HPP_INCLUDED

#include <gate/interfaces/socop.h>


/// @brief 
class Socop
{
public:
    enum Result
    {
        Ok                  = SOCOP_OK,
        Error               = SOCOP_ERROR,
        Error_NotImpl       = SOCOP_ERROR_NOTIMPL,
        Error_IO            = SOCOP_ERROR_IO,
        Error_Timeout       = SOCOP_ERROR_TIMEOUT,
        Error_InvalidData   = SOCOP_ERROR_INVALIDDATA,
        Error_OutOfBounds   = SOCOP_ERROR_OUTOFBOUNDS,
        Error_WrongMessage  = SOCOP_ERROR_WRONGMESSAGE,
        Error_ResourceError = SOCOP_ERROR_RESOURCEERROR,
        Error_Corruption    = SOCOP_ERROR_CORRUPTION,
        Error_InvalidArg    = SOCOP_ERROR_INVALIDARG,
        Error_Failed        = SOCOP_ERROR_FAILED
    };

    enum Flags
    {
        Flag_Receive    = SOCOP_MSG_FLAG_RECV,
        Flag_Send       = SOCOP_MSG_FLAG_SEND,
        Flag_Error      = SOCOP_MSG_FLAG_ERROR
    };


    typedef socop_socket_t  Socket;

    typedef socop_addr_t    Address;

    typedef socop_word_t    word_t;

protected:
    static Result   translate_result(int result);
    static int      translate_result(Result result);

};


/// @brief 
class SocopDriver : public socop_driver_t, public Socop
{
public:
    typedef socop_read_byte_t   Bytereader;
    typedef socop_write_byte_t  Bytewriter;

    SocopDriver();

    SocopDriver(Bytereader reader_function, void* reader_param,
        Bytewriter writer_function, void* writer_param);
    virtual ~SocopDriver();

protected:
    virtual Socop::Result   do_read(unsigned char& received_byte, bool want_more);
    virtual Socop::Result   do_write(unsigned char byte_to_send, bool send_more);

    static int do_read_dispatcher(unsigned char* num, unsigned want_more, void* user_param);
    static int do_write_dispatcher(unsigned char num, unsigned send_more, void* user_param);

};


/// @brief 
class SocopClient : public Socop
{
public:
    SocopClient();
    SocopClient(SocopDriver& driver);

    void init(SocopDriver& driver);

    Result  reset();
    Result  net_status();
    Result  net_start(char const* parameters, unsigned parameters_length);
    Result  net_stop();

    Result  connect(Address const& addr, Socket& new_tcp_socket);
    Result  bind(Address const& addr, Socket& new_udp_socket);
    Result  listen(Address const& addr, Socket& new_listen_socket);
    Result  accept(Socket listen_socket, Socket& new_connection_socket);
    Result  close(Socket sock);

    Result  receive(Socket sock, void* buffer, unsigned buffer_length, unsigned& buffer_used);
    Result  receive_from(Socket sock, void* buffer, unsigned buffer_length, unsigned& buffer_used, Address& addr);
    Result  send(Socket sock, void const* buffer, unsigned buffer_length, unsigned& buffer_sent);
    Result  send_to(Socket sock, void const* buffer, unsigned buffer_length, unsigned& buffer_sent, Address const& addr);

    Result  shutdown(Socket sock, unsigned flags);
    Result  select(Socket sock, unsigned& flags, unsigned timeout_ms);
    Result  peer(Socket sock, Address& addr);
    Result  iocontrol(Socket sock, unsigned option, void* buffer, unsigned buffer_len);

private:
    socop_client_t	impl;

};


class SocopServerDispatcher;


/// @brief 
class SocopServer : public Socop
{
public:
    friend class SocopServerDispatcher;

    SocopServer();
    SocopServer(SocopDriver const& driver);
    virtual ~SocopServer();

    void init(SocopDriver const& driver);

    Result	chat();

protected:
    virtual Result	on_reset() = 0;
    virtual Result	on_net_status() = 0;
    virtual Result	on_net_start(char const* parameter, unsigned parameter_length) = 0;
    virtual Result	on_net_stop() = 0;

    virtual Result	on_connect(Address const& addr, Socket& new_sock) = 0;
    virtual Result	on_bind(Address const& addr, Socket& new_sock) = 0;
    virtual Result	on_listen(Address const& addr, Socket& new_sock) = 0;
    virtual Result	on_accept(Socket listen_sock, Socket& new_sock) = 0;
    virtual Result	on_close(Socket sock) = 0;

    virtual Result	on_receive(Socket sock, void* buffer, unsigned buffer_length, unsigned& buffer_used) = 0;
    virtual Result	on_receive_from(Socket sock, void* buffer, unsigned buffer_length, unsigned& buffer_used, Address& addr) = 0;
    virtual Result	on_send(Socket sock, void const* buffer, unsigned buffer_length, unsigned& buffer_sent) = 0;
    virtual Result	on_send_to(Socket sock, void const* buffer, unsigned buffer_length, unsigned& buffer_sent, Address const& addr) = 0;
    virtual Result	on_shutdown(Socket sock, unsigned flags) = 0;
    virtual Result	on_select(Socket sock, unsigned& flags) = 0;
    virtual Result	on_peer(Socket sock) = 0;
    virtual Result	on_iocontrol(Socket sock) = 0;

private:
    socop_server_t	impl;
};


#endif