1 /*
2     Copyright (c) 2007-2011 iMatix Corporation
3     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
4 
5     This file is part of 0MQ.
6 
7     0MQ is free software; you can redistribute it and/or modify it under
8     the terms of the GNU Lesser General Public License as published by
9     the Free Software Foundation; either version 3 of the License, or
10     (at your option) any later version.
11 
12     0MQ is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU Lesser General Public License for more details.
16 
17     You should have received a copy of the GNU Lesser General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __ZMQ_TCP_CONNECTER_HPP_INCLUDED__
22 #define __ZMQ_TCP_CONNECTER_HPP_INCLUDED__
23 
24 #include "platform.hpp"
25 #include "fd.hpp"
26 
27 #ifdef ZMQ_HAVE_WINDOWS
28 #include "windows.hpp"
29 #else
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #endif
33 
34 namespace zmq
35 {
36 
37     //  The class encapsulating simple TCP listening socket.
38 
39     class tcp_connecter_t
40     {
41     public:
42 
43         tcp_connecter_t ();
44         ~tcp_connecter_t ();
45 
46         //  Set address to connect to.
47         int set_address (const char *protocol, const char *addr_);
48 
49         //  Open TCP connecting socket. Address is in
50         //  <hostname>:<port-number> format. Returns -1 in case of error,
51         //  0 if connect was successfull immediately and 1 if async connect
52         //  was launched.
53         int open ();
54 
55         //  Close the connecting socket.
56         int close ();
57 
58         //  Get the file descriptor to poll on to get notified about
59         //  connection success.
60         fd_t get_fd ();
61 
62         //  Get the file descriptor of newly created connection. Returns
63         //  retired_fd if the connection was unsuccessfull.
64         fd_t connect ();
65 
66     private:
67 
68         //  Address to connect to.
69         sockaddr_storage addr;
70         socklen_t addr_len;
71 
72         //  Underlying socket.
73         fd_t s;
74 
75         tcp_connecter_t (const tcp_connecter_t&);
76         const tcp_connecter_t &operator = (const tcp_connecter_t&);
77     };
78 
79 }
80 
81 #endif
82