PreviousUpNext

15.4.1209  src/lib/std/src/socket/proto-socket–premicrothread.pkg

## proto-socket--premicrothread.pkg
#
# These are some common type definitions used in the sockets library.

# Compiled by:
#     src/lib/std/src/standard-core.sublib




stipulate
    package hu  =  host_unt_guts;                                       # host_unt_guts                         is from   src/lib/std/src/bind-sysword-32.pkg
    package u1b =  one_byte_unt_guts;                                   # one_byte_unt_guts                     is from   src/lib/std/src/one-byte-unt-guts.pkg
    package unt =  unt_guts;                                            # unt_guts                              is from   src/lib/std/src/bind-unt-guts.pkg
    package ci  =  mythryl_callable_c_library_interface;                # mythryl_callable_c_library_interface  is from   src/lib/std/src/unsafe/mythryl-callable-c-library-interface.pkg
    package v1u =  vector_of_one_byte_unts;                             # vector_of_one_byte_unts               is from   src/lib/std/src/vector-of-one-byte-unts.pkg
herein

    package proto_socket__premicrothread {
        #
        Internet_Address   =  v1u::Vector;                              # The raw representation address data.

        Raw_Address_Family =  ci::System_Constant;                      # The raw representation of an address family.

        # The raw representation of a socket:
        #   a file descriptor
        #
        Socket_Fd = Int;

        # Sockets are typeagnostic. The instantiation of the type variables
        # provides a way to distinguish between different kinds of sockets.
        #
        Socket(A_sock, A_af)
            =
            Socket_Fd;

        Socket_Address(A_af)
            =
            ADDRESS  Internet_Address;


        # Witness types for the socket parameter:
        #
        Datagram  = DATAGRAM;
        Stream(X) = STREAM;
        Passive   = PASSIVE;
        Active    = ACTIVE;

        package af {
            #
            Address_Family =  ADDRESS_FAMILY Raw_Address_Family;
        };

        package typ {                                                   # Socket types:
            #
            Socket_Type =  SOCKET_TYPE  ci::System_Constant;
        };

        Shutdown_Mode =  NO_RECVS
                      |  NO_SENDS
                      |  NO_RECVS_OR_SENDS
                      ;

        Socket_Descriptor =  winix_types::io::Iod;

        # Socket I/O option types:
        #
        Out_Flags = { oob: Bool,   don't_route: Bool };
        In_Flags  = { oob: Bool,   peek:        Bool };

        # Utility functions for parsing/unparsing network addresses:
        #
        stipulate

            package sys_w =  hu;                        # host_unt              is from   src/lib/std/types-only/bind-largest32.pkg
            package scvt  =  number_string;             # number_string         is from   src/lib/std/src/number-string.pkg

            fun to_w (getc, stream)
                =
                {   fun scan  radix  stream
                        =
                        case (sys_w::scan  radix  getc  stream)
                            #
                            THE (w, stream) =>  THE (w, stream);
                            NULL            =>  NULL;
                        esac;

                    case (getc stream)
                        #                     
                        NULL => NULL;
                        #                     
                        THE ('0', stream')
                            =>
                            (case (getc stream')

                                  NULL => THE (0u0, stream');

                                  THE(('x' | 'X'), stream'')
                                      =>
                                      scan scvt::HEX stream'';

                                  _ =>  scan scvt::OCTAL stream;
                             esac
                            );

                        _ => scan scvt::DECIMAL stream;
                    esac;
                };

            # Check that the word is representable
            # in the given number of bits;
            # raise OVERFLOW if not.
            #
            fun check (w, bits)
                =
                if (sys_w::(>=) (sys_w::(>>) (0uxffffffff, unt::(-) (0u32, bits)), w))
                     w;
                else raise exception exceptions_guts::OVERFLOW;                                 # exceptions_guts       is from   src/lib/std/src/exceptions-guts.pkg
                fi;

            # Scan a sequence of numbers separated by '.' 
            #
            fun scan getc stream
                =
                (case (to_w (getc, stream))

                      NULL => NULL;

                      THE (w, stream') => scan_rest getc ([w], stream');
                 esac
                )

            also
            fun scan_rest getc (l, stream)
                =
                case (getc stream)

                      THE ('.', stream')
                          =>
                          (case (to_w (getc, stream'))

                                NULL            =>  THE (list::reverse l, stream);

                                THE (w, stream'') =>  scan_rest getc (w ! l,  stream'');
                           esac
                        );

                      _ => THE (list::reverse l, stream);
                esac;

        herein

            fun to_unts getc stream
                =
                case (scan getc stream)
                    #              
                    THE ([a, b, c, d], stream)
                        => 
                        THE ([check (a, 0u8), check (b, 0u8), check (c, 0u8), check (d, 0u8)], stream);

                    THE ([a, b, c], stream)
                        =>
                        THE ([check (a, 0u8), check (b, 0u8), check (c, 0u16)], stream);

                    THE ([a, b], stream)
                        =>
                        THE ([check (a, 0u8), check (b, 0u24)], stream);

                    THE ([a], stream)
                        =>
                        THE ([check (a, 0u32)], stream);

                     _ => NULL;
                esac;

            fun from_bytes (a, b, c, d)
                =
                {   format =  u1b::format number_string::DECIMAL;

                    cat [ format a, ".",
                          format b, ".",
                          format c, ".",
                          format d
                        ];
                };

        end;    # stipulate

        fun to_string file_descriptor
            #                                                           # NB: sfprintf package is not defined at this level of the library.
            =  "(socket) file_descriptor => "
            +  (number_format::format_int  number_string::DECIMAL  (one_word_int_guts::from_int file_descriptor))
            ;

    };          # proto_socket__premicrothread 
end;            # stipulate



## COPYRIGHT (c) 1995 AT&T Bell Laboratories.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext