33 AINFO <<
"session has hold a valid fd[" << fd_ <<
"]";
36 int sock_fd = socket(domain, type | SOCK_NONBLOCK, protocol);
45 return listen(fd_, backlog);
51 return bind(fd_, addr, addrlen);
57 int sock_fd = accept4(fd_, addr, addrlen, SOCK_NONBLOCK);
58 while (sock_fd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
59 poll_handler_->Block(-1,
true);
60 sock_fd = accept4(fd_, addr, addrlen, SOCK_NONBLOCK);
67 return std::make_shared<Session>(sock_fd);
74 socklen_t optlen =
sizeof(optval);
75 int res = connect(fd_, addr, addrlen);
76 if (res == -1 && errno == EINPROGRESS) {
77 poll_handler_->Block(-1,
false);
78 getsockopt(fd_, SOL_SOCKET, SO_ERROR,
reinterpret_cast<void *
>(&optval),
92 poll_handler_->Unblock();
98ssize_t
Session::Recv(
void *buf,
size_t len,
int flags,
int timeout_ms) {
102 ssize_t nbytes = recv(fd_, buf, len, flags);
103 if (timeout_ms == 0) {
107 while (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
108 if (poll_handler_->Block(timeout_ms,
true)) {
109 nbytes = recv(fd_, buf, len, flags);
111 if (timeout_ms > 0) {
119 struct sockaddr *src_addr, socklen_t *addrlen,
124 ssize_t nbytes = recvfrom(fd_, buf, len, flags, src_addr, addrlen);
125 if (timeout_ms == 0) {
129 while (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
130 if (poll_handler_->Block(timeout_ms,
true)) {
131 nbytes = recvfrom(fd_, buf, len, flags, src_addr, addrlen);
133 if (timeout_ms > 0) {
140ssize_t
Session::Send(
const void *buf,
size_t len,
int flags,
int timeout_ms) {
144 ssize_t nbytes = send(fd_, buf, len, flags);
145 if (timeout_ms == 0) {
149 while ((nbytes == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) {
150 if (poll_handler_->Block(timeout_ms,
false)) {
151 nbytes = send(fd_, buf, len, flags);
153 if (timeout_ms > 0) {
161 const struct sockaddr *dest_addr, socklen_t addrlen,
164 ACHECK(dest_addr !=
nullptr);
167 ssize_t nbytes = sendto(fd_, buf, len, flags, dest_addr, addrlen);
168 if (timeout_ms == 0) {
172 while ((nbytes == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) {
173 if (poll_handler_->Block(timeout_ms,
false)) {
174 nbytes = sendto(fd_, buf, len, flags, dest_addr, addrlen);
176 if (timeout_ms > 0) {
187 ssize_t nbytes = read(fd_, buf, count);
188 if (timeout_ms == 0) {
192 while ((nbytes == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) {
193 if (poll_handler_->Block(timeout_ms,
true)) {
194 nbytes = read(fd_, buf, count);
196 if (timeout_ms > 0) {
207 ssize_t nbytes = write(fd_, buf, count);
208 if (timeout_ms == 0) {
212 while ((nbytes == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) {
213 if (poll_handler_->Block(timeout_ms,
false)) {
214 nbytes = write(fd_, buf, count);
216 if (timeout_ms > 0) {
ssize_t RecvFrom(void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen, int timeout_ms=-1)
ssize_t Write(const void *buf, size_t count, int timeout_ms=-1)
std::shared_ptr< Session > SessionPtr
ssize_t SendTo(const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen, int timeout_ms=-1)
int Bind(const struct sockaddr *addr, socklen_t addrlen)
ssize_t Recv(void *buf, size_t len, int flags, int timeout_ms=-1)
ssize_t Read(void *buf, size_t count, int timeout_ms=-1)
SessionPtr Accept(struct sockaddr *addr, socklen_t *addrlen)
ssize_t Send(const void *buf, size_t len, int flags, int timeout_ms=-1)
int Connect(const struct sockaddr *addr, socklen_t addrlen)
int Socket(int domain, int type, int protocol)