I'm trying to create a custom game server protocol in Ruby but i'm failing to understand how i can/should do a couple things:
Q->1#
Server sends an array to client using TCPServer/TCPSocket. but i can't use JSON as the transfer needs to be binary.. How can i convert a ruby array into binary which can then be transformed back into an array in client side?
This is my server:
#!/usr/bin/env ruby
require 'socket'
server = TCPServer.new 5020
loop do
Thread.start(server.accept) { |c|
print "new Client connected"
a = ["Hello, there"]
payload_with_header = CustomProtocolModule.constructMethod(a) # what method do i use to convert a (array -> binary representation? don't really understand .unpack, .pack, what template would i use if i did use String#Unpack? will String#Unpack work for what i need?
client.write payload_with_header
client.close
}
end
(Above is my first question)
Q->2#
Next since i know i need to use some sort of termination to check the end of message or a way to determine if the client/server has received the full message could i set the first two bytes of the payload to contain the size of the message? How would i do this server-side & client-side? The receiving party would have to loop TCPSocket.recv ? until it receives the first two bytes (the header), then read up the size the first two bytes contains? Something like this is what i had in mind:
| Payload Header (2 bytes) | array (contains cmd, message etc) |
If someone could help guide me in the right direction and/or provide pseudo code that can help. It would be greatly appreciated. Specifically i'm interested in how to loop for header, then read the payload back into an array and constructing the payload header.
Thanks!