source: xplcommon/src/xplcommon/posix/Connector.h@ 14:3caa1d3122db

Last change on this file since 14:3caa1d3122db was 14:3caa1d3122db, checked in by István Váradi <ivaradi@…>, 11 years ago

Moved the storage of the error code to where it belongs

File size: 4.9 KB
Line 
1// Copyright (c) 2012 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30#ifndef XPLCOMMON_POSIX_CONNECTOR_H
31#define XPLCOMMON_POSIX_CONNECTOR_H
32//------------------------------------------------------------------------------
33
34#include "../Failable.h"
35
36#include "ClientSocket.h"
37
38#include <cstdlib>
39
40//------------------------------------------------------------------------------
41
42struct sockaddr;
43
44//------------------------------------------------------------------------------
45
46namespace xplcommon { namespace posix {
47
48//------------------------------------------------------------------------------
49
50/**
51 * Class representing the creation of a connection to some remote
52 * entity, like a socket or a pipe.
53 */
54class Connector : public FailableReference<Connector>
55{
56private:
57 /**
58 * The socket on behalf of which the connection is being
59 * performed.
60 */
61 ClientSocket& socket;
62
63 /**
64 * Indicate that we are in the process of connecting.
65 */
66 bool connecting;
67
68 /**
69 * Indicate that we are connected.
70 */
71 bool connected;
72
73protected:
74 /**
75 * Construct the connector.
76 */
77 Connector(ClientSocket* socket);
78
79public:
80 /**
81 * Connect to an address properly set up.
82 *
83 * If the connection succeeds immediately, it indicates success.
84 *
85 * If the connection does not succeed, because it would block, it
86 * sets up the socket to wait for the connection to succeed, and
87 * returns failure.
88 *
89 * If the connection fails for another reason, the error code is
90 * set, and false is returned.
91 */
92 bool connect();
93
94protected:
95 /**
96 * Get the parameters for connecting.
97 */
98 virtual const struct sockaddr* getAddress(size_t& addrlen) = 0;
99
100 /**
101 * Handle the event that the socket is writable.
102 *
103 * If connection is in progress, this function returns
104 * immediately.
105 *
106 * Otherwise it reads the error code using getsockopt(), and if it
107 * indicates success, the connector is set to connected, otherwise
108 * it is set to failed.
109 */
110 void handleWritable();
111
112private:
113 /**
114 * Get the failable object.
115 */
116 const ::xplcommon::Failable& getFailable() const;
117
118 /**
119 * Get the failable object.
120 */
121 ::xplcommon::Failable& getFailable();
122
123 friend class FailableReference<Connector>;
124 friend class ClientSocket;
125};
126
127//------------------------------------------------------------------------------
128// Inline definitions
129//------------------------------------------------------------------------------
130
131inline Connector::Connector(ClientSocket* socket) :
132 socket(*socket),
133 connecting(false),
134 connected(false)
135{
136}
137
138//------------------------------------------------------------------------------
139
140inline const ::xplcommon::Failable& Connector::getFailable() const
141{
142 return socket;
143}
144
145//------------------------------------------------------------------------------
146
147inline ::xplcommon::Failable& Connector::getFailable()
148{
149 return socket;
150}
151
152//------------------------------------------------------------------------------
153
154} /* namespace xplcommon::posix */ } /* namespace xplcommon */
155
156//------------------------------------------------------------------------------
157#endif // XPLCOMMON_POSIX_CONNECTOR_H
158
159// Local Variables:
160// mode: C++
161// c-basic-offset: 4
162// indent-tabs-mode: nil
163// End:
Note: See TracBrowser for help on using the repository browser.