--
--	Inmos Libraries
--	Copyright (C) 1988, 1989, 1990, 1991, 1993  Inmos Limited
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation; either
--	version 2 of the License, or (at your option) any later version.
--
--	This library 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
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library; if not, write to the Free Software
--	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
--

--** @module hostsp

#COMMENT "Host i/o library, V1.1, 14/05/90"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC hostsp version 1996"
#INCLUDE "hostio.inc"
--{{{  sp.write
--* Write a limited number of bytes to a stream.
--
-- @param fs from server
-- @param ts to server
-- @param streamid the identification number of the open stream to
--   write to
-- @param data a string of bytes ALL of which are to be written to the
--   stream
-- @param length.written this gives the number of bytes written. If
--   length.written <> (SIZE data) then an error occurred. 0 <=
--   length.written <= sp.max.writebuffer.size
-- @param result if not equal to spr.bad.packet.size meaning that too
--   many bytes were requested, ie (SIZE data) >
--   sp.max.writebuffer.size, then it is equal to spr.ok
PROC sp.write (CHAN OF SP fs, ts, VAL INT32 streamid, VAL []BYTE data,
               INT length.written, BYTE result)
  IF
    (SIZE data) > sp.max.writebuffer.size
      SEQ
        result := spr.bad.packet.size
        length.written := 0
    TRUE
      [sp.max.packet.data.size]BYTE buffer :
      INT16 temp.INT16 :
      SEQ
        --{{{  pack the data into the buffer (7 bytes plus data)
        buffer [0] := sp.write.tag
        
        VAL [4]BYTE b.streamid RETYPES streamid :
        [buffer FROM 1 FOR 4] := b.streamid
        
        temp.INT16 := INT16 (SIZE data)
        VAL [2]BYTE b.temp RETYPES temp.INT16 :
        [buffer FROM 5 FOR 2] := b.temp
        
        [buffer FROM 7 FOR SIZE data] := data
        --}}}pack the data into the buffer (7 bytes plus data)
        --{{{  send and receive the buffer
        INT len :
        SEQ
          -- Size of buffer is (SIZE data) + 7.
          -- Add 1, then mask off bottom bit, to ensure it is even
          len := ((SIZE data) + 8) /\ ((-1) << 1)
          IF
            len < sp.min.packet.data.size
              temp.INT16 := INT16 sp.min.packet.data.size
            TRUE
              temp.INT16 := INT16 len
        
        ts ! temp.INT16 :: buffer
        fs ? temp.INT16 :: buffer
        --}}}send and receive the buffer
        --{{{  unpack the data               (3 bytes)
        result := buffer [0]  -- always 0 for sp.write.tag
        
        [2]BYTE b.temp RETYPES temp.INT16 :
        b.temp := [buffer FROM 1 FOR 2]
        length.written := INT temp.INT16
        --}}}unpack the data               (3 bytes)
:
--}}}sp.write
