-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
206 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include <enet/enet.h> | ||
#include <client_peer.h> | ||
|
||
ClientPeer::ClientPeer(NAPI_CB) : Napi::ObjectWrap<ClientPeer>(info), peer(nullptr) {} | ||
|
||
Napi::Value ClientPeer::GetRTT(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->roundTripTime); | ||
} | ||
|
||
Napi::Value ClientPeer::GetState(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->state); | ||
} | ||
|
||
Napi::Value ClientPeer::GetChannelCount(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->channelCount); | ||
} | ||
|
||
Napi::Value ClientPeer::GetConnectID(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->connectID); | ||
} | ||
|
||
Napi::Value ClientPeer::GetIncomingBandwidth(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->incomingBandwidth); | ||
} | ||
|
||
Napi::Value ClientPeer::GetOutgoingBandwidth(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->outgoingBandwidth); | ||
} | ||
|
||
Napi::Value ClientPeer::GetPacketsLost(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->packetsLost); | ||
} | ||
|
||
Napi::Value ClientPeer::GetPacketsSent(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->packetsSent); | ||
} | ||
|
||
Napi::Value ClientPeer::GetPacketLoss(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->packetLoss); | ||
} | ||
|
||
Napi::Value ClientPeer::GetTotalWaitingData(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, peer->totalWaitingData); | ||
} | ||
|
||
Napi::Value ClientPeer::GetAddress(NAPI_CB) | ||
{ | ||
Napi::Env env = info.Env(); | ||
|
||
Napi::Object obj = Napi::Object::New(env); | ||
|
||
char ipStr[INET_ADDRSTRLEN]; | ||
if (inet_ntop(AF_INET, &(peer->address.host), ipStr, INET_ADDRSTRLEN) == NULL) | ||
{ | ||
Napi::Error::New(env, "Failed to convert IP address").ThrowAsJavaScriptException(); | ||
return env.Null(); | ||
} | ||
|
||
obj.Set("address", Napi::String::New(env, ipStr)); | ||
obj.Set("port", Napi::Number::New(env, peer->address.port)); | ||
return obj; | ||
} | ||
|
||
Napi::Object ClientPeer::Init(Napi::Env env, Napi::Object exports) | ||
{ | ||
|
||
// clang-format off | ||
Napi::Function func = DefineClass(env, "Peer", { | ||
InstanceMethod("getRTT", &ClientPeer::GetRTT), | ||
InstanceMethod("getState", &ClientPeer::GetState), | ||
InstanceMethod("getChannelCount", &ClientPeer::GetChannelCount), | ||
InstanceMethod("getConnectID", &ClientPeer::GetConnectID), | ||
InstanceMethod("getIncomingBandwidth", &ClientPeer::GetIncomingBandwidth), | ||
InstanceMethod("getOutgoingBandwidth", &ClientPeer::GetOutgoingBandwidth), | ||
InstanceMethod("getPacketsLost", &ClientPeer::GetPacketsLost), | ||
InstanceMethod("getPacketsSent", &ClientPeer::GetPacketsSent), | ||
InstanceMethod("getPacketLoss", &ClientPeer::GetPacketLoss), | ||
InstanceMethod("getTotalWaitingData", &ClientPeer::GetTotalWaitingData), | ||
InstanceMethod("getAddress", &ClientPeer::GetAddress) | ||
}); | ||
// clang-format on | ||
|
||
Napi::FunctionReference *constructor = new Napi::FunctionReference(); | ||
*constructor = Napi::Persistent(func); | ||
exports.Set("Peer", func); | ||
env.SetInstanceData(constructor); | ||
|
||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <napi.h> | ||
#include <enet/enet.h> | ||
|
||
#ifdef _WIN32 | ||
// Windows specific headers and code | ||
#include <winsock2.h> | ||
#include <ws2tcpip.h> // for InetNtop | ||
#else | ||
// Unix-like specific headers and code | ||
#include <arpa/inet.h> | ||
#endif | ||
|
||
#define NAPI_CB const Napi::CallbackInfo &info | ||
|
||
class ClientPeer : public Napi::ObjectWrap<ClientPeer> | ||
{ | ||
public: | ||
static Napi::Object Init(Napi::Env env, Napi::Object exports); | ||
ClientPeer(NAPI_CB); | ||
|
||
void SetENetPeer(ENetPeer *peer) { this->peer = peer; } | ||
|
||
private: | ||
Napi::Value GetRTT(NAPI_CB); | ||
Napi::Value GetState(NAPI_CB); | ||
Napi::Value GetChannelCount(NAPI_CB); | ||
Napi::Value GetConnectID(NAPI_CB); | ||
Napi::Value GetData(NAPI_CB); | ||
Napi::Value GetIncomingBandwidth(NAPI_CB); | ||
Napi::Value GetOutgoingBandwidth(NAPI_CB); | ||
Napi::Value GetPacketsLost(NAPI_CB); | ||
Napi::Value GetPacketsSent(NAPI_CB); | ||
Napi::Value GetPacketLoss(NAPI_CB); | ||
Napi::Value GetTotalWaitingData(NAPI_CB); | ||
Napi::Value GetAddress(NAPI_CB); | ||
|
||
ENetPeer *peer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
export interface PeerData { | ||
netID: number; | ||
} | ||
|
||
export * from "./client"; | ||
export * from "./packets"; | ||
export * from "./events"; | ||
export * from "./items"; | ||
export * from "./peer"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export interface NativePeerMethod { | ||
getRTT: () => number; | ||
getState: () => number; | ||
getChannelCount: () => number; | ||
getConnectID: () => number; | ||
getIncomingBandwidth: () => unknown; | ||
getOutgoingBandwidth: () => number; | ||
getPacketsLost: () => number; | ||
getPacketsSent: () => number; | ||
getTotalWaitingData: () => number; | ||
getAddress: () => NativeAddress; | ||
} | ||
|
||
export interface NativePeerAddress { | ||
address: string; | ||
host: number; | ||
} | ||
|
||
export interface PeerData { | ||
netID: number; | ||
enet: NativePeerMethod; | ||
} |