Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
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
Operations
Operations
Metrics
Environments
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 Framework
Commits
e23a402f
Commit
e23a402f
authored
Aug 28, 2019
by
Dominik Charousset
Committed by
GitHub
Aug 28, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #16
Add socket guard
parents
617cd5ea
a6c7daab
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
141 additions
and
0 deletions
+141
-0
libcaf_net/caf/net/socket_guard.hpp
libcaf_net/caf/net/socket_guard.hpp
+55
-0
libcaf_net/test/socket_guard.cpp
libcaf_net/test/socket_guard.cpp
+86
-0
No files found.
libcaf_net/caf/net/socket_guard.hpp
0 → 100644
View file @
e23a402f
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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_id.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
()
{
if
(
sock_
.
id
!=
invalid_socket_id
)
close
(
sock_
);
}
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 @
e23a402f
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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
;
}
struct
fixture
{
fixture
()
:
id
{
dummy_id
},
closed
{
false
},
sock
{
id
,
closed
}
{
// nop
}
socket_id
id
;
bool
closed
;
dummy_socket
sock
;
};
}
// namespace
CAF_TEST_FIXTURE_SCOPE
(
socket_guard_tests
,
fixture
)
CAF_TEST
(
cleanup
)
{
{
auto
guard
=
make_socket_guard
(
sock
);
CAF_CHECK_EQUAL
(
sock
.
id
,
dummy_id
);
}
CAF_CHECK
(
sock
.
closed
);
}
CAF_TEST
(
release
)
{
{
auto
guard
=
make_socket_guard
(
sock
);
CAF_CHECK_EQUAL
(
sock
.
id
,
dummy_id
);
guard
.
release
();
CAF_CHECK_EQUAL
(
sock
.
id
,
invalid_socket_id
);
}
CAF_CHECK_EQUAL
(
sock
.
closed
,
false
);
}
CAF_TEST_FIXTURE_SCOPE_END
()
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