Lua-Enet cant create peer

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Purple Fedora
Prole
Posts: 23
Joined: Fri Oct 26, 2018 8:41 pm

Lua-Enet cant create peer

Post by Purple Fedora »

I have written an system for networking. It does'nt work in "client" or "both" mode. it says it cant create the peer. Thanks for your help

This is my Code:

Code: Select all

function love.load()
	socket = require('socket')
	enet = require("enet")
	Serialize = require('knife-serialize')
	
	hostIP = socket.dns.toip(socket.dns.gethostname())
	serverHostPort = 6666
	clientHostPort = 7777
	
	networkingStage = 0
	networkingMode = "b" --"s" or "c" or "b"   (server, client, both)
end


function love.update( dt )
	if networkingStage == 0 then    --this Stage creates the objects for networking
		
		if networkingMode == "s" or networkingMode == "b" then
			server = enet.host_create((hostIP.. ":" ..serverHostPort), 64, 3)
			password = nil
			clientList = {number = 0, 0}
			networkingStage = 1
			server:service()
		end
		if networkingMode == "c" or networkingMode == "b" then
			client = enet.host_create((hostIP.. ":" ..clientHostPort), 64, 3)
			
			cserver = nil
			serverToConnect = {nil, nil}    --{ip, port}
			if networkingMode == "b" then
				serverToConnect = {hostIP, serverHostPort}    --{ip, port}
			end
			serverpwd = nil
			confail = nil
			clientID = nil
			networkingStage = 1
			client:service()
		end

	elseif networkingStage == 1 then    --in this stage the objects connect
		if networkingMode == "c" or networkingMode == "b" then
			
			--serverToConnect must be defined
			cserver = client:connect((serverToConnect[1].. ":" ..serverToConnect[2]), 3, 1)
			cserver:send(Serialize({"#con1#", serverpwd, (hostIP.. ":" ..clientHostPort)}), 1, "reliable")
			client:service()
		end
		if networkingMode == "s" or networkingMode == "b" then
			local newEvent = true
			while newEvent ~= nil do
				newEvent = server:check_events()
				
				--removes irrevelant events
				if newEvent ~= nil and (newEvent.type == "connect" or newEvent.type == "disconnect") then
					newEvent = server:check_events()
				end
				
				--connects clients
				if newEvent ~= nil and newEvent.type == "receive" then
					local reconData = loadstring(newEvent.data)
					if reconData[1] == "#con1#" then
						if (password ~= nil) and (password == reconData[2]) then
							clientList[clientList.number + 1] = server:connect(reconData[3], 3, 2)
							clientList.number = clientList.number + 1
							clientList["#" ..clientList.number.. "#"] = clientList[clientList.number + 1]
							clientList[clientList.number + 1] = nil
							clientList["#" ..clientList.number.. "#"]:send(Serialize({"#con2#", ("#" ..clientList.number.. "#")}), 1, "reliable")
						else
							clientList[clientList.number + 1] = server:connect(reconData[3], 3, 3)
							clientList[clientList.number + 1]:disconnect_now(3)
						end
					elseif reconData[1] == "#inp1#" then
						
					end
				end
			end
			server:service()
		end
		
		
		
		
	elseif networkingStage == 2 then
		if networkingMode == "c" or networkingMode == "b" then
			local newEvent = client:check_events()
			if newEvent ~= nil and newEvent.type == "connect" and newEvent.data == 2 then
				newEvent = client:check_events()
				if newEvent ~= nil and newEvent.type == "receive" then
					local reconData = loadstring(newEvent.data)
					if reconData[1] == "#con2#" then
						clientID = reconData[2]
					end
				end
			elseif newEvent ~= nil and newEvent.data == 3 then
				client:check_events()
				if newEvent ~= nil and newEvent.data == 3 and newEvent.type == "disconnect" then
					networkingStage = 1
					confail = "wrongpwd"
					cserver:disconnect_now()
					newEvent = client:check_events()
					if newEvent ~= nil and newEvent.type == "disconnect" then
						newEvent = nil
					end
				end
			end
		end
	end
end


This is my Error:

Code: Select all

Error

main.lua:44: Failed to create peer


Traceback

[C]: in function 'connect'
main.lua:44: in function 'update'
[C]: in function 'xpcall'
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: Lua-Enet cant create peer

Post by AdrianN »

Hi, sorry I don't have your answer but ... why you don't use sock.lua library.
https://github.com/camchenry/sock.lua
Its most easy and you can do the same, and the documentation has some examples to use.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 237 guests