Skip to content

Commit

Permalink
Sdk/LuaBinding: Bind UdpSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed May 18, 2017
1 parent ab76aa0 commit 101cdd1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
2 changes: 2 additions & 0 deletions SDK/include/NDK/Lua/LuaBinding_Network.hpp
Expand Up @@ -9,6 +9,7 @@

#include <Nazara/Network/AbstractSocket.hpp>
#include <Nazara/Network/IpAddress.hpp>
#include <Nazara/Network/UdpSocket.hpp>
#include <NDK/Lua/LuaBinding_Base.hpp>

namespace Ndk
Expand All @@ -23,6 +24,7 @@ namespace Ndk

Nz::LuaClass<Nz::AbstractSocket> abstractSocket;
Nz::LuaClass<Nz::IpAddress> ipAddress;
Nz::LuaClass<Nz::UdpSocket> udpSocket;
};
}

Expand Down
60 changes: 59 additions & 1 deletion SDK/src/NDK/Lua/LuaBinding_Network.cpp
Expand Up @@ -32,7 +32,7 @@ namespace Ndk
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 9U);

int argIndex = 2;
int argIndex = 1;
switch (argCount)
{
case 0:
Expand Down Expand Up @@ -142,6 +142,63 @@ namespace Ndk
}
});
}

udpSocket.Reset("UdpSocket");
{
udpSocket.Inherit<Nz::AbstractSocket>(abstractSocket);

udpSocket.BindDefaultConstructor();

udpSocket.BindMethod("Create", &Nz::UdpSocket::Create);
udpSocket.BindMethod("EnableBroadcasting", &Nz::UdpSocket::EnableBroadcasting);
udpSocket.BindMethod("GetBoundAddress", &Nz::UdpSocket::GetBoundAddress);
udpSocket.BindMethod("GetBoundPort", &Nz::UdpSocket::GetBoundPort);
udpSocket.BindMethod("IsBroadcastingEnabled", &Nz::UdpSocket::IsBroadcastingEnabled);
udpSocket.BindMethod("QueryMaxDatagramSize", &Nz::UdpSocket::QueryMaxDatagramSize);

udpSocket.BindMethod("Bind", [](Nz::LuaInstance& lua, Nz::UdpSocket& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "IpAddress"))
return lua.Push(instance.Bind(*static_cast<Nz::IpAddress*>(lua.ToUserdata(argIndex))));
else
return lua.Push(instance.Bind(lua.Check<Nz::UInt16>(&argIndex)));
});

udpSocket.BindMethod("Receive", [](Nz::LuaInstance& lua, Nz::UdpSocket& instance, std::size_t /*argumentCount*/) -> int
{
Nz::IpAddress from;

std::array<char, 0xFFFF> buffer;
std::size_t received;
if (instance.Receive(buffer.data(), buffer.size(), &from, &received))
{
lua.PushBoolean(true);
lua.PushString(from.ToString());
lua.PushString(buffer.data(), received);
return 3;
}

lua.PushBoolean(false);
return 1;
});

udpSocket.BindMethod("Send", [](Nz::LuaInstance& lua, Nz::UdpSocket& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
Nz::String to = lua.Check<Nz::String>(&argIndex);

std::size_t bufferLength;
const char* buffer = lua.CheckString(argIndex, &bufferLength);

std::size_t sent;
bool ret;
if ((ret = instance.Send(Nz::IpAddress(to), buffer, bufferLength, &sent)) != true)
sent = 0;

return lua.Push(std::make_pair(ret, sent));
});
}
}

/*!
Expand All @@ -154,6 +211,7 @@ namespace Ndk
// Classes
abstractSocket.Register(instance);
ipAddress.Register(instance);
udpSocket.Register(instance);

// Enums

Expand Down
2 changes: 2 additions & 0 deletions examples/FirstScene/main.cpp
Expand Up @@ -16,6 +16,7 @@
#include <Nazara/Lua.hpp> // Module de scripting
#include <Nazara/Graphics.hpp> // Module graphique
#include <Nazara/Renderer.hpp> // Module de rendu
#include <Nazara/Network.hpp> // Module utilitaire
#include <Nazara/Utility.hpp> // Module utilitaire
#include <NDK/Application.hpp>
#include <NDK/Components.hpp>
Expand All @@ -33,6 +34,7 @@ int main()
{
// Ndk::Application est une classe s'occupant de l'initialisation du moteur ainsi que de la gestion de beaucoup de choses
Ndk::Application application;
Nz::Initializer<Nz::Network> network;

// Nazara étant initialisé, nous pouvons créer le monde pour contenir notre scène.
// Dans un ECS, le monde représente bien ce que son nom indique, c'est l'ensemble de ce qui existe au niveau de l'application.
Expand Down
1 change: 0 additions & 1 deletion include/Nazara/Network/UdpSocket.hpp
Expand Up @@ -33,7 +33,6 @@ namespace Nz

inline IpAddress GetBoundAddress() const;
inline UInt16 GetBoundPort() const;
inline SocketState GetState() const;

inline bool IsBroadcastingEnabled() const;

Expand Down
10 changes: 0 additions & 10 deletions include/Nazara/Network/UdpSocket.inl
Expand Up @@ -103,16 +103,6 @@ namespace Nz
return m_boundAddress.GetPort();
}

/*!
* \brief Gets the state of the socket
* \return State of the socket
*/

inline SocketState UdpSocket::GetState() const
{
return m_state;
}

/*!
* \brief Checks whether the broadcasting is enabled
* \return true If it is the case
Expand Down

0 comments on commit 101cdd1

Please sign in to comment.