Skip to content

Commit

Permalink
Add lock optimisations to TCP client
Browse files Browse the repository at this point in the history
Close #4
  • Loading branch information
bojidar-bg committed Oct 7, 2015
1 parent 604cd68 commit f7ecf09
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions lib/tcp/client.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ const ReadWriteLock = preload("../ReadWriteLock.gd")

var tcp_stream = StreamPeerTCP.new()
var packet_peer = PacketPeerStream.new()
var client_running
var message_queue = []
var M_tcp_stream = Mutex.new()

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

var client_running
var M_client_running = Mutex.new()

var loop_thread = Thread.new()

func _init():
Expand All @@ -26,35 +30,45 @@ func connect_to(host, port):

M_tcp_stream.unlock()

func send(message):
M_message_queue.lock()

message_queue.push_back(message)

M_message_queue.unlock()

func stop():
M_client_running.lock()

client_running = false

M_client_running.unlock()

func loop(data):
while true:
M_tcp_stream.lock()
M_client_running.lock()
if not client_running:
M_client_running.unlock()

tcp_stream.disconnect()
M_tcp_stream.unlock()
break;
else:
M_client_running.unlock()

while packet_peer.get_available_packet_count():
emit_signal("message", packet_peer.get_var())

M_message_queue.lock()

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

M_message_queue.unlock()

M_tcp_stream.unlock()
OS.delay_msec(100)

func send(message):
M_tcp_stream.lock()

message_queue.push_back(message)

M_tcp_stream.unlock()

func stop():
M_tcp_stream.lock()

client_running = false

M_tcp_stream.unlock()


0 comments on commit f7ecf09

Please sign in to comment.