Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
actor-incubator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
actor-incubator
Commits
d3ad8242
Commit
d3ad8242
authored
Aug 28, 2019
by
Joseph Noir
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add socket guard
parent
7f2798df
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
139 additions
and
0 deletions
+139
-0
libcaf_net/caf/net/socket_guard.hpp
libcaf_net/caf/net/socket_guard.hpp
+58
-0
libcaf_net/test/socket_guard.cpp
libcaf_net/test/socket_guard.cpp
+81
-0
No files found.
libcaf_net/caf/net/socket_guard.hpp
0 → 100644
View file @
d3ad8242
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include "caf/net/socket.hpp"
namespace
caf
{
namespace
net
{
/// Closes the guarded socket when destroyed.
template
<
class
Socket
>
class
socket_guard
{
public:
explicit
socket_guard
(
Socket
sock
)
:
sock_
(
sock
)
{
// nop
}
~
socket_guard
()
{
using
net
::
close
;
if
(
sock_
.
id
!=
invalid_socket_id
)
{
close
(
sock_
);
sock_
.
id
=
invalid_socket_id
;
}
}
Socket
release
()
{
auto
sock
=
sock_
;
sock_
.
id
=
invalid_socket_id
;
return
sock
;
}
private:
Socket
sock_
;
};
template
<
class
Socket
>
socket_guard
<
Socket
>
make_socket_guard
(
Socket
sock
)
{
return
socket_guard
<
Socket
>
{
sock
};
}
}
// namespace net
}
// namespace caf
libcaf_net/test/socket_guard.cpp
0 → 100644
View file @
d3ad8242
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE socket_guard
#include "caf/net/socket_guard.hpp"
#include "caf/test/dsl.hpp"
#include "caf/net/abstract_socket.hpp"
#include "caf/net/socket_id.hpp"
using
namespace
caf
;
using
namespace
caf
::
net
;
namespace
{
constexpr
socket_id
dummy_id
=
13
;
struct
dummy_socket
{
dummy_socket
(
socket_id
&
id
,
bool
&
closed
)
:
id
(
id
),
closed
(
closed
)
{
// nop
}
dummy_socket
&
operator
=
(
const
dummy_socket
&
other
)
{
id
=
other
.
id
;
closed
=
other
.
closed
;
return
*
this
;
}
socket_id
&
id
;
bool
&
closed
;
};
void
close
(
dummy_socket
x
)
{
x
.
closed
=
true
;
}
}
// namespace
CAF_TEST
(
cleanup
)
{
auto
id
=
dummy_id
;
auto
closed
=
false
;
dummy_socket
sock
{
id
,
closed
};
{
auto
guard
=
make_socket_guard
(
sock
);
CAF_CHECK_EQUAL
(
sock
.
id
,
dummy_id
);
}
CAF_CHECK_EQUAL
(
sock
.
id
,
invalid_socket_id
);
CAF_CHECK
(
sock
.
closed
);
}
CAF_TEST
(
release
)
{
auto
id
=
dummy_id
;
auto
closed
=
false
;
dummy_socket
sock
{
id
,
closed
};
{
auto
guard
=
make_socket_guard
(
sock
);
CAF_CHECK_EQUAL
(
sock
.
id
,
dummy_id
);
auto
released
=
guard
.
release
();
CAF_CHECK_EQUAL
(
sock
.
id
,
invalid_socket_id
);
sock
=
released
;
}
// Cannot check socket.id because it is a reference and will be invalidated.
CAF_CHECK_EQUAL
(
sock
.
closed
,
false
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment