Sdk/LuaBinding: Bind UdpSocket
This commit is contained in:
parent
3b7881ebfe
commit
09bace0f28
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include <Nazara/Network/AbstractSocket.hpp>
|
#include <Nazara/Network/AbstractSocket.hpp>
|
||||||
#include <Nazara/Network/IpAddress.hpp>
|
#include <Nazara/Network/IpAddress.hpp>
|
||||||
|
#include <Nazara/Network/UdpSocket.hpp>
|
||||||
#include <NDK/Lua/LuaBinding_Base.hpp>
|
#include <NDK/Lua/LuaBinding_Base.hpp>
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
|
|
@ -23,6 +24,7 @@ namespace Ndk
|
||||||
|
|
||||||
Nz::LuaClass<Nz::AbstractSocket> abstractSocket;
|
Nz::LuaClass<Nz::AbstractSocket> abstractSocket;
|
||||||
Nz::LuaClass<Nz::IpAddress> ipAddress;
|
Nz::LuaClass<Nz::IpAddress> ipAddress;
|
||||||
|
Nz::LuaClass<Nz::UdpSocket> udpSocket;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ namespace Ndk
|
||||||
{
|
{
|
||||||
std::size_t argCount = std::min<std::size_t>(argumentCount, 9U);
|
std::size_t argCount = std::min<std::size_t>(argumentCount, 9U);
|
||||||
|
|
||||||
int argIndex = 2;
|
int argIndex = 1;
|
||||||
switch (argCount)
|
switch (argCount)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
|
|
@ -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));
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -154,6 +211,7 @@ namespace Ndk
|
||||||
// Classes
|
// Classes
|
||||||
abstractSocket.Register(instance);
|
abstractSocket.Register(instance);
|
||||||
ipAddress.Register(instance);
|
ipAddress.Register(instance);
|
||||||
|
udpSocket.Register(instance);
|
||||||
|
|
||||||
// Enums
|
// Enums
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
#include <Nazara/Lua.hpp> // Module de scripting
|
#include <Nazara/Lua.hpp> // Module de scripting
|
||||||
#include <Nazara/Graphics.hpp> // Module graphique
|
#include <Nazara/Graphics.hpp> // Module graphique
|
||||||
#include <Nazara/Renderer.hpp> // Module de rendu
|
#include <Nazara/Renderer.hpp> // Module de rendu
|
||||||
|
#include <Nazara/Network.hpp> // Module utilitaire
|
||||||
#include <Nazara/Utility.hpp> // Module utilitaire
|
#include <Nazara/Utility.hpp> // Module utilitaire
|
||||||
#include <NDK/Application.hpp>
|
#include <NDK/Application.hpp>
|
||||||
#include <NDK/Components.hpp>
|
#include <NDK/Components.hpp>
|
||||||
|
|
@ -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 est une classe s'occupant de l'initialisation du moteur ainsi que de la gestion de beaucoup de choses
|
||||||
Ndk::Application application;
|
Ndk::Application application;
|
||||||
|
Nz::Initializer<Nz::Network> network;
|
||||||
|
|
||||||
// Nazara étant initialisé, nous pouvons créer le monde pour contenir notre scène.
|
// 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.
|
// 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.
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ namespace Nz
|
||||||
|
|
||||||
inline IpAddress GetBoundAddress() const;
|
inline IpAddress GetBoundAddress() const;
|
||||||
inline UInt16 GetBoundPort() const;
|
inline UInt16 GetBoundPort() const;
|
||||||
inline SocketState GetState() const;
|
|
||||||
|
|
||||||
inline bool IsBroadcastingEnabled() const;
|
inline bool IsBroadcastingEnabled() const;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,16 +103,6 @@ namespace Nz
|
||||||
return m_boundAddress.GetPort();
|
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
|
* \brief Checks whether the broadcasting is enabled
|
||||||
* \return true If it is the case
|
* \return true If it is the case
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue