Don't need queue here
This commit is contained in:
parent
c8e81c2c81
commit
444e10acaf
2 changed files with 9 additions and 18 deletions
|
@ -5,14 +5,6 @@
|
||||||
|
|
||||||
static const int RpcVersion = 1;
|
static const int RpcVersion = 1;
|
||||||
static RpcConnection Instance;
|
static RpcConnection Instance;
|
||||||
static const size_t SendQueueSize = 4;
|
|
||||||
static RpcConnection::MessageFrame SendQueue[SendQueueSize];
|
|
||||||
static std::atomic_uint SendQueueNext = 0;
|
|
||||||
|
|
||||||
static RpcConnection::MessageFrame* NextSendFrame() {
|
|
||||||
auto index = (SendQueueNext++) % SendQueueSize;
|
|
||||||
return &SendQueue[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId)
|
/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId)
|
||||||
{
|
{
|
||||||
|
@ -42,13 +34,12 @@ void RpcConnection::Open()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto handshakeFrame = NextSendFrame();
|
sendFrame.opcode = Opcode::Handshake;
|
||||||
handshakeFrame->opcode = Opcode::Handshake;
|
char* json = sendFrame.message;
|
||||||
char* json = handshakeFrame->message;
|
|
||||||
JsonWriteHandshakeObj(json, RpcVersion, appId);
|
JsonWriteHandshakeObj(json, RpcVersion, appId);
|
||||||
handshakeFrame->length = json - handshakeFrame->message;
|
sendFrame.length = json - sendFrame.message;
|
||||||
|
|
||||||
if (connection->Write(handshakeFrame, sizeof(MessageFrameHeader) + handshakeFrame->length)) {
|
if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
|
||||||
state = State::Connected;
|
state = State::Connected;
|
||||||
if (onConnect) {
|
if (onConnect) {
|
||||||
onConnect();
|
onConnect();
|
||||||
|
@ -67,11 +58,10 @@ void RpcConnection::Close()
|
||||||
|
|
||||||
void RpcConnection::Write(const void* data, size_t length)
|
void RpcConnection::Write(const void* data, size_t length)
|
||||||
{
|
{
|
||||||
auto frame = NextSendFrame();
|
sendFrame.opcode = Opcode::Frame;
|
||||||
frame->opcode = Opcode::Frame;
|
memcpy(sendFrame.message, data, length);
|
||||||
memcpy(frame->message, data, length);
|
sendFrame.length = length;
|
||||||
frame->length = length;
|
if (!connection->Write(&sendFrame, sizeof(MessageFrameHeader) + length)) {
|
||||||
if (!connection->Write(frame, sizeof(MessageFrameHeader) + length)) {
|
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,7 @@ struct RpcConnection {
|
||||||
char appId[64]{};
|
char appId[64]{};
|
||||||
int lastErrorCode{0};
|
int lastErrorCode{0};
|
||||||
char lastErrorMessage[256]{};
|
char lastErrorMessage[256]{};
|
||||||
|
RpcConnection::MessageFrame sendFrame;
|
||||||
|
|
||||||
static RpcConnection* Create(const char* applicationId);
|
static RpcConnection* Create(const char* applicationId);
|
||||||
static void Destroy(RpcConnection*&);
|
static void Destroy(RpcConnection*&);
|
||||||
|
|
Loading…
Reference in a new issue