Debian Codegraph @

cpp/src/listener.cpp

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "proton/connection_options.hpp"
21 #include "proton/listener.hpp"
22 #include "proton/listen_handler.hpp"
23
24 #include <proton/listener.h>
25 #include <proton/netaddr.h>
26
27 #include "contexts.hpp"
28
29 #include <stdlib.h>
30
31 namespace proton {
32
33 listener::listener(): listener_(0) {}
34 listener::listener(pn_listener_t* l): listener_(l) {}
35 // Out-of-line big-3 with trivial implementations, in case we need them in future.
36 listener::listener(const listener& l) : listener_(l.listener_) {}
37 listener::~listener() = default;
38 listener& listener::operator=(const listener& l) { listener_ = l.listener_; return *this; }
39
40 void listener::stop() {
41 if (listener_) {
42 pn_listener_close(listener_);
43 listener_ = 0; // Further calls to stop() are no-op
44 }
45 }
46
47 int listener::port() {
48 if (!listener_) throw error("listener is closed");
49 char port[16] = "";
50 pn_netaddr_host_port(pn_listener_addr(listener_), NULL, 0, port, sizeof(port));
51 int i = atoi(port);
52 if (!i) throw error("listener has no port");
53 return i;
54 }
55
56 class container& listener::container() const {
57 if (!listener_) throw error("listener is closed");
58 void *c = pn_listener_get_context(listener_);
59 if (!c) throw proton::error("no container");
60 return *reinterpret_cast<class container*>(c);
61 }
62
63 // Listen handler
64 listen_handler::~listen_handler() = default;
65 void listen_handler::on_open(listener&) {}
66 connection_options listen_handler::on_accept(listener&) { return connection_options(); }
67 void listen_handler::on_error(listener&, const std::string& what) { throw proton::error(what); }
68 void listen_handler::on_close(listener&) {}
69 }