1. Post #161
    King of the Oil Refinery
    Tobba's Avatar
    December 2008
    6,229 Posts
    Trying to do a server query, but it wont respond
    require("oosocks")
    
    local connection = OOSock(IPPROTO_UDP);
    
    connection:SetCallback(function(socket, callType, callId, err, data, peer, peerPort)
    	if(callType == SCKCALL_BIND && err == SCKERR_OK) then
    		print("Bound.");
    		connection:Send("\255\255\255\255TSource Engine Query\0", "91.192.210.199", 27015);
    		connection:ReceiveDatagram();
    	end
    	
    	if(callType == SCKCALL_SEND && err == SCKERR_OK) then
    		print("Sent.");
    	end
    	
    	if(callType == SCKCALL_REC_DATAGRAM && err == SCKERR_OK) then
    		print("Got '" .. data .. "'  from  " .. peer .. ":" .. tostring(peerPort));
    		socket:Close()
    	end
    	
    	if(err != SCKERR_OK) then
    		socket:Close()
    	end
    end);
    
    connection:Bind("", 27015);

    Edited:

    Can you please make it possible to send NULLs? shouldent be too hard as strings in lua isnt NULL terminated, and
    string.len("lol\0wut")
    returns 7

  2. Post #162
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Gmod API doesn't support it.

    I will do something like {"String", 0, "Bob", 23}
    Which would equate to "String\0Bob\23"

  3. Post #163
    King of the Oil Refinery
    Tobba's Avatar
    December 2008
    6,229 Posts
    Chrisaster tested, strings in lua isnt NULL terminated, so it should be simple

  4. Post #164
    IS AWESOME@@@@@@@@@@

    January 2009
    968 Posts
    http://www.facepunch.com/showpost.ph...&postcount=203

    If string.len works then you might even be able to use ILuaInterface::StringLength instead of manual specification.

  5. Post #165
    King of the Oil Refinery
    Tobba's Avatar
    December 2008
    6,229 Posts
    string.len works just fine

    Edited:

    Read the help description thingy on StringLength, it says lua strings can have nulls

    Edited:

    LUA_FUNCTION(ReadString)
    {
    	gLua->CheckType(1, GLua::TYPE_STRING);
    
    	const char *str = Lua()->GetString(1);
    	unsigned int len = Lua()->StringLength(1);
    
    	Msg("STR[");
    
    	for (unsigned int i=0;i<len;i++)
    		Msg("%c", str[i]);
    
    	Msg("]\n");
    
    	return 0;
    }
    

    Crashed me, but its becuse of enginespew/something else is messing up i think

    Edited:

    StingLength cause a crash on any string

  6. Post #166
    Gold Member
    LuaStoned's Avatar
    September 2007
    1,358 Posts
    Anyone got a working server query (not masterserver) script? I'm not getting it :(

  7. Post #167
    King of the Oil Refinery
    Tobba's Avatar
    December 2008
    6,229 Posts
    Source server queries must end with a NULL char if i recall correctly

  8. Post #168
    IS AWESOME@@@@@@@@@@

    January 2009
    968 Posts
    Source server queries must end with a NULL char if i recall correctly
    Yes, because "Source Engine Query" is a NULL terminated string.

  9. Post #169
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    I've fixed the module so you can use strings with null characters.

    But, I cant release it until Garry adds a function to GMod to allow pushing sized strings.

    I cant do it(not easily) since lua_pushlstring is not included in lua_shared since its taken out at link time. And gmod lua uses non standard lua_State structure, so I can't compile my own in.

  10. Post #170
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Updated with BinRead.

  11. Post #171
    Gold Member
    LuaStoned's Avatar
    September 2007
    1,358 Posts
    Have you added ReadString here too? :D

  12. Post #172
    likes men
    Python1320's Avatar
    May 2007
    1,722 Posts
    Could we have a TCP Server example? I can't seem to get mine to work.

    Oh and linux oosocks is outdated. It should now be _linux.dll
    Plus some of the engine files changed name.

  13. Post #173
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Updated Linux.

    Edited:

    Example TCP server acceptor.

    Might not work, but thats the concept.

    require("OOSocks");
    
    local clients = {};
    
    local function ClientSockCallback(socket, callType, callId, err, data, peer, peerPort)
    	if(err != SCKERR_OK) then
    		Error(err);
    	end
    
    	if(callType == SCKCALL_REC_LINE) then
    		print(data);
    		if(data != "END") then
    			socket:ReceiveLine(); -- If the client hasnt told us they are done, read annother line!
    		end
    	end
    end
    
    
    -- This is our listen socket. It will just accept every socket into clients.
    
    local ServerSock = OOSock(IPPROTO_TCP);
    
    ServerSock:SetCallback(function(socket, callType, callId, err, data, peer, peerPort)
    	if(err != SCKERR_OK) then
    		Error(err);
    	end
    
    	if(callType == SCKCALL_BIND) then
    		socket:Listen(10);
    	end
    
    	if(callType == SCKCALL_LISTEN) then
    		socket:Accept();
    	end
    
    	if(callType == SCKCALL_ACCEPT) then
    		print("Got client " .. peer .. "\n");
    				
    		data:SetCallback(ClientSockCallback);
    		data:ReceiveLine() -- Lets for some reason, read 1 line.
    
    		table.insert(clients, data); -- I dunno, we might need em.
    
    		-- Accept annother socket.
    		socket:Accept();
    	end
    end)
    
    ServerSock:Bind("", 1000);

  14. Post #174
    Grocel's Avatar
    October 2008
    1,249 Posts
    We will we are able to send binary stuff?

  15. Post #175
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    You can send binary as a string. It supports \0

  16. Post #176
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Updated fixing Accept socket bug.

  17. Post #177
    likes men
    Python1320's Avatar
    May 2007
    1,722 Posts
    For some reason even your example code results into
    Code:
    attempt to call a userdata value
    on console. No error lines or anything. Really confusing.

  18. Post #178
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Yeah thats the fix. ;) update the .dll

  19. Post #179
    likes men
    Python1320's Avatar
    May 2007
    1,722 Posts
    Linux build too, please? ;)
    I haven't had luck compiling in linux yet.

  20. Post #180
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Updated linux.

  21. Post #181
    likes men
    Python1320's Avatar
    May 2007
    1,722 Posts
    Outside of any hook. If I do clients[#clients]:SendLine"Greetings"
    I receive the message only after sending something from the other end :s
    Probably because of the issued ReceiveLine?

  22. Post #182
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Outside of any hook. If I do clients[#clients]:SendLine"Greetings"
    I receive the message only after sending something from the other end :s
    Probably because of the issued ReceiveLine?
    Receive and Send buffers are supposed to be separate. I'll have a look at it thou.

  23. Post #183
    kna_rus's Avatar
    September 2008
    121 Posts
    Queueing mechanism seems to suck.

    sock:ReceiveLine()
    sock:Send"Hax"
    sock:Send"Hax"
    sock:Send"Hax"
    sock:ReceiveLine()
    This code will send only after it receives. If it does not receive anything, my data will never be sent. Additionally, after it receives one line, it will send one "hax" and then wait for another line before sending rest of data.
    If someone needs to wait for data while sending data on demand, and do that quick, this module is far worse that luasocket.
    A possible solution is having two threads, one for sending and other for receiving.
    Reply With Quote Edit / Delete Reply Russian Federation Show Events Agree Agree x 1 (list)

  24. Post #184
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    This module works fine.

    If you have a problem, give me a full blown example. It is most likely you are doing something wrong.

    I have a fully working HTTP server with this module.

    Are you using the latest bins? Are you using linux or windows? What is on the other end of the socket?

  25. Post #185
    kna_rus's Avatar
    September 2008
    121 Posts
    Latest (or almost latest) bins, both windows and linux, on other end - luajit with luasocket doing send(recv()).
    Code:
    local rs = {"x.x.x.x",27020}
    RETR_Socket = OOSock(IPPROTO_TCP)
    RETR_Socket:SetCallback(function(sc,ct,id,err,data,ip,port)
    	print("Sox callback")
    	if err ~= SCKERR_OK then ErrorNoHalt("Sock Error!"..err.." "..data) SocketReconnect() end
    	if ct == SCKCALL_CONNECT then
    		sc:ReceiveLine() //start it
    	elseif ct == SCKCALL_REC_LINE then
    		if data:find"HEARTBEAT\r?\n?" then
    			sc:Send("HEARTBEAT2\r\n")
    			RETR_LHb = os.time()
    			return
    		end
    		local dt = string.match(data,"DATA: (%S+) ")
    		if dt then
    			//stuff about processing data
    		end
    		timer.Simple(0.1, function() sc:ReceiveLine() end) //otherwise sends can be fucked up
    	end
    end)
    RETR_Socket:Bind("",0)
    RETR_Socket:Connect(rs[1],rs[2])
    function SocketPreChangeLevel()
    	if RETR_Socket then //to avoid crashing
    		RETR_Socket:Close() //anyways it crashes on mapchange
    		RETR_Socket = nil //probably problem of old version
    	end
    end
    hook.Add("Shutdown", "SocketShutdown", SocketPreChangeLevel)
    function SocketReconnect()
    	RETR_Socket:Connect(rs[1],rs[2])
    end
    function SendMessage(dt,data)
    	RETR_Socket:Send("DATA: "..dt.." "..data.."\r\n")
    end
    When luasocket was working in gmod, I was able to get almost instant message exchange between servers. With OOSocks I have to wait either for heartbeat or for incoming message to send my data.
    My current solution is my own luasocket-like module :D

  26. Post #186
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Updated windows and linux, should be fixed now.

  27. Post #187
    pak5695's Avatar
    March 2010
    11 Posts
    None of the http examples worked, any ideas?

  28. Post #188
    kna_rus's Avatar
    September 2008
    121 Posts
    Do not test them on empty server. Add a bot, then run examples.
    And give us more info, if we don't know anything - we can't help.

  29. Post #189
    pak5695's Avatar
    March 2010
    11 Posts
    Why would I need a bot to get data from a php script, I have a script that on a website that takes two values in, does a mySQL query and spits out a 1 or a 0, I want to be able to get a response by what ever the user put into the two text field. I don't know if theres an easier way but after digging around for 30 minutes, I could only find this becouse LuaSocket doesnt work.

  30. Post #190
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    Do not test them on empty server. Add a bot, then run examples.
    And give us more info, if we don't know anything - we can't help.
    Did that update fix your problem?

  31. Post #191
    kna_rus's Avatar
    September 2008
    121 Posts
    @pak5695: this is how source servers are made. Think hook won't be called unless there are players on server. All threaded modules are using Think hook for checking results.
    @haza: I have my own module now, but after reading your code I would say that it is still not fixed. You are using select() wrong. First argument is "highest-numbered file descriptor plus 1", not zero. As you have only one file descriptor (socket itself) is should be (socket->m_iSocket+1). This seems to be reason why all recv calls block until they get data.

  32. Post #192
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    It's ignored on windows, so that was probally what my thinking was. Are you using linux? Cause then that would explain why its blocking, select would return an error and then the read set wouldn't have changed.

    I'll update linux + windows, and if you would be so kind as to test it, I would be grateful.

  33. Post #193
    Vinze's Avatar
    August 2009
    4,042 Posts
    OOSocks is multi-threaded, object orientated callback based module.
    :geno:

    I guess I'm off to the dumb folk section where things makes sense again...
    Reply With Quote Edit / Delete Reply Spain Show Events Dumb Dumb x 1 (list)

  34. Post #194
    DarkTyrael's Avatar
    January 2008
    54 Posts
    Does anyone know what BAD error mean in a CONNECT callback?

    Code:
    Socket : Error (Clientside)
    ----- ----- ----- -----
    Error    : SCKERR_BAD
    CallType : SCKCALL_CONNECT
    CallID   : nil
    ----- ----- ----- -----
    I'm getting this error when I try to connect to my dedicated server's OOSocks.
    Also, it works perfectly when I test it in Singleplayer (both client and server), so I think the error doesn't come from my code...

  35. Post #195
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    BAD means it could be anything that isn't included in the error list.

    This is because I can only translate so many errors, since some don't exists under the same name in windows and linux.

    If you can show us your code maybe we can help.

  36. Post #196
    ColdFusion's Avatar
    December 2009
    2,227 Posts
    Does anyone know what BAD error mean in a CONNECT callback?

    Code:
    Socket : Error (Clientside)
    ----- ----- ----- -----
    Error    : SCKERR_BAD
    CallType : SCKCALL_CONNECT
    CallID   : nil
    ----- ----- ----- -----
    I'm getting this error when I try to connect to my dedicated server's OOSocks.
    Also, it works perfectly when I test it in Singleplayer (both client and server), so I think the error doesn't come from my code...
    Do you rent a server or do you rent a box ?

  37. Post #197
    DarkTyrael's Avatar
    January 2008
    54 Posts
    Do you rent a server or do you rent a box ?
    I'm hosting it on the same PC I'm running the game.

    Here's the Client connection / Server listening code:
    http://meeeeoooow.pastebin.com/cLTV1uVq

  38. Post #198
    Gold Member
    LuaStoned's Avatar
    September 2007
    1,358 Posts
    Waiting for haza do fix incoming data :cop:

  39. Post #199
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    I'm hosting it on the same PC I'm running the game.

    Here's the Client connection / Server listening code:
    http://meeeeoooow.pastebin.com/cLTV1uVq
    Fixed.
    function Mw.Connect()
            if Mw.Connected then Mw.Disconnect(true) end
            g_Ready = false
            -- Create the socket, bind it, connect
            Mw.Socket = OOSock(IPPROTO_TCP)
            Mw.Socket:SetBinaryMode(true)
            Mw.Socket:SetCallback(Callback)
            Mw.Socket:Connect(Mw.CV.ServerIP:GetString(), Mw.CV.ServerPort:GetInt())
        end
    

    Fixed.
    Mw.Err = {}
        Mw.Err[SCKERR_OK] = "SCKERR_OK"
        Mw.Err[SCKERR_NOT_CONNECTED] = "SCKERR_NOT_CONNECTED"
        Mw.Err[SCKERR_CONNECTION_RESET] = "SCKERR_CONNECTION_RESET"
        Mw.Err[SCKERR_TIMED_OUT] = "SCKERR_TIMED_OUT"
        Mw.Err[SCKERR_BAD] = "SCKERR_BAD"
     
    Mw.CType = {}
        Mw.CType[SCKCALL_CONNECT] = "SCKCALL_CONNECT"
        Mw.CType[SCKCALL_REC_SIZE] = "SCKCALL_REC_SIZE"
        Mw.CType[SCKCALL_REC_LINE] = "SCKCALL_REC_LINE"
        Mw.CType[SCKCALL_SEND] = "SCKCALL_SEND"
        Mw.CType[SCKCALL_BIND] = "SCKCALL_BIND"
        Mw.CType[SCKCALL_ACCEPT] = "SCKCALL_ACCEPT"
        Mw.CType[SCKCALL_LISTEN] = "SCKCALL_LISTEN"
        Mw.CType[SCKCALL_REC_DATAGRAM] = "SCKCALL_REC_DATAGRAM"

  40. Post #200
    DarkTyrael's Avatar
    January 2008
    54 Posts
    Fixed.
    [lua]
    Done.
    Still getting the same errors:
    Working in Singleplayer
    BAD - CONNECT (client) using srcds or "Create Multiplayer" in GMod

    I'll open my ports and ask someone else to connect to my server...