jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
jsocketpp::internal::ScopedBlockingMode Class Reference

RAII helper for temporarily overriding a socket's blocking mode. More...

#include <ScopedBlockingMode.hpp>

Public Member Functions

 ScopedBlockingMode (const SOCKET sock, bool temporaryNonBlocking)
 Construct a ScopedBlockingMode that temporarily overrides the socket's blocking mode.
 ~ScopedBlockingMode ()
 Restore the socket's original blocking mode on destruction.

Private Attributes

SOCKET _sock
 Socket descriptor being managed for temporary mode override.
bool _wasBlocking {}
 Whether the socket was originally in blocking mode.

Detailed Description

RAII helper for temporarily overriding a socket's blocking mode.

This class manages a temporary change to a socket's blocking/non-blocking state. On construction, it queries the socket's current mode and applies the requested override. When the object is destroyed (typically at the end of a scope), the original blocking mode is restored automatically, ensuring consistent socket state even in the presence of exceptions.

Use Case

Primarily used in socket operations like connect() that require temporarily switching to non-blocking mode to perform select()-based timeout handling without permanently altering the socket's mode.

Platform Behavior

  • POSIX: Uses fcntl() with O_NONBLOCK
  • Windows: Uses ioctlsocket() with FIONBIO

Safety

  • This class is exception-safe: if an error occurs during the override, the constructor throws.
  • If the socket is externally modified via setNonBlocking() during the lifetime of a ScopedBlockingMode, the restored state may be incorrect. Do not call setNonBlocking() on the same socket while a ScopedBlockingMode is active.

Example

{
jsocketpp::internal::ScopedBlockingMode guard(sockFd, true); // temporarily non-blocking
// Perform connect() or poll()
} // original mode restored
RAII helper for temporarily overriding a socket's blocking mode.
Definition ScopedBlockingMode.hpp:77
See also
setNonBlocking()
Socket::connect()

Member Data Documentation

◆ _sock

SOCKET jsocketpp::internal::ScopedBlockingMode::_sock
private

Socket descriptor being managed for temporary mode override.

◆ _wasBlocking

bool jsocketpp::internal::ScopedBlockingMode::_wasBlocking {}
private

Whether the socket was originally in blocking mode.


The documentation for this class was generated from the following file: