source: xplcommon/src/xplcommon/posix/Connector.h@ 6:81a0ade149e1

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

Implemented basic POSIX socket infrastructure

File size: 4.4 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 <cstdlib>
37
38//------------------------------------------------------------------------------
39
40struct sockaddr;
41
42//------------------------------------------------------------------------------
43
44namespace xplcommon { namespace posix {
45
46//------------------------------------------------------------------------------
47
48class ClientSocket;
49
50//------------------------------------------------------------------------------
51
52/**
53 * Class representing the creation of a connection to some remote
54 * entity, like a socket or a pipe.
55 */
56class Connector : public Failable
57{
58private:
59 /**
60 * The socket on behalf of which the connection is being
61 * performed.
62 */
63 ClientSocket& socket;
64
65 /**
66 * Indicate that we are in the process of connecting.
67 */
68 bool connecting;
69
70 /**
71 * Indicate that we are connected.
72 */
73 bool connected;
74
75protected:
76 /**
77 * Construct the connector.
78 */
79 Connector(ClientSocket* socket);
80
81public:
82 /**
83 * Connect to an address properly set up.
84 *
85 * If the connection succeeds immediately, it indicates success.
86 *
87 * If the connection does not succeed, because it would block, it
88 * sets up the socket to wait for the connection to succeed, and
89 * returns failure.
90 *
91 * If the connection fails for another reason, the error code is
92 * set, and false is returned.
93 */
94 bool connect();
95
96protected:
97 /**
98 * Get the parameters for connecting.
99 */
100 virtual const struct sockaddr* getAddress(size_t& addrlen) = 0;
101
102 /**
103 * Handle the event that the socket is writable.
104 *
105 * If connection is in progress, this function returns
106 * immediately.
107 *
108 * Otherwise it reads the error code using getsockopt(), and if it
109 * indicates success, the connector is set to connected, otherwise
110 * it is set to failed.
111 */
112 void handleWritable();
113
114 friend class ClientSocket;
115};
116
117//------------------------------------------------------------------------------
118// Inline definitions
119//------------------------------------------------------------------------------
120
121inline Connector::Connector(ClientSocket* socket) :
122 socket(*socket),
123 connecting(false),
124 connected(false)
125{
126}
127
128//------------------------------------------------------------------------------
129
130} /* namespace xplcommon::posix */ } /* namespace xplcommon */
131
132//------------------------------------------------------------------------------
133#endif // XPLCOMMON_POSIX_CONNECTOR_H
134
135// Local Variables:
136// mode: C++
137// c-basic-offset: 4
138// indent-tabs-mode: nil
139// End:
Note: See TracBrowser for help on using the repository browser.