Help! I suck at networking!
Posted: Wed Jun 11, 2014 1:10 pm
So I started a project the other day to make a networked game. Today I got a dumb terminal system set up, in which the client sends the server packets containing the user inputs, the server takes the inputs and moves the player, and then sends the new player position to the client to be drawn. It works, sort of.
When I focus on the server, move windows around, or open more clients, the ping skyrockets, giving the game a delay of 5-10 seconds, or losing connection all together. I feel like I'm using enet wrong; I never understood it in the first place.
Anyway, I'm hoping you guys can show me what I'm doing wrong, I'm completely stumped. Also, if you've got any tips as to how I could make my code better, please tell me! Thanks!
P.S. - stc is just a serialization (that's what it's called, right?) thing I wrote because I didn't understand how to use others.
Server
Client
When I focus on the server, move windows around, or open more clients, the ping skyrockets, giving the game a delay of 5-10 seconds, or losing connection all together. I feel like I'm using enet wrong; I never understood it in the first place.
Anyway, I'm hoping you guys can show me what I'm doing wrong, I'm completely stumped. Also, if you've got any tips as to how I could make my code better, please tell me! Thanks!
P.S. - stc is just a serialization (that's what it's called, right?) thing I wrote because I didn't understand how to use others.
Server
Code: Select all
require "enet"
require "stc"
local UUID = require "UUID"
local host = enet.host_create("*:12345")
local event = host:service()
local tableReceive
local playerTable = {}
local userTable = {}
local dataSendTimer = .05
local test1 = false
local test2 = false
local stringTest = ""
local stringTest2 = ""
function connect(event)
if event and event.type == "receive" then
if event.data:match("^(%S*)") == "startup" then
local currentUser = table.getn(userTable) + 1
local currentPlayer = table.getn(playerTable) + 1
userTable[currentUser] = {}
userTable[currentUser].id = UUID()
userTable[currentUser].player = currentPlayer
userTable[currentUser].w = false
userTable[currentUser].s = false
userTable[currentUser].a = false
userTable[currentUser].d = false
playerTable[currentPlayer] = {}
playerTable[currentPlayer][1] = 100
playerTable[currentPlayer][2] = 100
playerTable[currentPlayer][3] = userTable[currentUser].id
playerTable[currentPlayer][4] = currentUser
userTable[currentUser].peer = event.peer
userTable[currentUser].peer:send("started" .. " " .. userTable[currentUser].id)
end
end
end
function takeInputs(event)
if event and event.type == "receive" then
if event.data:match("^(%S*)") == "inputsend" then
--matches ID of user with userTable
local idCheck = event.data:match("^%S* (%S*)")
stringTest2 = idCheck
test1 = true
for i = 1, table.getn(userTable) do
if userTable[i].id == idCheck then
--messy, but how else would you do this? lua regex sucks
--uses local operators to derive booleans from strings
userTable[i].w = ((event.data:match("^%S* %S* (%S*)")) == "true")
userTable[i].s = ((event.data:match("^%S* %S* %S* (%S*)")) == "true")
userTable[i].a = ((event.data:match("^%S* %S* %S* %S* (%S*)")) == "true")
userTable[i].d = ((event.data:match("^%S* %S* %S* %S* %S* (%S*)")) == "true")
stringTest = (event.data:match("^%S* %S* (%S*)"))
end
end
end
end
end
function sendData()
for i = 1, table.getn(userTable) do
userTable[i].peer:send("playertable" .. " " .. stcstring(playerTable))
end
end
function updatePlayers(dt)
for i = 1, table.getn(playerTable) do
local self = playerTable[i]
if userTable[self[4]].w == true then
self[2] = self[2] - (50 * dt)
end
if userTable[self[4]].s == true then
self[2] = self[2] + (50 * dt)
end
if userTable[self[4]].a == true then
self[1] = self[1] - (50 * dt)
end
if userTable[self[4]].d == true then
self[1] = self[1] + (50 * dt)
end
end
end
function love.update(dt)
local event = host:service()
connect(event)
takeInputs(event)
updatePlayers(dt)
dataSendTimer = dataSendTimer - dt
if dataSendTimer <= 0 then
sendData()
dataSendTimer = .05
end
--[[if event and event.type == "receive" then
tableReceive = stctable(event.data)
for i = 1, table.getn(tableReceive) do
for j = 1, table.getn(tableReceive[i]) do
tableReceive[i][j] = tableReceive[i][j] * 2
end
end
event.peer:send(stcstring(tableReceive))
end
--host:service()]]
end
function love.draw()
if userTable[1] then
love.graphics.print(tostring(userTable[1].w), 10, 10)
love.graphics.print(tostring(userTable[1].id), 10, 90)
end
love.graphics.print(tostring(stringTest), 10, 30)
love.graphics.print(tostring(test1), 10, 50)
love.graphics.print(tostring(stringTest2), 10, 70)
end
Code: Select all
require "enet"
require "stc"
local UUID = require "UUID"
local playerTable = {nil}
local selfPeer = false
local connected = false
local started = false
local sendTimer = 0
local id = nil
local test1 = false
local stringTest = ""
local sentCount = 0
local host = enet.host_create()
local server = host:connect("localhost:12345")
function connect(event)
if event and event.type == "connect" then
selfPeer = event.peer
selfPeer:send("startup")
connected = true
sendTimer = .05
test1 = true
end
end
function dataReceive(event)
if event and event.type == "receive" then
local cmd = event.data:match("^(%S*)")
if cmd == "playertable" then
local tableData = event.data:sub(13)
playerTable = stctable(tableData)
end
if cmd == "started" then
started = true
id = event.data:match("^%S* (%S*)")
end
end
end
function movementSend()
local wIn = love.keyboard.isDown("w")
local sIn = love.keyboard.isDown("s")
local aIn = love.keyboard.isDown("a")
local dIn = love.keyboard.isDown("d")
selfPeer:send("inputsend " .. tostring(id) .. " " .. tostring(wIn) .. " " .. tostring(sIn) .. " " .. tostring(aIn) .. " " .. tostring(dIn))
stringTest = "inputsend " .. tostring(id) .. " " .. tostring(wIn) .. " " .. tostring(sIn) .. " " .. tostring(aIn) .. " " .. tostring(dIn)
sentCount = sentCount + 1
end
function love.update(dt)
sendTimer = sendTimer - dt
local event = host:service()
if connected then
dataReceive(event)
if sendTimer <= 0 then
movementSend()
end
else
connect(event)
end
end
function love.draw()
love.graphics.print(tostring(connected), 10, 10)
love.graphics.print(tostring(started), 10, 30)
love.graphics.print(tostring(id), 10, 50)
love.graphics.print(tostring(test1), 10, 70)
love.graphics.print(tostring(stringTest), 10, 90)
if selfPeer then
love.graphics.print(tostring(selfPeer:round_trip_time()), 10, 110)
end
love.graphics.print(tostring(sentCount), 10, 130)
if playerTable[1] then
for i = 1, table.getn(playerTable) do
love.graphics.rectangle("fill", playerTable[i][1], playerTable[i][2], 32, 32)
end
end
end