Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

net/connection.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature connection
#
# -----------------------------------------------------------------------

module:public connection(desc i32) is

  # close connection manually.
  # note that connections are closed automatically
  # when effect is uninstalled, i.e. on abort or when thread is finished.
  #
  public close outcome unit =>
    fuzion.sys.net.close desc


  # this installs reader, writer and channel for the given connection
  #
  public with(T type, LM type : mutate, fn ()->T) T
  =>

    reader =>
      # helper feature to create a read handler
      # for a given descriptor
      #
      read_handler : io.Read_Handler is
        public redef read(count i32) choice (array u8) io.end_of_file error =>
          match fuzion.sys.net.read desc count.as_i64
            a array => a
            e error => e
      (io.buffered LM)
        .reader read_handler 1024

    writer =>

      # helper feature to create a read handler
      # for a given descriptor
      #
      write_handler : io.Write_Handler is
        public redef write(data Sequence u8) outcome unit =>
          fuzion.sys.net.write desc data.as_array

      (io.buffered LM)
        .writer write_handler 1024

    channel_ =>
      net
        .channel desc

    reader
      .and T _ writer
      .and channel_
      .call fn




  # this installs reader, writer and channel for the given connection
  #
  # NYI: DOCUMENTATION: see tests/sockets_thread_pool for now
  #
  public in_thread_pool(T type, TP type : concur.thread_pool, LM type : mutate, lm LM, fn ()->T) concur.Future (outcome T)
  =>
    TP.env.submit (outcome T) ()->
      lm ! ()->
        with T LM fn

last changed: 2025-07-10