| 1052 | |
| 1053 | |
| 1054 | class Server |
| 1055 | { |
| 1056 | public: |
| 1057 | // Options for creating a server. |
| 1058 | // |
| 1059 | // NOTE: until GCC 5.0 default member initializers prevented the |
| 1060 | // class from being an aggregate which prevented you from being able |
| 1061 | // to use aggregate initialization, thus we introduce and use |
| 1062 | // `DEFAULT_CREATE_OPTIONS` for the default parameter of `create`. |
| 1063 | struct CreateOptions |
| 1064 | { |
| 1065 | Scheme scheme; |
| 1066 | size_t backlog; |
| 1067 | }; |
| 1068 | |
| 1069 | static CreateOptions DEFAULT_CREATE_OPTIONS() |
| 1070 | { |
| 1071 | return { |
| 1072 | /* .scheme = */ Scheme::HTTP, |
| 1073 | /* .backlog = */ 16384, |
| 1074 | }; |
| 1075 | }; |
| 1076 | |
| 1077 | // Options for stopping a server. |
| 1078 | // |
| 1079 | // NOTE: see note above as to why we have `DEFAULT_STOP_OPTIONS`. |
| 1080 | struct StopOptions |
| 1081 | { |
| 1082 | // During the grace period: |
| 1083 | // * No new sockets will be accepted (but on OS X they'll still queue). |
| 1084 | // * Existing sockets will be shut down for reads to prevent new |
| 1085 | // requests from arriving. |
| 1086 | // * Existing sockets will be shut down after already received |
| 1087 | // requests have their responses sent. |
| 1088 | // After the grace period connections will be forcibly shut down. |
| 1089 | Duration grace_period; |
| 1090 | }; |
| 1091 | |
| 1092 | static StopOptions DEFAULT_STOP_OPTIONS() |
| 1093 | { |
| 1094 | return { |
| 1095 | /* .grace_period = */ Seconds(0), |
| 1096 | }; |
| 1097 | }; |
| 1098 | |
| 1099 | static Try<Server> create( |
| 1100 | network::Socket socket, |
| 1101 | std::function<Future<Response>( |
| 1102 | const network::Socket& socket, |
| 1103 | const Request&)>&& f, |
| 1104 | const CreateOptions& options = DEFAULT_CREATE_OPTIONS()); |
| 1105 | |
| 1106 | template <typename F> |
| 1107 | static Try<Server> create( |
| 1108 | network::Socket socket, |
| 1109 | F&& f, |
| 1110 | const CreateOptions& options = DEFAULT_CREATE_OPTIONS()) |
| 1111 | { |