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

net/channel.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 net.channel
#
# -----------------------------------------------------------------------


# a channel encapsulates an open network resource
# that can be read from and written to.
# channels are usually installed by a successfully connected client
# or a by a newly accepted server connection.
#
# Note that even though UDP is a connection-less protocol its
# usage in fuzion does not differ from TCP except for the following
# caveats.
# 1) Reading a Datagram with a buffer not large enough to hold
# all data will lead to the rest of the datagram being dropped.
# 2) writing to a channel installed by `server.accept` will result in an error.
# 3) reading from a channel installed by `client ...` will block indefinitely.
#
module:public channel(desc i32) : effect
is


  # get the peer's ip address (tcp)
  public get_peer_address outcome (Sequence u8) =>
    fuzion.sys.net.get_peer_address desc


  # get the peer's port (tcp)
  public get_peer_port outcome u16 =>
    fzE_get_peer_port desc



# helper feature to create a read handler
# for a given descriptor
#
module read_handler(desc i32) : io.Read_Handler is
  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



# helper feature to create a read handler
# for a given descriptor
#
module write_handler(desc i32) : io.Write_Handler is
  redef write(data Sequence u8) outcome unit =>
    fuzion.sys.net.write desc data.as_array
last changed: 2025-01-23