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
ecde0240
Unverified
Commit
ecde0240
authored
Sep 09, 2021
by
kladko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SKALE-4586 Added Queue
parent
832ff75a
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
1778 additions
and
26 deletions
+1778
-26
atomicops.h
third_party/atomicops.h
+761
-0
readerwriterqueue.h
third_party/readerwriterqueue.h
+979
-0
ZMQClient.h
zmq_src/ZMQClient.h
+0
-1
ZMQServer.cpp
zmq_src/ZMQServer.cpp
+23
-21
ZMQServer.h
zmq_src/ZMQServer.h
+15
-4
No files found.
third_party/atomicops.h
0 → 100644
View file @
ecde0240
This diff is collapsed.
Click to expand it.
third_party/readerwriterqueue.h
0 → 100644
View file @
ecde0240
This diff is collapsed.
Click to expand it.
zmq_src/ZMQClient.h
View file @
ecde0240
...
@@ -31,7 +31,6 @@
...
@@ -31,7 +31,6 @@
#include <openssl/rand.h>
#include <openssl/rand.h>
#include "third_party/spdlog/spdlog.h"
#include "third_party/spdlog/spdlog.h"
#include <zmq.hpp>
#include <zmq.hpp>
#include "zhelpers.hpp"
#include "zhelpers.hpp"
#include <jsonrpccpp/client.h>
#include <jsonrpccpp/client.h>
...
...
zmq_src/ZMQServer.cpp
View file @
ecde0240
...
@@ -42,7 +42,7 @@ using namespace std;
...
@@ -42,7 +42,7 @@ using namespace std;
shared_ptr
<
ZMQServer
>
ZMQServer
::
zmqServer
=
nullptr
;
shared_ptr
<
ZMQServer
>
ZMQServer
::
zmqServer
=
nullptr
;
ZMQServer
::
ZMQServer
(
bool
_checkSignature
,
bool
_checkKeyOwnership
,
const
string
&
_caCertFile
)
ZMQServer
::
ZMQServer
(
bool
_checkSignature
,
bool
_checkKeyOwnership
,
const
string
&
_caCertFile
)
:
checkSignature
(
_checkSignature
),
checkKeyOwnership
(
_checkKeyOwnership
),
:
outgoingQueue
(
NUM_ZMQ_WORKER_THREADS
),
checkSignature
(
_checkSignature
),
checkKeyOwnership
(
_checkKeyOwnership
),
caCertFile
(
_caCertFile
),
ctx
(
make_shared
<
zmq
::
context_t
>
(
1
))
{
caCertFile
(
_caCertFile
),
ctx
(
make_shared
<
zmq
::
context_t
>
(
1
))
{
socket
=
make_shared
<
zmq
::
socket_t
>
(
*
ctx
,
ZMQ_ROUTER
);
socket
=
make_shared
<
zmq
::
socket_t
>
(
*
ctx
,
ZMQ_ROUTER
);
...
@@ -103,7 +103,7 @@ void ZMQServer::run() {
...
@@ -103,7 +103,7 @@ void ZMQServer::run() {
spdlog
::
info
(
"Exited zmq server loop"
);
spdlog
::
info
(
"Exited zmq server loop"
);
}
}
std
::
atomic
<
bool
>
ZMQServer
::
isExitRequested
(
false
);
atomic
<
bool
>
ZMQServer
::
isExitRequested
(
false
);
void
ZMQServer
::
exitZMQServer
()
{
void
ZMQServer
::
exitZMQServer
()
{
// if already exited do not exit
// if already exited do not exit
...
@@ -151,7 +151,7 @@ void ZMQServer::initZMQServer(bool _checkSignature, bool _checkKeyOwnership) {
...
@@ -151,7 +151,7 @@ void ZMQServer::initZMQServer(bool _checkSignature, bool _checkKeyOwnership) {
zmqServer
=
make_shared
<
ZMQServer
>
(
_checkSignature
,
_checkKeyOwnership
,
rootCAPath
);
zmqServer
=
make_shared
<
ZMQServer
>
(
_checkSignature
,
_checkKeyOwnership
,
rootCAPath
);
CHECK_STATE
(
zmqServer
)
CHECK_STATE
(
zmqServer
)
serverThread
=
make_shared
<
thread
>
(
std
::
bind
(
&
ZMQServer
::
run
,
ZMQServer
::
zmqServer
));
serverThread
=
make_shared
<
thread
>
(
bind
(
&
ZMQServer
::
run
,
ZMQServer
::
zmqServer
));
serverThread
->
detach
();
serverThread
->
detach
();
spdlog
::
info
(
"Releasing SGX worker threads ..."
);
spdlog
::
info
(
"Releasing SGX worker threads ..."
);
...
@@ -163,7 +163,7 @@ void ZMQServer::initZMQServer(bool _checkSignature, bool _checkKeyOwnership) {
...
@@ -163,7 +163,7 @@ void ZMQServer::initZMQServer(bool _checkSignature, bool _checkKeyOwnership) {
spdlog
::
info
(
"Inited zmq server."
);
spdlog
::
info
(
"Inited zmq server."
);
}
}
shared_ptr
<
std
::
thread
>
ZMQServer
::
serverThread
=
nullptr
;
shared_ptr
<
thread
>
ZMQServer
::
serverThread
=
nullptr
;
ZMQServer
::~
ZMQServer
()
{
ZMQServer
::~
ZMQServer
()
{
exitZMQServer
();
exitZMQServer
();
...
@@ -192,38 +192,41 @@ PollResult ZMQServer::poll() {
...
@@ -192,38 +192,41 @@ PollResult ZMQServer::poll() {
return
GOT_INCOMING_MSG
;
return
GOT_INCOMING_MSG
;
}
}
string
ZMQServer
::
receiveMessage
(
zmq
::
message_t
&
_identity
)
{
pair
<
string
,
shared_ptr
<
zmq
::
message_t
>>
ZMQServer
::
receiveMessage
()
{
if
(
!
socket
->
recv
(
&
_identity
))
{
auto
identity
=
make_shared
<
zmq
::
message_t
>
();
if
(
!
socket
->
recv
(
identity
.
get
()))
{
checkForExit
();
checkForExit
();
// something terrible happened
// something terrible happened
spdlog
::
error
(
"Fatal error: socket->recv(&identity) returned false. Exiting."
);
spdlog
::
error
(
"Fatal error: socket->recv(&identity) returned false. Exiting."
);
exit
(
-
11
);
exit
(
-
11
);
}
}
if
(
!
_identity
.
more
())
{
if
(
!
identity
->
more
())
{
checkForExit
();
checkForExit
();
// something terrible happened
// something terrible happened
spdlog
::
error
(
"Fatal error: zmq_msg_more(identity) returned false. Existing."
);
spdlog
::
error
(
"Fatal error: zmq_msg_more(identity) returned false. Existing."
);
exit
(
-
12
);
exit
(
-
12
);
}
}
zmq
::
message_t
reqMsg
;
auto
reqMsg
=
make_shared
<
zmq
::
message_t
>
()
;
if
(
!
socket
->
recv
(
&
reqMsg
,
0
))
{
if
(
!
socket
->
recv
(
reqMsg
.
get
()
,
0
))
{
checkForExit
();
checkForExit
();
// something terrible happened
// something terrible happened
spdlog
::
error
(
"Fatal error: socket.recv(&reqMsg, 0) returned false. Exiting"
);
spdlog
::
error
(
"Fatal error: socket.recv(&reqMsg, 0) returned false. Exiting"
);
exit
(
-
13
);
exit
(
-
13
);
}
}
auto
result
=
string
((
char
*
)
reqMsg
.
data
(),
reqMsg
.
size
());
auto
result
=
string
((
char
*
)
reqMsg
->
data
(),
reqMsg
->
size
());
CHECK_STATE
(
result
.
front
()
==
'{'
)
CHECK_STATE
(
result
.
front
()
==
'{'
)
CHECK_STATE
(
result
.
back
()
==
'}'
)
CHECK_STATE
(
result
.
back
()
==
'}'
)
return
result
;
return
{
result
,
identity
}
;
}
}
void
ZMQServer
::
sendToClient
(
Json
::
Value
&
_result
,
zmq
::
message_t
&
_identity
)
{
void
ZMQServer
::
sendToClient
(
Json
::
Value
&
_result
,
shared_ptr
<
zmq
::
message_t
>
&
_identity
)
{
string
replyStr
;
string
replyStr
;
try
{
try
{
Json
::
FastWriter
fastWriter
;
Json
::
FastWriter
fastWriter
;
...
@@ -235,7 +238,7 @@ void ZMQServer::sendToClient(Json::Value& _result, zmq::message_t& _identity )
...
@@ -235,7 +238,7 @@ void ZMQServer::sendToClient(Json::Value& _result, zmq::message_t& _identity )
CHECK_STATE
(
replyStr
.
front
()
==
'{'
);
CHECK_STATE
(
replyStr
.
front
()
==
'{'
);
CHECK_STATE
(
replyStr
.
back
()
==
'}'
);
CHECK_STATE
(
replyStr
.
back
()
==
'}'
);
if
(
!
socket
->
send
(
_identity
,
ZMQ_SNDMORE
))
{
if
(
!
socket
->
send
(
*
_identity
,
ZMQ_SNDMORE
))
{
exit
(
-
15
);
exit
(
-
15
);
}
}
if
(
!
s_send
(
*
socket
,
replyStr
))
{
if
(
!
s_send
(
*
socket
,
replyStr
))
{
...
@@ -243,7 +246,7 @@ void ZMQServer::sendToClient(Json::Value& _result, zmq::message_t& _identity )
...
@@ -243,7 +246,7 @@ void ZMQServer::sendToClient(Json::Value& _result, zmq::message_t& _identity )
}
}
}
catch
(
ExitRequestedException
)
{
}
catch
(
ExitRequestedException
)
{
throw
;
throw
;
}
catch
(
std
::
exception
&
e
)
{
}
catch
(
exception
&
e
)
{
checkForExit
();
checkForExit
();
spdlog
::
error
(
"Exception in zmq server worker send :{}"
,
e
.
what
());
spdlog
::
error
(
"Exception in zmq server worker send :{}"
,
e
.
what
());
exit
(
-
17
);
exit
(
-
17
);
...
@@ -260,14 +263,14 @@ void ZMQServer::doOneServerLoop() {
...
@@ -260,14 +263,14 @@ void ZMQServer::doOneServerLoop() {
Json
::
Value
result
;
Json
::
Value
result
;
result
[
"status"
]
=
ZMQ_SERVER_ERROR
;
result
[
"status"
]
=
ZMQ_SERVER_ERROR
;
zmq
::
message_t
identity
;
shared_ptr
<
zmq
::
message_t
>
identity
=
nullptr
;
string
msgStr
;
string
msgStr
;
try
{
try
{
poll
();
poll
();
msgStr
=
receiveMessage
(
identity
);
tie
(
msgStr
,
identity
)
=
receiveMessage
(
);
auto
msg
=
ZMQMessage
::
parse
(
auto
msg
=
ZMQMessage
::
parse
(
msgStr
.
c_str
(),
msgStr
.
size
(),
true
,
checkSignature
,
checkKeyOwnership
);
msgStr
.
c_str
(),
msgStr
.
size
(),
true
,
checkSignature
,
checkKeyOwnership
);
...
@@ -276,25 +279,24 @@ void ZMQServer::doOneServerLoop() {
...
@@ -276,25 +279,24 @@ void ZMQServer::doOneServerLoop() {
if
((
dynamic_pointer_cast
<
BLSSignReqMessage
>
(
msg
)
!=
nullptr
)
||
if
((
dynamic_pointer_cast
<
BLSSignReqMessage
>
(
msg
)
!=
nullptr
)
||
dynamic_pointer_cast
<
ECDSASignReqMessage
>
(
msg
))
{
dynamic_pointer_cast
<
ECDSASignReqMessage
>
(
msg
))
{
spdlog
::
info
(
"FUFUFUFUF"
);
}
else
{
}
else
{
spdlog
::
info
(
"HAHAHA"
);
}
}
result
=
msg
->
process
();
result
=
msg
->
process
();
}
catch
(
ExitRequestedException
)
{
}
catch
(
ExitRequestedException
)
{
throw
;
throw
;
}
catch
(
std
::
exception
&
e
)
{
}
catch
(
exception
&
e
)
{
checkForExit
();
checkForExit
();
result
[
"errorMessage"
]
=
string
(
e
.
what
());
result
[
"errorMessage"
]
=
string
(
e
.
what
());
spdlog
::
error
(
"Exception in zmq server :{}"
,
e
.
what
());
spdlog
::
error
(
"Exception in zmq server :{}"
,
e
.
what
());
spdlog
::
error
(
"ID:"
+
string
((
char
*
)
identity
.
data
(),
identity
.
size
()));
spdlog
::
error
(
"ID:"
+
string
((
char
*
)
identity
->
data
(),
identity
->
size
()));
spdlog
::
error
(
"Client request :"
+
msgStr
);
spdlog
::
error
(
"Client request :"
+
msgStr
);
}
catch
(...)
{
}
catch
(...)
{
checkForExit
();
checkForExit
();
spdlog
::
error
(
"Error in zmq server "
);
spdlog
::
error
(
"Error in zmq server "
);
result
[
"errorMessage"
]
=
"Error in zmq server "
;
result
[
"errorMessage"
]
=
"Error in zmq server "
;
spdlog
::
error
(
"ID:"
+
string
((
char
*
)
identity
.
data
(),
identity
.
size
()));
spdlog
::
error
(
"ID:"
+
string
((
char
*
)
identity
->
data
(),
identity
->
size
()));
spdlog
::
error
(
"Client request :"
+
msgStr
);
spdlog
::
error
(
"Client request :"
+
msgStr
);
}
}
...
...
zmq_src/ZMQServer.h
View file @
ecde0240
...
@@ -25,14 +25,22 @@
...
@@ -25,14 +25,22 @@
#ifndef SGXWALLET_ZMQServer_H
#ifndef SGXWALLET_ZMQServer_H
#define SGXWALLET_ZMQServer_H
#define SGXWALLET_ZMQServer_H
#include "third_party/readerwriterqueue.h"
#include <zmq.hpp>
#include <zmq.hpp>
#include "zhelpers.hpp"
#include "zhelpers.hpp"
#include "Agent.h"
#include "Agent.h"
#include "WorkerThreadPool.h"
#include "WorkerThreadPool.h"
using
namespace
moodycamel
;
typedef
enum
{
GOT_INCOMING_MSG
=
0
,
GOT_OUTFOING_MSG
=
1
}
PollResult
;
typedef
enum
{
GOT_INCOMING_MSG
=
0
,
GOT_OUTFOING_MSG
=
1
}
PollResult
;
static
const
uint64_t
NUM_ZMQ_WORKER_THREADS
=
2
;
class
ZMQServer
:
public
Agent
{
class
ZMQServer
:
public
Agent
{
uint64_t
workerThreads
;
uint64_t
workerThreads
;
...
@@ -40,12 +48,16 @@ class ZMQServer : public Agent{
...
@@ -40,12 +48,16 @@ class ZMQServer : public Agent{
string
caCertFile
;
string
caCertFile
;
string
caCert
;
string
caCert
;
ReaderWriterQueue
<
pair
<
string
,
shared_ptr
<
zmq_msg_t
>>>
outgoingQueue
;
vector
<
ReaderWriterQueue
<
pair
<
string
,
shared_ptr
<
zmq_msg_t
>>>>
incomingQueue
;
bool
checkKeyOwnership
=
true
;
bool
checkKeyOwnership
=
true
;
shared_ptr
<
zmq
::
context_t
>
ctx
;
shared_ptr
<
zmq
::
context_t
>
ctx
;
shared_ptr
<
zmq
::
socket_t
>
socket
;
shared_ptr
<
zmq
::
socket_t
>
socket
;
static
std
::
atomic
<
bool
>
isExitRequested
;
static
atomic
<
bool
>
isExitRequested
;
void
doOneServerLoop
();
void
doOneServerLoop
();
...
@@ -57,7 +69,6 @@ public:
...
@@ -57,7 +69,6 @@ public:
shared_ptr
<
WorkerThreadPool
>
threadPool
=
nullptr
;
shared_ptr
<
WorkerThreadPool
>
threadPool
=
nullptr
;
static
shared_ptr
<
std
::
thread
>
serverThread
;
static
shared_ptr
<
std
::
thread
>
serverThread
;
ZMQServer
(
bool
_checkSignature
,
bool
_checkKeyOwnership
,
const
string
&
_caCertFile
);
ZMQServer
(
bool
_checkSignature
,
bool
_checkKeyOwnership
,
const
string
&
_caCertFile
);
...
@@ -79,9 +90,9 @@ public:
...
@@ -79,9 +90,9 @@ public:
PollResult
poll
();
PollResult
poll
();
string
receiveMessage
(
zmq
::
message_t
&
_identity
);
pair
<
string
,
shared_ptr
<
zmq
::
message_t
>>
receiveMessage
(
);
void
sendToClient
(
Json
::
Value
&
_result
,
zmq
::
message_t
&
_identity
);
void
sendToClient
(
Json
::
Value
&
_result
,
shared_ptr
<
zmq
::
message_t
>
&
_identity
);
};
};
...
...
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