2 * Copyright (c) 2020, The OpenThread Authors.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
31 * This file includes connection definition for RESTful HTTP server.
34 #ifndef OTBR_REST_CONNECTION_HPP_
35 #define OTBR_REST_CONNECTION_HPP_
40 #include "rest/parser.hpp"
41 #include "rest/resource.hpp"
43 using std::chrono::steady_clock;
49 * This class implements a Connection class of each socket connection.
56 * The constructor is to initialize a socket connection instance.
58 * @param[in] aStartTime The reference start time of a conneciton which is set when created for the first time
59 * and maybe reset when transfer to wait callback or wait write state.
60 * @param[in] aResource A pointer to the resource handler.
61 * @param[in] aFd The file descriptor for the conneciton.
64 Connection(steady_clock::time_point aStartTime, Resource *aResource, int aFd);
67 * This method initializes the connection.
74 * This method performs processing.
76 * @param[in] aReadFdSet The read file descriptors.
77 * @param[in] aWriteFdSet The write file descriptors.
80 void Process(fd_set &aReadFdSet, fd_set &aWriteFdSet);
83 * This method updates the file descriptor sets and timeout for mainloop.
85 * @param[inout] aMainloop A reference to OpenThread mainloop context.
88 void UpdateFdSet(otSysMainloopContext &aMainloop) const;
91 * This method indicates whether this connection no longer need to be processed.
93 * @retval true This connection could be released in next loop.
94 * @retval false This connection still needs to be processed in next loop.
97 bool IsComplete(void) const;
100 void UpdateReadFdSet(fd_set &aReadFdSet, int &aMaxFd) const;
101 void UpdateWriteFdSet(fd_set &aWriteFdSet, int &aMaxFd) const;
102 void UpdateTimeout(timeval &aTimeout) const;
103 void ProcessWaitRead(fd_set &aReadFdSet);
104 void ProcessWaitCallback(void);
105 void ProcessWaitWrite(fd_set &aWriteFdSet);
108 void Disconnect(void);
110 // Timestamp used for each check point of a connection
111 steady_clock::time_point mTimeStamp;
113 // File descriptor for this connection
116 // Enum indicates the state of this connection
117 ConnectionState mState;
119 // Response instance binded to this connection
122 // Request instance binded to this connection
125 // HTTP parser instance
128 // Resource handler instance
131 // Write buffer in case write multiple times
132 std::string mWriteContent;
138 #endif // OTBR_REST_CONNECTION_HPP_