Skip to content

Commit

Permalink
Merge pull request condy0919#35 from condy0919/dev
Browse files Browse the repository at this point in the history
Net module enhancement
  • Loading branch information
condy0919 authored Jan 27, 2020
2 parents d61760f + afe8871 commit 8cca438
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 17 deletions.
44 changes: 44 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: 2.1

commands:
install-bazel:
steps:
- run:
name: Installing bazel
command: |
bazel_version=2.0.0
curl -OL https://github.com/bazelbuild/bazel/releases/download/${bazel_version}/bazel-${bazel_version}-installer-linux-x86_64.sh
chmod +x bazel-${bazel_version}-installer-linux-x86_64.sh
./bazel-${bazel_version}-installer-linux-x86_64.sh
rm ./bazel-${bazel_version}-installer-linux-x86_64.sh
bazel --version
sysinfo:
steps:
- run:
name: System information
command: |
uname -a
free -m
gcc --version
jobs:
build:
docker:
- image: gcc:9
steps:
- sysinfo
- install-bazel
- checkout
- run:
name: Building
command: bazel build //... --jobs=4 # --jobs=auto will be OOM killed
- run:
name: Testing
command: bazel test //... --test_tag_filters='-io_uring,-benchmark,-example' --test_output=errors

workflows:
version: 2
default_workflow:
jobs:
- build
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
- name: Building
run: CC=clang bazel build //...

- name: Testing
run: CC=clang bazel test //... --test_tag_filters='-io_uring,-benchmark,-example' --test_output=errors

build:
name: ubuntu-gcc-9
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Build Status][circle-badge]][circle-link]
[![Build Status][github-ci-badge]][github-link]
[![Codacy Badge][codacy-badge]][codacy-link]
[![Codecov Badge][codecov-badge]][codecov-link]
Expand Down Expand Up @@ -74,6 +75,8 @@ Interested in getting involved? We would love to help you! For simple
bug fixes, just submit a PR with the fix and we can discuss the fix
directly in the PR. If the fix is more complex, start with an issue.

[circle-badge]: https://circleci.com/gh/condy0919/bipolar.svg?style=shield
[circle-link]: https://circleci.com/gh/condy0919/bipolar
[github-ci-badge]: https://github.com/condy0919/bipolar/workflows/BIPOLAR%20CI/badge.svg
[github-link]: https://github.com/condy0919/bipolar
[codacy-badge]: https://api.codacy.com/project/badge/Grade/7c5e88ade2944d7ca1741d2b3e709f4f
Expand Down
2 changes: 1 addition & 1 deletion bipolar/net/epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Result<Epoll, int> Epoll::create() noexcept {
Result<Void, int> Epoll::poll(std::vector<struct epoll_event>& events,
std::chrono::milliseconds timeout) noexcept {
const int ret =
::epoll_wait(epfd_, events.data(), events.size(), timeout.count());
::epoll_wait(epfd_, events.data(), events.capacity(), timeout.count());
if (ret == -1) {
return Err(errno);
}
Expand Down
42 changes: 42 additions & 0 deletions bipolar/net/tcp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "bipolar/net/tcp.hpp"

#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
Expand Down Expand Up @@ -132,6 +133,15 @@ Result<Void, int> TcpStream::shutdown(int how) noexcept {
return Ok(Void{});
}

Result<Void, int> TcpStream::set_nonblocking(bool enable) noexcept {
int opt = static_cast<int>(enable);
const int ret = ::ioctl(fd_, FIONBIO, &opt);
if (ret == -1) {
return Err(errno);
}
return Ok(Void{});
}

Result<Void, int> TcpStream::set_nodelay(bool enable) noexcept {
const int optval = static_cast<int>(enable);
const int ret =
Expand All @@ -152,6 +162,38 @@ Result<bool, int> TcpStream::nodelay() noexcept {
return Ok(static_cast<bool>(optval));
}

Result<Void, int>
TcpStream::set_linger(Option<std::chrono::seconds> s) noexcept {
struct linger opt = {
.l_onoff = s.has_value(),
.l_linger = 0,
};

if (s.has_value()) {
opt.l_linger = s.value().count();
}

const int ret = ::setsockopt(fd_, SOL_SOCKET, SO_LINGER, &opt, sizeof(opt));
if (ret == -1) {
return Err(errno);
}
return Ok(Void{});
}

Result<Option<std::chrono::seconds>, int> TcpStream::linger() noexcept {
struct linger opt;
socklen_t len = sizeof(opt);
const int ret = ::getsockopt(fd_, SOL_SOCKET, SO_LINGER, &opt, &len);
if (ret == -1) {
return Err(errno);
}
if (opt.l_onoff) {
return Ok(Some(std::chrono::seconds(opt.l_linger)));
} else {
return Ok(None);
}
}

Result<int, int> TcpStream::take_error() noexcept {
int optval = 0;
socklen_t len = sizeof(optval);
Expand Down
24 changes: 24 additions & 0 deletions bipolar/net/tcp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

#include <netinet/tcp.h>

#include <chrono>
#include <tuple>
#include <utility>

#include "bipolar/core/movable.hpp"
#include "bipolar/core/option.hpp"
#include "bipolar/core/result.hpp"
#include "bipolar/core/void.hpp"
#include "bipolar/net/socket_address.hpp"
Expand Down Expand Up @@ -40,6 +42,9 @@ namespace bipolar {
/// stream.write(buf, 4);
/// stream.read(buf, 4);
/// ```
///
/// `man 7 tcp` for more information.
///
class TcpStream final : public Movable {
public:
/// Constructs a TCP stream from native handle (file descriptor).
Expand Down Expand Up @@ -166,6 +171,15 @@ class TcpStream final : public Movable {
/// `man 2 shutdown` for more information.
Result<Void, int> shutdown(int how) noexcept;

/// Moves this TCP stream into or out of nonblocking mode.
///
/// This will result in `read`, `write`, `recv` and `send` operations
/// becoming nonblocking, i.e., immediately returning from their calls.
/// If the IO operation is successful, `Ok` is returned and no further
/// action is required. If the IO operation could not be completed and
/// needs to be retried, `EAGAIN` is returned.
Result<Void, int> set_nonblocking(bool enable) noexcept;

/// Sets the value of the `TCP_NODELAY` option on this socket.
///
/// If set, this option disables the Nagle algorithm. This means that
Expand All @@ -178,6 +192,16 @@ class TcpStream final : public Movable {
/// Gets the value of the `TCP_NODELAY` option on this socket
Result<bool, int> nodelay() noexcept;

/// Sets the linger duration of this socket by setting the `SO_LINGER`
/// option.
///
/// It used to **RESET** connections.
Result<Void, int> set_linger(Option<std::chrono::seconds> s) noexcept;

/// Reads the linger duration for this socket by getting the `SO_LINGER`
/// option.
Result<Option<std::chrono::seconds>, int> linger() noexcept;

/// Gets the value of the `SO_ERROR` option on this socket.
///
/// This will retrive the stored error in the underlying socket, clearing
Expand Down
Loading

0 comments on commit 8cca438

Please sign in to comment.