Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / rest / connection.hpp
1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
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.
15  *
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.
27  */
28
29 /**
30  * @file
31  *   This file includes connection definition for RESTful HTTP server.
32  */
33
34 #ifndef OTBR_REST_CONNECTION_HPP_
35 #define OTBR_REST_CONNECTION_HPP_
36
37 #include <string.h>
38 #include <unistd.h>
39
40 #include "rest/parser.hpp"
41 #include "rest/resource.hpp"
42
43 using std::chrono::steady_clock;
44
45 namespace otbr {
46 namespace rest {
47
48 /**
49  * This class implements a Connection class of each socket connection.
50  *
51  */
52 class Connection
53 {
54 public:
55     /**
56      * The constructor is to initialize a socket connection instance.
57      *
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.
62      *
63      */
64     Connection(steady_clock::time_point aStartTime, Resource *aResource, int aFd);
65
66     /**
67      * This method initializes the connection.
68      *
69      *
70      */
71     void Init(void);
72
73     /**
74      * This method performs processing.
75      *
76      * @param[in]       aReadFdSet    The read file descriptors.
77      * @param[in]       aWriteFdSet   The write file descriptors.
78      *
79      */
80     void Process(fd_set &aReadFdSet, fd_set &aWriteFdSet);
81
82     /**
83      * This method updates the file descriptor sets and timeout for mainloop.
84      *
85      * @param[inout]    aMainloop   A reference to OpenThread mainloop context.
86      *
87      */
88     void UpdateFdSet(otSysMainloopContext &aMainloop) const;
89
90     /**
91      * This method indicates whether this connection no longer need to be processed.
92      *
93      * @retval  true     This connection could be released in next loop.
94      * @retval  false    This connection still needs to be processed in next loop.
95      *
96      */
97     bool IsComplete(void) const;
98
99 private:
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);
106     void Write(void);
107     void Handle(void);
108     void Disconnect(void);
109
110     // Timestamp used for each check point of a connection
111     steady_clock::time_point mTimeStamp;
112
113     // File descriptor for this connection
114     int mFd;
115
116     // Enum indicates the state of this connection
117     ConnectionState mState;
118
119     // Response instance binded to this connection
120     Response mResponse;
121
122     // Request instance binded to this connection
123     Request mRequest;
124
125     // HTTP parser instance
126     Parser mParser;
127
128     // Resource handler instance
129     Resource *mResource;
130
131     // Write buffer in case write multiple times
132     std::string mWriteContent;
133 };
134
135 } // namespace rest
136 } // namespace otbr
137
138 #endif // OTBR_REST_CONNECTION_HPP_