forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerSocket.h
62 lines (58 loc) · 1.87 KB
/
ServerSocket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include "ErrorCodes.h"
#include "AbortChecker.h"
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
namespace facebook {
namespace wdt {
class ServerSocket {
public:
ServerSocket(ServerSocket &&that) noexcept;
ServerSocket(const ServerSocket &that) = delete;
ServerSocket(int32_t port, int backlog, IAbortChecker const *abortChecker);
ServerSocket &operator=(const ServerSocket &that) = delete;
ServerSocket &operator=(ServerSocket &&that);
virtual ~ServerSocket();
/// Sets up listening socket (first wildcard type (ipv4 or ipv6 depending
/// on flag)).
ErrorCode listen();
/// will accept next (/only) incoming connection
ErrorCode acceptNextConnection(int timeoutMillis);
/// tries to read nbyte data and periodically checks for abort
int read(char *buf, int nbyte, bool tryFull = true);
/// tries to write nbyte data and periodically checks for abort
int write(const char *buf, int nbyte, bool tryFull = true);
/// @return peer ip
std::string getPeerIp() const;
/// @return peer port
std::string getPeerPort() const;
int getFd() const;
int getListenFd() const;
int closeCurrentConnection();
int32_t getPort() const;
int getBackLog() const;
/// Destroy the active connection and the listening fd
/// if done by the same thread who owned the socket
void closeAll();
private:
int32_t port_;
const int backlog_;
int listeningFd_;
int fd_;
std::string peerIp_;
std::string peerPort_;
struct addrinfo sa_;
IAbortChecker const *abortChecker_;
};
}
} // namespace facebook::wdt