5 people like it.

tcpStream abstraction

Basic (but experimental) TCP stream wrappers to help make TCP more abstracted and cleaner. Giving the user just the impression of a 'stream' that one connects to and sends and receives. This helps promote composition. The first abstraction is 'stream', really just wrapping .NET stream functions so that read returns the read n bytes and write returns the stream, all functions take NetworkStream as the first parameters, followed by other parameters. Currently we don't care about timeouts, exceptions, or other nasty's.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
open System.Net
open System.Text 
open System.Net.Sockets

type stream               = NetworkStream
let curry g b n           = g(b,0,n) |> ignore; b
let read  n (s : stream)  = curry s.Read (Array.zeroCreate n) n,s
let write b (s : stream)  = curry s.Write b b.Length; s
let close (b,(s : stream))= s.Close(); b
let connect host port     = TcpClient(host,port).GetStream()

let response : byte[] = 
  connect "google.com" 80
  |> write "GET / HTTP/1.1\r\n\r\n"
  |> read 256
  |> close
namespace System
namespace System.Net
namespace System.Text
namespace System.Net.Sockets
type stream = NetworkStream

Full name: Script.stream
Multiple items
type NetworkStream =
  inherit Stream
  new : socket:Socket -> NetworkStream + 3 overloads
  member BeginRead : buffer:byte[] * offset:int * size:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginWrite : buffer:byte[] * offset:int * size:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanTimeout : bool
  member CanWrite : bool
  member Close : timeout:int -> unit
  member DataAvailable : bool
  member EndRead : asyncResult:IAsyncResult -> int
  ...

Full name: System.Net.Sockets.NetworkStream

--------------------
NetworkStream(socket: Socket) : unit
NetworkStream(socket: Socket, ownsSocket: bool) : unit
NetworkStream(socket: Socket, access: System.IO.FileAccess) : unit
NetworkStream(socket: Socket, access: System.IO.FileAccess, ownsSocket: bool) : unit
val curry : g:('a * int * 'b -> 'c) -> b:'a -> n:'b -> 'a

Full name: Script.curry
val g : ('a * int * 'b -> 'c)
val b : 'a
val n : 'b
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val read : n:int -> s:stream -> byte [] * stream

Full name: Script.read
val n : int
val s : stream
NetworkStream.Read(buffer: byte [], offset: int, size: int) : int
module Array

from Microsoft.FSharp.Collections
val zeroCreate : count:int -> 'T []

Full name: Microsoft.FSharp.Collections.Array.zeroCreate
val write : b:byte [] -> s:stream -> stream

Full name: Script.write
val b : byte []
NetworkStream.Write(buffer: byte [], offset: int, size: int) : unit
property System.Array.Length: int
val close : b:'a * s:stream -> 'a

Full name: Script.close
System.IO.Stream.Close() : unit
NetworkStream.Close(timeout: int) : unit
val connect : host:string -> port:int -> NetworkStream

Full name: Script.connect
val host : string
val port : int
Multiple items
type TcpClient =
  new : unit -> TcpClient + 3 overloads
  member Available : int
  member BeginConnect : host:string * port:int * requestCallback:AsyncCallback * state:obj -> IAsyncResult + 2 overloads
  member Client : Socket with get, set
  member Close : unit -> unit
  member Connect : remoteEP:IPEndPoint -> unit + 3 overloads
  member Connected : bool
  member EndConnect : asyncResult:IAsyncResult -> unit
  member ExclusiveAddressUse : bool with get, set
  member GetStream : unit -> NetworkStream
  ...

Full name: System.Net.Sockets.TcpClient

--------------------
TcpClient() : unit
TcpClient(localEP: IPEndPoint) : unit
TcpClient(family: AddressFamily) : unit
TcpClient(hostname: string, port: int) : unit
val response : byte []

Full name: Script.response
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte

More information

Link:http://fssnip.net/hg
Posted:11 years ago
Author:David Klein
Tags: streams , networking , tcp