Sha256: a8ef00ec840dd00c39f9ec929303b7dd5d3b830a72756b40a19dd3e994b50cb6

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'socket'

module Backport
  module Server
    class Tcpip < Base
      include Connectable

      def initialize host: 'localhost', port: 1117, adapter: Adapter
        @socket = TCPServer.new(host, port)
        @adapter = adapter
      end

      def stopped?
        @stopped ||= false
      end

      def tick
        mutex.synchronize do
          clients.each do |client|
            input = client.read
            client.sending input unless input.nil?
          end
        end
      end

      def start
        super
        start_accept_thread
      end

      private

      attr_reader :socket

      def mutex
        @mutex ||= Mutex.new
      end

      def start_accept_thread
        Thread.new do
          until stopped?
            conn = socket.accept
            mutex.synchronize do
              clients.push Client.new(conn, conn, @adapter)
              clients.last.run
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
backport-0.1.0 lib/backport/server/tcpip.rb