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
#ifndef SOCOPCLI_MASTER_HPP_INCLUDED
#define SOCOPCLI_MASTER_HPP_INCLUDED

#include "gate/interfaces/socop.hpp"

#include "gate/net/sockets.hpp"
#include "gate/wrappers.hpp"
#include "gate/maps.hpp"
#include "gate/synchronization.hpp"
#include "gate/atomics.hpp"
#include "gate/streams.hpp"
#include "gate/threading.hpp"

class GATE_API_LOCAL SocopMaster : public SocopDriver, public SocopServer, public gate::IRunnable
{
public:
    SocopMaster() noexcept;
    virtual ~SocopMaster() noexcept;

    bool start(gate::Stream& strm) noexcept;
    bool stop() noexcept;
    bool isStarted() noexcept;

protected:	// implementation of SocopDriver
    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);

protected:	// implementation of SocopServer
    virtual Socop::Result	on_reset();
    virtual Socop::Result	on_net_status();
    virtual Socop::Result	on_net_start(char const* parameter, unsigned parameter_length);
    virtual Socop::Result	on_net_stop();

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

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

protected:
    virtual void run();

public:
    struct Resource
    {
        Socop::Socket		id;
        gate::net::Socket	native;

        Resource(gate::net::Socket::SocketTypeEnum type);
    };
    typedef gate::Ref<Resource> ResourceRef;

private:
    gate::AtomicInt server_state;
    gate::Thread server_thread;
    gate::Stream server_stream;

    gate::Mutex	res_mutex;
    gate::AtomicInt next_sock_id;
    typedef gate::Map<Socop::Socket, ResourceRef>	ResourceMap;
    ResourceMap	tcp_clients;
    ResourceMap	tcp_servers;
    ResourceMap	udp_sockets;

private:
    Socop::Socket	get_next_socket_id();
    ResourceRef		find_resource(Socop::Socket sock_id);


};

#endif