Qpid Proton C++  0.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
io.hpp
1 #ifndef SOCKET_IO_HPP
2 #define SOCKET_IO_HPP
3 
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements. See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership. The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License. You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied. See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  */
22 
23 #include <proton/connection_engine.hpp>
24 
25 namespace proton {
26 
28 
32 
33 namespace io {
34 
49 
51 PN_CPP_EXTERN void initialize();
52 
54 PN_CPP_EXTERN void finalize(); // nothrow
55 
57 struct guard {
58  guard() { initialize(); }
59  ~guard() { finalize(); }
60 };
61 
63 
65 typedef int64_t descriptor;
66 
69 PN_CPP_EXTERN extern const descriptor INVALID_DESCRIPTOR;
71 
73 PN_CPP_EXTERN std::string error_str();
74 
76 PN_CPP_EXTERN descriptor connect(const proton::url&);
77 
79 class listener {
80  public:
83  PN_CPP_EXTERN listener(const std::string& host, const std::string& port);
84  PN_CPP_EXTERN ~listener();
85 
88  PN_CPP_EXTERN descriptor accept(std::string& host, std::string& port);
89 
91  descriptor accept() { std::string dummy; return accept(dummy, dummy); }
92 
94  descriptor socket() const { return socket_; }
95 
96  private:
97  guard guard_;
98  listener(const listener&);
99  listener& operator=(const listener&);
100  descriptor socket_;
101 };
102 
105  public:
107  PN_CPP_EXTERN socket_engine(descriptor socket_, handler&, const connection_options& = no_opts);
108 
110  PN_CPP_EXTERN socket_engine(const url&, handler&, const connection_options& = no_opts);
111 
113  descriptor socket() const { return socket_; }
114 
116  PN_CPP_EXTERN void run();
117 
118  protected:
119  PN_CPP_EXTERN size_t io_read(char* buf, size_t max);
120  PN_CPP_EXTERN size_t io_write(const char*, size_t);
121  PN_CPP_EXTERN void io_close();
122 
123  private:
124  void init();
125  guard guard_;
126  descriptor socket_;
127 };
128 
129 }} // proton::io
130 
131 #endif // SOCKET_IO_HPP
descriptor socket() const
Convert to descriptor.
Definition: io.hpp:94
listener(const std::string &host, const std::string &port)
Listen on host/port.
An interface for connection-oriented IO integration.
Definition: connection_engine.hpp:64
void finalize()
Finalize the proton::io subsystem.
void run()
Start the engine.
Use to call io::initialize and io::finalize around a scope.
Definition: io.hpp:57
descriptor socket() const
Get the socket descriptor.
Definition: io.hpp:113
Options for creating a connection.
Definition: connection_options.hpp:60
Listening socket.
Definition: io.hpp:79
A proton URL.
Definition: url.hpp:51
std::string error_str()
Return a string describing the most recent IO error.
Callback functions for handling proton events.
Definition: handler.hpp:40
void initialize()
Initialize the proton::io subsystem.
socket_engine(descriptor socket_, handler &, const connection_options &=no_opts)
Wrap an open socket. Sets non-blocking mode.
descriptor connect(const proton::url &)
Open a TCP connection to the host:port (port can be a service name or number) from a proton::url...
int64_t descriptor
An IO resource.
Definition: io.hpp:65
A connection_engine for socket-based IO.
Definition: io.hpp:104
descriptor accept()
Accept a connection, does not provide address info.
Definition: io.hpp:91