Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sgxwallet
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
董子豪
sgxwallet
Commits
d898fb07
Unverified
Commit
d898fb07
authored
Jun 10, 2021
by
kladko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SKALE-4284
parent
f08c08d5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
140 additions
and
97 deletions
+140
-97
Makefile.am
Makefile.am
+1
-1
ZMQServer.cpp
ZMQServer.cpp
+136
-88
ZMQServer.h
ZMQServer.h
+3
-8
No files found.
Makefile.am
View file @
d898fb07
...
@@ -70,7 +70,7 @@ bin_PROGRAMS = sgxwallet testw sgx_util
...
@@ -70,7 +70,7 @@ bin_PROGRAMS = sgxwallet testw sgx_util
## have to be explicitly listed
## have to be explicitly listed
COMMON_SRC
=
SGXException.cpp ExitHandler.cpp ZMQClient.cpp BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp
ServerWorker.cpp
InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp
\
COMMON_SRC
=
SGXException.cpp ExitHandler.cpp ZMQClient.cpp BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp
\
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp
\
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp
\
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp
\
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp
\
third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c
\
third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c
\
...
...
ZMQServer.cpp
View file @
d898fb07
...
@@ -30,6 +30,7 @@
...
@@ -30,6 +30,7 @@
#include "common.h"
#include "common.h"
#include "SGXException.h"
#include "SGXException.h"
#include "ZMQMessage.h"
#include "ZMQServer.h"
#include "ZMQServer.h"
#include "sgxwallet_common.h"
#include "sgxwallet_common.h"
...
@@ -42,14 +43,7 @@ ZMQServer::ZMQServer(bool _checkSignature, const string &_caCertFile)
...
@@ -42,14 +43,7 @@ ZMQServer::ZMQServer(bool _checkSignature, const string &_caCertFile)
caCertFile
(
_caCertFile
),
ctx_
(
make_shared
<
zmq
::
context_t
>
(
1
))
{
caCertFile
(
_caCertFile
),
ctx_
(
make_shared
<
zmq
::
context_t
>
(
1
))
{
frontend
=
make_shared
<
zmq
::
socket_t
>
(
*
ctx_
,
ZMQ_ROUTER
);
socket
=
make_shared
<
zmq
::
socket_t
>
(
*
ctx_
,
ZMQ_ROUTER
);
backend
=
make_shared
<
zmq
::
socket_t
>
(
*
ctx_
,
ZMQ_DEALER
);
//workerThreads = 2 * thread::hardware_concurrency();
workerThreads
=
1
;
// do one thread for now
if
(
_checkSignature
)
{
if
(
_checkSignature
)
{
CHECK_STATE
(
!
_caCertFile
.
empty
());
CHECK_STATE
(
!
_caCertFile
.
empty
());
...
@@ -61,9 +55,7 @@ ZMQServer::ZMQServer(bool _checkSignature, const string &_caCertFile)
...
@@ -61,9 +55,7 @@ ZMQServer::ZMQServer(bool _checkSignature, const string &_caCertFile)
int
linger
=
0
;
int
linger
=
0
;
zmq_setsockopt
(
*
socket
,
ZMQ_LINGER
,
&
linger
,
sizeof
(
linger
));
zmq_setsockopt
(
*
frontend
,
ZMQ_LINGER
,
&
linger
,
sizeof
(
linger
));
zmq_setsockopt
(
*
backend
,
ZMQ_LINGER
,
&
linger
,
sizeof
(
linger
));
}
}
...
@@ -75,8 +67,8 @@ void ZMQServer::run() {
...
@@ -75,8 +67,8 @@ void ZMQServer::run() {
spdlog
::
info
(
"Starting zmq server on port {} ..."
,
port
);
spdlog
::
info
(
"Starting zmq server on port {} ..."
,
port
);
try
{
try
{
CHECK_STATE
(
frontend
);
CHECK_STATE
(
socket
);
frontend
->
bind
(
"tcp://*:"
+
to_string
(
port
));
socket
->
bind
(
"tcp://*:"
+
to_string
(
port
));
}
catch
(...)
{
}
catch
(...)
{
spdlog
::
error
(
"Server task could not bind to port:{}"
,
port
);
spdlog
::
error
(
"Server task could not bind to port:{}"
,
port
);
throw
SGXException
(
ZMQ_COULD_NOT_BIND_FRONT_END
,
"Server task could not bind."
);
throw
SGXException
(
ZMQ_COULD_NOT_BIND_FRONT_END
,
"Server task could not bind."
);
...
@@ -84,68 +76,10 @@ void ZMQServer::run() {
...
@@ -84,68 +76,10 @@ void ZMQServer::run() {
spdlog
::
info
(
"Bound port ..."
);
spdlog
::
info
(
"Bound port ..."
);
try
{
CHECK_STATE
(
backend
);
backend
->
bind
(
"inproc://backend"
);
}
catch
(
exception
&
e
)
{
spdlog
::
error
(
"Could not bind to zmq backend: {}"
,
e
.
what
());
throw
SGXException
(
ZMQ_COULD_NOT_BIND_BACK_END
,
"Could not bind to zmq backend."
);
}
spdlog
::
info
(
"Creating {} zmq server workers ..."
,
workerThreads
);
try
{
for
(
int
i
=
0
;
i
<
workerThreads
;
++
i
)
{
workers
.
push_back
(
make_shared
<
ServerWorker
>
(
*
ctx_
,
ZMQ_DEALER
,
this
->
checkSignature
,
this
->
caCert
));
auto
th
=
make_shared
<
std
::
thread
>
(
std
::
bind
(
&
ServerWorker
::
work
,
workers
[
i
]));
worker_threads
.
push_back
(
th
);
}
}
catch
(
std
::
exception
&
e
)
{
spdlog
::
error
(
"Could not create zmq server workers:{} "
,
e
.
what
());
throw
SGXException
(
ZMQ_COULD_NOT_CREATE_WORKERS
,
"Could not create zmq server workers."
);
};
spdlog
::
info
(
"Created {} zmq server workers ..."
,
workerThreads
);
spdlog
::
info
(
"Creating zmq proxy."
);
try
{
zmq
::
proxy
(
static_cast
<
void
*>
(
*
frontend
),
static_cast
<
void
*>
(
*
backend
),
nullptr
);
spdlog
::
info
(
"Exited zmq proxy"
);
}
catch
(
exception
&
_e
)
{
if
(
isExitRequested
)
{
spdlog
::
info
(
"Exited ZMQServer main thread"
);
return
;
}
spdlog
::
info
(
"Error, exiting zmq server ... {}"
,
_e
.
what
());
return
;
}
catch
(...)
{
if
(
isExitRequested
)
{
spdlog
::
info
(
"Exited ZMQServer main thread"
);
return
;
}
spdlog
::
info
(
"Error, exiting zmq server ..."
);
throw
SGXException
(
ZMQ_COULD_NOT_CREATE_PROXY
,
"Error, exiting zmq server."
);
}
}
}
void
ZMQServer
::
exitAll
()
{
void
ZMQServer
::
exitAll
()
{
spdlog
::
info
(
"Exiting zmq server workers ..."
);
for
(
auto
&&
worker
:
workers
)
{
worker
->
requestExit
();
}
for
(
auto
&&
workerThread
:
worker_threads
)
{
workerThread
->
join
();
}
spdlog
::
info
(
"Exited zmq server workers ..."
);
}
}
std
::
atomic
<
bool
>
ZMQServer
::
isExitRequested
(
false
);
std
::
atomic
<
bool
>
ZMQServer
::
isExitRequested
(
false
);
...
@@ -154,6 +88,7 @@ void ZMQServer::exitZMQServer() {
...
@@ -154,6 +88,7 @@ void ZMQServer::exitZMQServer() {
auto
doExit
=
!
isExitRequested
.
exchange
(
true
);
auto
doExit
=
!
isExitRequested
.
exchange
(
true
);
if
(
doExit
)
{
if
(
doExit
)
{
zmqServer
->
exitAll
();
zmqServer
->
exitAll
();
spdlog
::
info
(
"deleting zmq server"
);
spdlog
::
info
(
"deleting zmq server"
);
...
@@ -179,35 +114,148 @@ void ZMQServer::initZMQServer(bool _checkSignature) {
...
@@ -179,35 +114,148 @@ void ZMQServer::initZMQServer(bool _checkSignature) {
};
};
zmqServer
=
make_shared
<
ZMQServer
>
(
_checkSignature
,
rootCAPath
);
zmqServer
=
make_shared
<
ZMQServer
>
(
_checkSignature
,
rootCAPath
);
serverThread
=
make_shared
<
thread
>
(
std
::
bind
(
&
ZMQServer
::
run
,
ZMQServer
::
zmqServer
));
serverThread
->
detach
();
spdlog
::
info
(
"Inited zmq server ..."
);
CHECK_STATE
(
zmqServer
);
while
(
!
isExitRequested
)
{
try
{
zmqServer
->
doOneServerLoop
();
}
catch
(...)
{
spdlog
::
error
(
"doOneServerLoop threw exception. This should never happen!"
);
}
}
}
}
shared_ptr
<
std
::
thread
>
ZMQServer
::
serverThread
=
nullptr
;
shared_ptr
<
std
::
thread
>
ZMQServer
::
serverThread
=
nullptr
;
ZMQServer
::~
ZMQServer
()
{
ZMQServer
::~
ZMQServer
()
{
spdlog
::
info
(
"Deleting worker threads"
);
worker_threads
.
clear
();
spdlog
::
info
(
"Deleted worker threads"
);
spdlog
::
info
(
"Deleting
workers ...
"
);
spdlog
::
info
(
"Deleting
front end
"
);
workers
.
clear
()
;
socket
=
nullptr
;
spdlog
::
info
(
"Deleted
workers ...
"
);
spdlog
::
info
(
"Deleted
front end
"
);
spdlog
::
info
(
"Deleting front end and back end"
);
frontend
=
nullptr
;
backend
=
nullptr
;
spdlog
::
info
(
"Deleted front end and back end"
);
spdlog
::
info
(
"Deleting server thread"
);
ZMQServer
::
serverThread
=
nullptr
;
spdlog
::
info
(
"Deleted server thread"
);
spdlog
::
info
(
"Deleting ZMQ context"
);
spdlog
::
info
(
"Deleting ZMQ context"
);
ctx_
=
nullptr
;
ctx_
=
nullptr
;
spdlog
::
info
(
"Deleted ZMQ context"
);
spdlog
::
info
(
"Deleted ZMQ context"
);
}
}
void
ZMQServer
::
doOneServerLoop
()
{
string
replyStr
;
Json
::
Value
result
;
result
[
"status"
]
=
ZMQ_SERVER_ERROR
;
result
[
"errorMessage"
]
=
""
;
zmq
::
message_t
identity
;
zmq
::
message_t
identit2
;
zmq
::
message_t
copied_id
;
try
{
zmq_pollitem_t
items
[
1
];
items
[
0
].
socket
=
*
socket
;
items
[
0
].
events
=
ZMQ_POLLIN
;
int
pollResult
=
0
;
do
{
pollResult
=
zmq_poll
(
items
,
1
,
1000
);
if
(
isExitRequested
)
{
return
;
}
}
while
(
pollResult
==
0
);
if
(
!
socket
->
recv
(
&
identity
))
{
// something terrible happened
spdlog
::
error
(
"Fatal error: socket->recv(&identity) returned false"
);
exit
(
-
11
);
}
if
(
!
identity
.
more
())
{
// something terrible happened
spdlog
::
error
(
"Fatal error: zmq_msg_more(identity) returned false"
);
exit
(
-
12
);
}
copied_id
.
copy
(
&
identity
);
zmq
::
message_t
reqMsg
;
if
(
!
socket
->
recv
(
&
reqMsg
,
0
))
{
// something terrible happened
spdlog
::
error
(
"Fatal error: socket.recv(&reqMsg, 0) returned false"
);
exit
(
-
13
);
}
string
stringToParse
((
char
*
)
reqMsg
.
data
(),
reqMsg
.
size
());
CHECK_STATE
(
stringToParse
.
front
()
=
'{'
)
CHECK_STATE
(
stringToParse
.
back
()
=
'}'
)
auto
parsedMsg
=
ZMQMessage
::
parse
(
stringToParse
.
c_str
(),
stringToParse
.
size
(),
true
,
checkSignature
);
CHECK_STATE2
(
parsedMsg
,
ZMQ_COULD_NOT_PARSE
);
result
=
parsedMsg
->
process
();
}
catch
(
SGXException
&
e
)
{
result
[
"status"
]
=
e
.
getStatus
();
result
[
"errorMessage"
]
=
e
.
what
();
spdlog
::
error
(
"Exception in zmq server {}"
,
e
.
what
());
}
catch
(
std
::
exception
&
e
)
{
if
(
isExitRequested
)
{
return
;
}
result
[
"errorMessage"
]
=
string
(
e
.
what
());
spdlog
::
error
(
"Exception in zmq server :{}"
,
e
.
what
());
}
catch
(...)
{
if
(
isExitRequested
)
{
return
;
}
spdlog
::
error
(
"Error in zmq server "
);
result
[
"errorMessage"
]
=
"Error in zmq server "
;
}
try
{
Json
::
FastWriter
fastWriter
;
fastWriter
.
omitEndingLineFeed
();
replyStr
=
fastWriter
.
write
(
result
);
CHECK_STATE
(
replyStr
.
size
()
>
2
);
CHECK_STATE
(
replyStr
.
front
()
==
'{'
);
CHECK_STATE
(
replyStr
.
back
()
==
'}'
);
socket
->
send
(
copied_id
,
ZMQ_SNDMORE
);
s_send
(
*
socket
,
replyStr
);
}
catch
(
std
::
exception
&
e
)
{
if
(
isExitRequested
)
{
return
;
}
spdlog
::
error
(
"Exception in zmq server worker send :{}"
,
e
.
what
());
}
catch
(...)
{
if
(
isExitRequested
)
{
return
;
}
spdlog
::
error
(
"Unklnown exception in zmq server worker send"
);
}
}
ZMQServer.h
View file @
d898fb07
...
@@ -35,9 +35,6 @@
...
@@ -35,9 +35,6 @@
#include <zmq.hpp>
#include <zmq.hpp>
#include "zhelpers.hpp"
#include "zhelpers.hpp"
#include "ServerWorker.h"
using
namespace
std
;
using
namespace
std
;
...
@@ -70,14 +67,12 @@ public:
...
@@ -70,14 +67,12 @@ public:
private
:
private
:
shared_ptr
<
zmq
::
context_t
>
ctx_
;
shared_ptr
<
zmq
::
context_t
>
ctx_
;
shared_ptr
<
zmq
::
socket_t
>
frontend
;
shared_ptr
<
zmq
::
socket_t
>
socket
;
shared_ptr
<
zmq
::
socket_t
>
backend
;
std
::
vector
<
shared_ptr
<
ServerWorker
>
>
workers
;
std
::
vector
<
shared_ptr
<
std
::
thread
>>
worker_threads
;
static
std
::
atomic
<
bool
>
isExitRequested
;
static
std
::
atomic
<
bool
>
isExitRequested
;
void
doOneServerLoop
();
};
};
...
...
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