PLUGIN: UDP String Sockets.

The new ShiVa Asset Store. A forum for product inquiries, reviews, comments and critique.

PLUGIN: UDP String Sockets.

Postby Fraser » 2016-11-05 22:53

Just released a free UDP socket plugin:

http://www.shiva3dstore.com/sdudpsocket

Code: Select all
SD_Socket API: START

===============================================================================
nSocket = create ( nPortLocal, sAddressRemote, nPortRemote )
-------------------------------------------------------------------------------
Creates a non blocking UDP socket on the specified local port.
Will send data to the specified remote target address.
On success returns a socket identifier number to be used in all other functions.
On fail returns nil.
NOTE:  sAddressRemote must be like "127.0.0.1", nPortRemote is any valid port number.

===============================================================================
sAddressRemote, nPortRemote = getRemoteAddress ( nSocket )
-------------------------------------------------------------------------------
Returns the remote target address and port number.

===============================================================================
bOK = setRemoteAddress ( nSocket, sAddressRemote, nPortRemote )
-------------------------------------------------------------------------------
Respecify a remote target address.
This will change the socket to transmit to the new end point.
The end point previously specified with create() will be cleared.

===============================================================================
nPort = getLocalPort ( nSocket )
-------------------------------------------------------------------------------
Returns the specified sockets local port number.

===============================================================================
bOK = sendString( nSocket, sString )
-------------------------------------------------------------------------------
Transmits a string using the specified socket.

===============================================================================
sString, sRemoteAddress, nRemotePort = receiveString ( nSocket )
-------------------------------------------------------------------------------
Receives a string using the specified socket.
The senders address and port are also returned.
If no data is available it will return immediately with nil, nil, nil.
Call this every frame for minimal latency.

===============================================================================
bOK = destroy ( nSocket )
-------------------------------------------------------------------------------
Destroy the specified socket instance.

SD_Socket API: END
Last edited by Fraser on 2016-11-06 02:33, edited 1 time in total.
User avatar
Fraser
Platinum Boarder
Platinum Boarder
 
Posts: 1324
Joined: 2012-02-05 19:28
Location: Europe

Re: PLUGIN: UDP String Sockets.

Postby Shaderman » 2016-11-05 23:24

Thanks Fraser!

Which platforms are supported?
User avatar
Shaderman
Platinum Boarder
Platinum Boarder
 
Posts: 660
Joined: 2012-03-24 11:33
Location: Heidelberg, Germany

Re: PLUGIN: UDP String Sockets.

Postby Fraser » 2016-11-05 23:37

Windows and Android, Android is not tested but it should work, will update if it doesn't.

Notes on using this plugin:

When you send a string it is not guaranteed that the other end receives it (that's the nature of UDP), however you also get the benefit of not having packet pile-ups as found with TCP which ensures in order transfer where one slow packet will hold up the rest.

This is no problem for throw away data like realtime player position etc. provided one handles all cases of corruption, just use all valid RXs you get.

For guaranteed delivery of strings, for example low rate important game state stuff like "I_SHOT_U" one should implement some simple higher level protocol:

Make a simple packet like:

Code: Select all
local sStringToSend = "I_SHOT_U"
local nStringLen = string.getLength(sStringToSend)
local nUID = this.getUID()--returns incrementing AI var.
local sPacket = string.format ( "%s|%d|%d", sStringToSend, nStringLen, nUID )
SD_Socket.sendString ( nSocket, sPacket )


Then when other end receives use string explode on "|" and check string is correct len and send back ack packet:

Code: Select all
local sAckPacket = string.format ( "ACK|%d|%d", nStringLen, nUID )
SD_Socket.sendString ( nSocket, sAckPacket )


So the original sender waits for good ack packet back and maybe resends if not received after some timeout.

Some scheme like that, all wrapped up in a single user level AI could yield guaranteed delivery like TCP so you could just call:

Code: Select all
user.sendEvent ( hUser, "MySocketAI", "sendGuaranteed", nSocket, "I_SHOT_U" )
User avatar
Fraser
Platinum Boarder
Platinum Boarder
 
Posts: 1324
Joined: 2012-02-05 19:28
Location: Europe

Re: PLUGIN: UDP String Sockets.

Postby Fraser » 2016-11-06 00:03

Other note:

As it stands it is bandwidth wasteful, it sends only ASCII strings.

If anyone wants then I can add raw byte send capability which would need feeding with hex string like "FF,AD,7B,3C", made like:

Code: Select all
local sHex = ""
local nBytes = table.getLength ( tnMyNumberTable )
for nLoop = 0, nBytes-1
do
    sHex = sHex .. string.format ( "%X", table.getAt(tnMyNumberTable, nLoop)
end


The other end would receive hex string and parse it into a number array like this:

Code: Select all
for nLoop = 0, nHexBytes-1
do
    local sHexByteString = table.getAt ( tsMyHexByteStrings, nLoop )-- like "4E"
    table.setAt ( tnMyNumberTable, nLoop, string.toNumber ( "0x" ..  sHexByteString ) )
end


This way the socket could actually send raw byte data as efficiently as possible under the hood.
User avatar
Fraser
Platinum Boarder
Platinum Boarder
 
Posts: 1324
Joined: 2012-02-05 19:28
Location: Europe

Re: PLUGIN: UDP String Sockets.

Postby Wolfgangabersdorf » 2017-04-28 19:06

Hello.
In build time for android (using NDK 9 and 11)
Code: Select all
] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(Plugin.o):Plugin.cpp:function SD_Socket::~SD_Socket(): error: undefined reference to '__cxa_end_cleanup'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(Plugin.o):Plugin.cpp:function SD_Socket::SD_Socket(): error: undefined reference to '__cxa_end_cleanup'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(Plugin.o)(.ARM.extab+0x0): error: undefined reference to '__gxx_personality_v0'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(Plugin.o)(.ARM.extab+0x14): error: undefined reference to '__gxx_personality_v0'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(Plugin.o):Plugin.cpp:function _GLOBAL__sub_I_Plugin.cpp: error: undefined reference to 'std::ios_base::Init::Init()'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(Plugin.o):Plugin.cpp:function _GLOBAL__sub_I_Plugin.cpp: error: undefined reference to 'std::ios_base::Init::~Init()'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(SD_Socket.o):SD_Socket.cpp:function _GLOBAL__sub_I_SD_Socket.cpp: error: undefined reference to 'std::ios_base::Init::Init()'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(SD_Socket.o):SD_Socket.cpp:function _GLOBAL__sub_I_SD_Socket.cpp: error: undefined reference to 'std::ios_base::Init::~Init()'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(OBJ_Sockets.o):OBJ_Sockets.cpp:function OBJ_Sockets::create(unsigned short, bool, net::Address&): error: undefined reference to 'std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(OBJ_Sockets.o):OBJ_Sockets.cpp:function OBJ_Sockets::create(unsigned short, bool, net::Address&): error: undefined reference to 'std::cout'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(OBJ_Sockets.o):OBJ_Sockets.cpp:function _GLOBAL__sub_I_OBJ_Sockets.cpp: error: undefined reference to 'std::ios_base::Init::Init()'
     [exec] C:/workspace/game2d/obj/local/armeabi/SD_Socket_Android.a(OBJ_Sockets.o):OBJ_Sockets.cpp:function _GLOBAL__sub_I_OBJ_Sockets.cpp: error: undefined reference to 'std::ios_base::Init::~Init()'
     [exec] collect2.exe: error: ld returned 1 exit status
Wolfgangabersdorf
Junior Boarder
Junior Boarder
 
Posts: 20
Joined: 2014-04-09 13:15

Re: PLUGIN: UDP String Sockets.

Postby broozar » 2017-04-29 02:32

Lots of linker errors. I am not sure how often Fraser looks at the forum, so it would be great if you could contact him over the store as well and let him know. Might accelerate things :)
User avatar
broozar
Administrator
Administrator
 
Posts: 4089
Joined: 2007-09-16 10:22
Location: Berlin - Germany


Return to Asset Store

Who is online

Users browsing this forum: No registered users and 3 guests

cron