summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/sockets/sockets_extensions.h
blob: 9faa34e01192d62a24b98ae6c77c8e8cd588315d (plain)
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
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_SOCKETS_EXTENSIONs_
#define DLIB_SOCKETS_EXTENSIONs_

#include <iosfwd>
#include <memory>
#include <string>

#include "../sockets.h"
#include "../smart_pointers/scoped_ptr.h"
#include "sockets_extensions_abstract.h"


namespace dlib
{

// ----------------------------------------------------------------------------------------

    class invalid_network_address : public dlib::error 
    { 
    public: 
        invalid_network_address(const std::string& msg) : dlib::error(msg) {};
    };

// ----------------------------------------------------------------------------------------

    struct network_address
    {
        network_address() : port(0){}

        network_address(
            const std::string& full_address
        );

        network_address (
            const char* full_address
        )
        {
            *this = network_address(std::string(full_address));
        }

        network_address(
            const std::string& host_address_,
            const unsigned short port_
        ) : host_address(host_address_), port(port_) {}
            
        std::string host_address;
        unsigned short port;
    };

// ----------------------------------------------------------------------------------------

    inline bool operator < (
        const network_address& a,
        const network_address& b
    ) 
    {
        if (a.host_address < b.host_address)
            return true;
        else if (a.host_address > b.host_address)
            return false;
        else if (a.port < b.port)
            return true;
        else
            return false;
    }

    inline bool operator== (
        const network_address& a,
        const network_address& b
    ) { return a.host_address == b.host_address && a.port == b.port; }

    inline bool operator != (
        const network_address& a,
        const network_address& b
    ) { return !(a == b); }

// ----------------------------------------------------------------------------------------

    void serialize(
        const network_address& item,
        std::ostream& out
    );

    void deserialize(
        network_address& item,
        std::istream& in 
    );

    std::ostream& operator<< (
        std::ostream& out,
        const network_address& item
    );

    std::istream& operator>> (
        std::istream& in,
        network_address& item
    );

// ----------------------------------------------------------------------------------------

    connection* connect (
        const std::string& host_or_ip,
        unsigned short port
    );

// ----------------------------------------------------------------------------------------

    connection* connect (
        const network_address& addr
    );

// ----------------------------------------------------------------------------------------

    connection* connect (
        const std::string& host_or_ip,
        unsigned short port,
        unsigned long timeout
    );

// ----------------------------------------------------------------------------------------

    bool is_ip_address (
        std::string ip
    );

// ----------------------------------------------------------------------------------------

    void close_gracefully (
        connection* con,
        unsigned long timeout = 500
    );

// ----------------------------------------------------------------------------------------

    void close_gracefully (
        std::unique_ptr<connection>& con,
        unsigned long timeout = 500
    );

// ----------------------------------------------------------------------------------------

}

#ifdef NO_MAKEFILE
#include "sockets_extensions.cpp"
#endif

#endif // DLIB_SOCKETS_EXTENSIONs_