working prototype
This commit is contained in:
commit
880e85f12d
2 changed files with 115 additions and 0 deletions
36
client.rb
Normal file
36
client.rb
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'socket'
|
||||
require 'io/console'
|
||||
|
||||
HOST = 'localhost'
|
||||
PORT = 6789
|
||||
VERSION = 0.01
|
||||
|
||||
socket = TCPSocket.new(HOST, PORT)
|
||||
|
||||
puts "PORT: #{PORT}"
|
||||
puts "HOST: #{HOST}"
|
||||
|
||||
#reader = Thread.new do
|
||||
Thread.new do
|
||||
loop do
|
||||
msg = socket.gets
|
||||
exit 0 unless msg
|
||||
ver, msg = msg.split('|',2)
|
||||
next unless ver.to_f <= VERSION
|
||||
next unless msg != nil
|
||||
# In message version 0.01 and lower,
|
||||
# all messages are in an assumed format...
|
||||
row,col,chars = msg.split('|',3)
|
||||
print "\x1b[#{row};#{col}H#{chars}\x1b[A"
|
||||
end
|
||||
end
|
||||
|
||||
while (char = STDIN.noecho(&:getch))
|
||||
exit unless char != "\x03" # ^C
|
||||
exit unless char != "\x04" # ^D
|
||||
next unless char.ord >= 0x20 # Non printing
|
||||
next unless char.ord != 0x7F # DEL (Non print)
|
||||
socket.puts "#{VERSION}|#{char}"
|
||||
end
|
||||
79
server.rb
Normal file
79
server.rb
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'socket'
|
||||
|
||||
PORT = 6789
|
||||
ROWS = 24
|
||||
COLS = 40
|
||||
VERSION = 0.01
|
||||
|
||||
server = TCPServer.new(PORT)
|
||||
sockets = [server]
|
||||
|
||||
puts "PORT: #{PORT}"
|
||||
puts 'Listening...'
|
||||
|
||||
buffer = ' '*ROWS*COLS
|
||||
cont = 0
|
||||
|
||||
def new_buf_cont_out(buf, count, newtext)
|
||||
buf = buf.dup
|
||||
out = ''
|
||||
nti = 0 # New Text Index
|
||||
while nti < newtext.length
|
||||
out << "#{VERSION}|"
|
||||
out << ((count/COLS)+1).to_s + '|'
|
||||
out << ((count%COLS)+1).to_s + '|'
|
||||
loop do
|
||||
out << newtext[nti]
|
||||
buf[count] = newtext[nti]
|
||||
count += 1
|
||||
nti += 1
|
||||
break if nti >= newtext.length
|
||||
break if count%COLS == 0
|
||||
end
|
||||
count = 0 if count >= (ROWS*COLS)
|
||||
out << "\n"
|
||||
end
|
||||
return [buf, count, out]
|
||||
end
|
||||
|
||||
loop do
|
||||
ready, = IO.select(sockets)
|
||||
newtext = ''
|
||||
|
||||
ready.each do |sck|
|
||||
if sck == server
|
||||
client = server.accept
|
||||
sockets << client
|
||||
puts "Connection: #{client.peeraddr}"
|
||||
client.puts new_buf_cont_out(buffer,0,buffer)[2]
|
||||
client.puts new_buf_cont_out(buffer,cont-1,'')[2]
|
||||
next
|
||||
end
|
||||
begin
|
||||
|
||||
msg = sck.gets
|
||||
unless msg
|
||||
puts "Disconnect: #{sck.peeraddr}"
|
||||
sck.close
|
||||
sockets.delete(sck)
|
||||
next
|
||||
end
|
||||
ver, msg = msg.chomp.split('|',2)
|
||||
next unless ver.to_f <= VERSION
|
||||
next unless msg != nil
|
||||
newtext << msg
|
||||
rescue Errno::ECONNRESET, Errno::EPIPE
|
||||
puts "Disconnect (err): #{sck.peeraddr}"
|
||||
sck.close
|
||||
sockets.delete(sck)
|
||||
end
|
||||
buffer, cont, out = new_buf_cont_out(buffer,cont,newtext)
|
||||
clients = sockets - [server]
|
||||
clients.each { |c| c.puts out }
|
||||
#if out != ''
|
||||
# puts "#{cont}: (#{(cont/COLS)+1},#{(cont%COLS)+1})"
|
||||
#end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue