mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-06-17 14:21:30 +00:00
Optimize sendMessageEnet() with fewer allocations and less time holding locks
This commit is contained in:
+9
-20
@@ -297,35 +297,31 @@ static PNVCTL_TCP_PACKET_HEADER readNvctlPacketTcp(void) {
|
|||||||
static int sendMessageEnet(short ptype, short paylen, const void* payload) {
|
static int sendMessageEnet(short ptype, short paylen, const void* payload) {
|
||||||
PNVCTL_ENET_PACKET_HEADER packet;
|
PNVCTL_ENET_PACKET_HEADER packet;
|
||||||
ENetPacket* enetPacket;
|
ENetPacket* enetPacket;
|
||||||
ENetEvent event;
|
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
LC_ASSERT(AppVersionQuad[0] >= 5);
|
LC_ASSERT(AppVersionQuad[0] >= 5);
|
||||||
|
|
||||||
packet = malloc(sizeof(*packet) + paylen);
|
enetPacket = enet_packet_create(NULL, sizeof(*packet) + paylen, ENET_PACKET_FLAG_RELIABLE);
|
||||||
if (packet == NULL) {
|
if (enetPacket == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
packet = (PNVCTL_ENET_PACKET_HEADER)enetPacket->data;
|
||||||
packet->type = ptype;
|
packet->type = ptype;
|
||||||
memcpy(&packet[1], payload, paylen);
|
memcpy(&packet[1], payload, paylen);
|
||||||
|
|
||||||
enetPacket = enet_packet_create(packet, sizeof(*packet) + paylen, ENET_PACKET_FLAG_RELIABLE);
|
PltLockMutex(&enetMutex);
|
||||||
if (enetPacket == NULL) {
|
err = enet_peer_send(peer, 0, enetPacket);
|
||||||
free(packet);
|
PltUnlockMutex(&enetMutex);
|
||||||
return 0;
|
if (err < 0) {
|
||||||
}
|
|
||||||
|
|
||||||
if (enet_peer_send(peer, 0, enetPacket) < 0) {
|
|
||||||
Limelog("Failed to send ENet control packet\n");
|
Limelog("Failed to send ENet control packet\n");
|
||||||
enet_packet_destroy(enetPacket);
|
enet_packet_destroy(enetPacket);
|
||||||
free(packet);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PltLockMutex(&enetMutex);
|
||||||
enet_host_flush(client);
|
enet_host_flush(client);
|
||||||
|
PltUnlockMutex(&enetMutex);
|
||||||
free(packet);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -361,9 +357,7 @@ static int sendMessageAndForget(short ptype, short paylen, const void* payload)
|
|||||||
// Unlike regular sockets, ENet sockets aren't safe to invoke from multiple
|
// Unlike regular sockets, ENet sockets aren't safe to invoke from multiple
|
||||||
// threads at once. We have to synchronize them with a lock.
|
// threads at once. We have to synchronize them with a lock.
|
||||||
if (AppVersionQuad[0] >= 5) {
|
if (AppVersionQuad[0] >= 5) {
|
||||||
PltLockMutex(&enetMutex);
|
|
||||||
ret = sendMessageEnet(ptype, paylen, payload);
|
ret = sendMessageEnet(ptype, paylen, payload);
|
||||||
PltUnlockMutex(&enetMutex);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ret = sendMessageTcp(ptype, paylen, payload);
|
ret = sendMessageTcp(ptype, paylen, payload);
|
||||||
@@ -374,14 +368,9 @@ static int sendMessageAndForget(short ptype, short paylen, const void* payload)
|
|||||||
|
|
||||||
static int sendMessageAndDiscardReply(short ptype, short paylen, const void* payload) {
|
static int sendMessageAndDiscardReply(short ptype, short paylen, const void* payload) {
|
||||||
if (AppVersionQuad[0] >= 5) {
|
if (AppVersionQuad[0] >= 5) {
|
||||||
PltLockMutex(&enetMutex);
|
|
||||||
|
|
||||||
if (!sendMessageEnet(ptype, paylen, payload)) {
|
if (!sendMessageEnet(ptype, paylen, payload)) {
|
||||||
PltUnlockMutex(&enetMutex);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PltUnlockMutex(&enetMutex);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PNVCTL_TCP_PACKET_HEADER reply;
|
PNVCTL_TCP_PACKET_HEADER reply;
|
||||||
|
|||||||
Reference in New Issue
Block a user