(*
    Title: 	Things to chain together
    LastEdit:	"Wed Jan 27 14:11:19 1988"
    Author: 	Derek McAuley,Oms1,4663,329629
		Cambridge University Computer Laboratory
*)

DEFINITION MODULE PktBufs;

IMPORT
  Endian;
  
FROM RPCBasics IMPORT 
  Location, ServiceId, ThreadHand, CallEnd, CallId, ConverseId, ProtocolType, 
  SequenceNumber, PacketType, PacketRate, Instance;

FROM SYSTEM IMPORT
  WORD, BYTESPERWORD, MAXCARD;


EXPORT QUALIFIED
  Size, ByteSize, 
  
  ProtocolHdr, PackedHdr, T, PktBufR, ReceiveProc,

  recSize, recBSize, headSize, headBSize,

  New, Newish, Extra, Discard, Copy, CopyHeader, FetchDataWORD,
  PackHeader, UnPackHeader;


CONST
  Size     = 320; 
  ByteSize = Size * BYTESPERWORD;


TYPE
  ProtocolHdr = 
    RECORD
      packet:   PacketType;    (*  4 bits *)
      protocol: ProtocolType;  (*  3 bits *)
      endian:   Endian.Sex;    (*  1 bits *)
      pktrate:  PacketRate;    (*  8 bits *)
      seqNumber:SequenceNumber;(*  8 bits *)
      maxNumber:SequenceNumber (*  8 bits *)
    END;

  PackedHdr = WORD;

  T  = POINTER TO PktBufR;
  PktBufR = 
    RECORD
      next:       T;
      size:       CARDINAL;
      i:          Instance;
      protocolHdr:ProtocolHdr; (* Check PktHeadSize *)
      gubbins:    ARRAY [0..7] OF WORD;

      location:   ARRAY CallEnd OF Location; (* This is the transmitted bit *)
      serviceId:  ServiceId;
      thread:     ThreadHand;
      callId:     CallId;
      packedHdr:  PackedHdr;
      converseId: ConverseId;
      top:        CARDINAL;
      data:       ARRAY [0..(MAXCARD DIV 8)-1] OF WORD
    END;

    ReceiveProc = PROCEDURE(T):T;


VAR
  recSize,
  recBSize,
  headSize,
  headBSize:CARDINAL;
  

PROCEDURE New():T;

PROCEDURE Newish(wordsofdata:CARDINAL):T; (* Use with care *)

PROCEDURE Extra(p:T);
(*Chains another on the end*)

PROCEDURE Discard(VAR p:T (* InOut *));
(*Discards, with all followers*)

PROCEDURE Copy(p:T):T;
(*Copies an entire chain*)

PROCEDURE CopyHeader(from, to:T);
(*Copies RPC header *)

PROCEDURE FetchDataWORD(p:T; offset:CARDINAL):WORD;

PROCEDURE PackHeader(p:T);

PROCEDURE UnPackHeader(p:T);

END PktBufs.
