-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net/SocketPair: convenience wrapper for UniqueSocketDescriptor::Creat…
…eSocketPair()
- Loading branch information
1 parent
e95d8e7
commit 763e7d3
Showing
6 changed files
with
112 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
// author: Max Kellermann <[email protected]> | ||
|
||
#include "SocketPair.hxx" | ||
#include "SocketError.hxx" | ||
|
||
static inline std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
_CreateSocketPair(int domain, int type, int protocol) | ||
{ | ||
int sv[2]; | ||
if (socketpair(domain, type, protocol, sv)) | ||
throw MakeSocketError("socketpair() failed"); | ||
|
||
return { | ||
UniqueSocketDescriptor{sv[0]}, | ||
UniqueSocketDescriptor{sv[1]}, | ||
}; | ||
} | ||
|
||
std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
CreateSocketPair(int domain, int type, int protocol) | ||
{ | ||
#ifdef SOCK_CLOEXEC | ||
/* implemented since Linux 2.6.27 */ | ||
type |= SOCK_CLOEXEC; | ||
#endif | ||
|
||
return _CreateSocketPair(domain, type, protocol); | ||
} | ||
|
||
std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
CreateSocketPairNonBlock(int domain, int type, int protocol) | ||
{ | ||
#ifdef SOCK_NONBLOCK | ||
type |= SOCK_NONBLOCK; | ||
#endif | ||
|
||
return CreateSocketPair(domain, type, protocol); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
// author: Max Kellermann <[email protected]> | ||
|
||
#pragma once | ||
|
||
#include "UniqueSocketDescriptor.hxx" | ||
|
||
#include <utility> | ||
|
||
#include <sys/socket.h> | ||
|
||
class UniqueSocketDescriptor; | ||
|
||
/** | ||
* Wrapper for socketpair(). | ||
* | ||
* Throws on error. | ||
*/ | ||
std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
CreateSocketPair(int domain, int type, int protocol=0); | ||
|
||
/** | ||
* Shortcut for CreateSocketPair(AF_LOCAL, type). | ||
*/ | ||
static std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
CreateSocketPair(int type) | ||
{ | ||
return CreateSocketPair(AF_LOCAL, type); | ||
} | ||
|
||
/** | ||
* Like CreateSocketPair(), but with SOCK_NONBLOCK (if available). | ||
*/ | ||
std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
CreateSocketPairNonBlock(int domain, int type, int protocol=0); | ||
|
||
/** | ||
* Shortcut for CreateSocketPairNonBlock(AF_LOCAL, type). | ||
*/ | ||
static std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
CreateSocketPairNonBlock(int type) | ||
{ | ||
return CreateSocketPairNonBlock(AF_LOCAL, type); | ||
} | ||
|
||
/** | ||
* Shortcut for CreateSocketPair(SOCK_STREAM). | ||
*/ | ||
static inline std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
CreateStreamSocketPair() | ||
{ | ||
return CreateSocketPair(SOCK_STREAM); | ||
} | ||
|
||
/** | ||
* Shortcut for CreateSocketPairNonBlock(SOCK_STREAM). | ||
*/ | ||
static inline std::pair<UniqueSocketDescriptor, UniqueSocketDescriptor> | ||
CreateStreamSocketPairNonBlock() | ||
{ | ||
return CreateSocketPairNonBlock(SOCK_STREAM); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters