Skip to content

Commit

Permalink
rename castle-related API, allow disabling compression
Browse files Browse the repository at this point in the history
  • Loading branch information
nikki93 committed Dec 10, 2018
1 parent 2f18c6b commit 2338f3e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
22 changes: 18 additions & 4 deletions cs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ do
local idToPeer = {}
local nextId = 1

function server.useCastleServer()
function server.useCastleConfig()
if castle then
function castle.startServer(port)
server.enabled = true
Expand All @@ -32,12 +32,19 @@ do
end
end

local useCompression = true
function server.disableCompression()
useCompression = false
end

function server.start(port)
host = enet.host_create('*:' .. tostring(port or '22122'))
if host == nil then
error("couldn't start server -- is port in use?")
end
host:compress_with_range_coder()
if useCompression then
host:compress_with_range_coder()
end
server.started = true
end

Expand Down Expand Up @@ -169,7 +176,7 @@ do
local host
local peer

function client.useCastleServer()
function client.useCastleConfig()
if castle then
function castle.startClient(address)
client.enabled = true
Expand All @@ -178,9 +185,16 @@ do
end
end

local useCompression = true
function client.disableCompression()
useCompression = false
end

function client.start(address)
host = enet.host_create()
host:compress_with_range_coder()
if useCompression then
host:compress_with_range_coder()
end
host:connect(address or '127.0.0.1:22122')
end

Expand Down
6 changes: 3 additions & 3 deletions example_client.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local cs = require 'cs'
local client = cs.client

if USE_LOCAL_SERVER then
if USE_CASTLE_CONFIG then
client.useCastleConfig()
else
client.enabled = true
client.start('127.0.0.1:22122') -- IP address ('127.0.0.1' is same computer) and port of server
else
client.useCastleServer()
end


Expand Down
2 changes: 1 addition & 1 deletion example_combined.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- This runs both the client and server in the same process for easy testing

USE_LOCAL_SERVER = false
USE_CASTLE_CONFIG = true
require 'example_server'
require 'example_client'

Expand Down
6 changes: 3 additions & 3 deletions example_server.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local cs = require 'cs'
local server = cs.server

if USE_LOCAL_SERVER then
if USE_CASTLE_CONFIG then
server.useCastleConfig()
else
server.enabled = true
server.start('22122') -- Port of server
else
server.useCastleServer()
end


Expand Down

0 comments on commit 2338f3e

Please sign in to comment.