Skip to content

Commit

Permalink
Add locking optimisations in server code
Browse files Browse the repository at this point in the history
Ref #4
  • Loading branch information
bojidar-bg committed Oct 5, 2015
1 parent b1d9426 commit 604cd68
Showing 1 changed file with 63 additions and 32 deletions.
95 changes: 63 additions & 32 deletions lib/tcp/server.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ extends Reference

const ReadWriteLock = preload("../ReadWriteLock.gd")

class RawMessage:
var target = null
var data = {}

var tcp_server
var server_running
var global_message_queue = []
var M_server_running = Mutex.new()
var M_tcp_server = Mutex.new()

var connections = []
var RW_connections = ReadWriteLock.new()

var message_queue = []
var M_message_queue = Mutex.new()

var loop_thread = Thread.new()

func _init():
Expand All @@ -28,62 +35,86 @@ func start(port):

M_tcp_server.unlock()

func stop():
M_server_running.lock()

server_running = false

M_server_running.unlock()

func send_to(id, data):
var message = RawMessage.new()
message.target = id
message.data = data

M_message_queue.lock()

message_queue.push_back(message)

M_message_queue.unlock()

func send_to_all(data):
var message = RawMessage.new()
message.data = data

M_message_queue.lock()

message_queue.push_back(message)

M_message_queue.unlock()

func loop(data):
while true:
M_tcp_server.lock()

M_server_running.lock()
if not server_running:
M_server_running.unlock()

tcp_server.stop()
M_tcp_server.unlock()
break;
else:
M_server_running.unlock()

while tcp_server.is_connection_available():
var connection = tcp_server.take_connection()
var packet_peer = PacketPeerStream.new()

RW_connections.lock_write()

packet_peer.set_stream_peer(connection)
connections.push_back({
id = connections.size(),
connection = connection,
packet_peer = packet_peer,
message_queue = []
packet_peer = packet_peer
})
emit_signal("connect", connections.size() - 1)

RW_connections.unlock_write()

RW_connections.lock_read()

for connection in connections:
while(connection.packet_peer.get_available_packet_count()):
emit_signal("message", connection.id, connection.packet_peer.get_var())

for message in connection.message_queue:
connection.packet_peer.put_var(message)

for message in global_message_queue:
connection.packet_peer.put_var(message)

connection.message_queue.clear()
M_message_queue.lock()

global_message_queue.clear()
for message in message_queue:
if message.target != null:
connections[message.target].packet_peer.put_var(message.data)
else:
for connection in connections:
connection.packet_peer.put_var(message.data)

message_queue.clear()

M_message_queue.unlock()

RW_connections.unlock_read()

M_tcp_server.unlock()
OS.delay_msec(100)

func stop():
M_tcp_server.lock()

server_running = false

M_tcp_server.unlock()

func send_to(id, message):
M_tcp_server.lock()

connections[id].message_queue.push_back(message)

M_tcp_server.unlock()

func send_to_all(message):
M_tcp_server.lock()

global_message_queue.push_back(message)

M_tcp_server.unlock()


0 comments on commit 604cd68

Please sign in to comment.