From 880e85f12db0f85d201efd1a22c0891ce0997a09 Mon Sep 17 00:00:00 2001 From: mewrrythekibby Date: Sat, 31 Jan 2026 00:37:01 -0600 Subject: [PATCH] working prototype --- client.rb | 36 +++++++++++++++++++++++++ server.rb | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 client.rb create mode 100644 server.rb diff --git a/client.rb b/client.rb new file mode 100644 index 0000000..4ab09bd --- /dev/null +++ b/client.rb @@ -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 diff --git a/server.rb b/server.rb new file mode 100644 index 0000000..2411a42 --- /dev/null +++ b/server.rb @@ -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