Skip to content

Add tcp_socket::bind() for local endpoint binding before connect#224

Merged
sgerbino merged 1 commit intocppalliance:developfrom
sgerbino:pr/bind
Apr 6, 2026
Merged

Add tcp_socket::bind() for local endpoint binding before connect#224
sgerbino merged 1 commit intocppalliance:developfrom
sgerbino:pr/bind

Conversation

@sgerbino
Copy link
Copy Markdown
Collaborator

@sgerbino sgerbino commented Apr 6, 2026

Expose bind() on tcp_socket so TCP clients can bind to a specific local address/port before connecting (multi-homed hosts, source-port pinning). Also mark udp_socket::bind() [[nodiscard]] for consistency.

Resolves #210.

Summary by CodeRabbit

  • New Features
    • TCP sockets now support explicit local endpoint binding before establishing connections, providing enhanced control over socket configuration and enabling applications to select specific network interfaces across all supported platforms.
    • UDP socket bind() return value is now flagged as mandatory to check, further improving error handling practices and overall API robustness.

Expose bind() on tcp_socket so TCP clients can bind to a specific
local address/port before connecting (multi-homed hosts, source-port
pinning). Also mark udp_socket::bind() [[nodiscard]] for consistency.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 6, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bf08e2dd-90f0-4bb8-b30b-2ff120326930

📥 Commits

Reviewing files that changed from the base of the PR and between 9c7d1eb and 200dc8f.

⛔ Files ignored due to path filters (3)
  • test/unit/tcp_acceptor.cpp is excluded by !**/test/**
  • test/unit/tcp_socket.cpp is excluded by !**/test/**
  • test/unit/udp_socket.cpp is excluded by !**/test/**
📒 Files selected for processing (9)
  • include/boost/corosio/detail/tcp_service.hpp
  • include/boost/corosio/native/detail/epoll/epoll_tcp_service.hpp
  • include/boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp
  • include/boost/corosio/native/detail/iocp/win_tcp_service.hpp
  • include/boost/corosio/native/detail/kqueue/kqueue_tcp_service.hpp
  • include/boost/corosio/native/detail/select/select_tcp_service.hpp
  • include/boost/corosio/tcp_socket.hpp
  • include/boost/corosio/udp_socket.hpp
  • src/corosio/src/tcp_socket.cpp

📝 Walkthrough

Walkthrough

The pull request adds a bind() method to tcp_socket by introducing a pure virtual bind_socket() method to the base tcp_service interface and implementing it across all platform-specific backends (epoll, kqueue, select, IOCP). The public API exposes tcp_socket::bind() for pre-connection local endpoint binding, and udp_socket::bind() is marked [[nodiscard]] for consistency. The Windows implementation conditionally applies ephemeral binding only when the socket has not already been explicitly bound.

Changes

Cohort / File(s) Summary
Service Interface
include/boost/corosio/detail/tcp_service.hpp
Added pure virtual bind_socket(tcp_socket::implementation& impl, endpoint ep) method to base TCP service interface.
Platform Implementations
include/boost/corosio/native/detail/epoll/epoll_tcp_service.hpp, include/boost/corosio/native/detail/kqueue/kqueue_tcp_service.hpp, include/boost/corosio/native/detail/select/select_tcp_service.hpp
Implemented bind_socket() override by casting to platform-specific socket type and forwarding to do_bind(ep).
Windows IOCP Service
include/boost/corosio/native/detail/iocp/win_tcp_service.hpp, include/boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp
Added bind_socket() method with inline implementation performing ::bind() via to_sockaddr() and caching resolved local endpoint. Updated win_tcp_socket_internal::connect to conditionally bind only when local_endpoint_ is unset.
TCP Socket Public API
include/boost/corosio/tcp_socket.hpp, src/corosio/src/tcp_socket.cpp
Exposed [[nodiscard]] std::error_code bind(endpoint ep) member; validates socket is open and dispatches to platform-specific service.
UDP Socket Consistency
include/boost/corosio/udp_socket.hpp
Added [[nodiscard]] attribute to existing bind(endpoint ep) method signature.

Sequence Diagram

sequenceDiagram
    actor User
    participant tcp_sock as tcp_socket
    participant svc_base as tcp_service<br/>(abstract)
    participant svc_impl as Platform Service<br/>(epoll/kqueue/select/win)
    participant sock_impl as Socket Impl
    participant OS as OS Layer

    User->>tcp_sock: bind(endpoint)
    tcp_sock->>tcp_sock: validate is_open()
    tcp_sock->>svc_impl: dispatch to platform service
    svc_impl->>sock_impl: cast to platform socket type
    sock_impl->>sock_impl: do_bind(endpoint)
    sock_impl->>OS: ::bind / WSABind
    OS-->>sock_impl: error_code
    sock_impl->>sock_impl: cache local_endpoint
    sock_impl-->>svc_impl: error_code
    svc_impl-->>tcp_sock: error_code
    tcp_sock-->>User: error_code
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • UDP #208: Refactored socket_service types into tcp_service and platform backends; this PR directly extends those same classes with new bind_socket virtual methods and implementations.
  • Add Javadoc documentation to all public headers #193: Modified tcp_socket public/backend interface in the same header file; shares infrastructure updates for socket implementation bindings.

Poem

🐰 At last, at last, the TCP can bind!
A socket's heart, now given time
To choose its port before it dares to climb
Through networks wide—oh, how divine!

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding tcp_socket::bind() for local endpoint binding before connect, which is the primary objective of this PR.
Linked Issues check ✅ Passed All requirements from issue #210 are met: bind_socket() added to tcp_service interface [#210], implemented in all backends (epoll, iocp, kqueue, select) [#210], and bind() exposed on tcp_socket [#210].
Out of Scope Changes check ✅ Passed All changes are directly related to #210 objectives. The udp_socket::bind() [[nodiscard]] attribute addition is a minor consistency enhancement explicitly mentioned in the PR description.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cppalliance-bot
Copy link
Copy Markdown

An automated preview of the documentation is available at https://224.corosio.prtest3.cppalliance.org/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-04-06 17:37:08 UTC

@cppalliance-bot
Copy link
Copy Markdown

GCOVR code coverage report https://224.corosio.prtest3.cppalliance.org/gcovr/index.html
LCOV code coverage report https://224.corosio.prtest3.cppalliance.org/genhtml/index.html
Coverage Diff Report https://224.corosio.prtest3.cppalliance.org/diff-report/index.html

Build time: 2026-04-06 17:46:31 UTC

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.87%. Comparing base (9c7d1eb) to head (200dc8f).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #224   +/-   ##
========================================
  Coverage    77.87%   77.87%           
========================================
  Files           96       96           
  Lines         7364     7364           
  Branches      1794     1794           
========================================
  Hits          5735     5735           
  Misses        1110     1110           
  Partials       519      519           
Files with missing lines Coverage Δ
include/boost/corosio/tcp_socket.hpp 88.67% <ø> (ø)
src/corosio/src/tcp_socket.cpp 82.69% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9c7d1eb...200dc8f. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sgerbino sgerbino merged commit 0cf9f49 into cppalliance:develop Apr 6, 2026
42 checks passed
@sgerbino sgerbino deleted the pr/bind branch April 6, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tcp_socket missing bind() method

2 participants