"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.0/examples/echo/echo.pony" (1 Nov 2019, 1369 Bytes) of package /linux/misc/ponyc-0.33.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Pony source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 use "net"
    2 
    3 actor Main
    4   new create(env: Env) =>
    5     try
    6       TCPListener(env.root as AmbientAuth, Listener(env.out))
    7     else
    8       env.out.print("unable to use the network")
    9     end
   10 
   11 class Listener is TCPListenNotify
   12   let _out: OutStream
   13   var _host: String = ""
   14   var _port: String = ""
   15 
   16   new iso create(out: OutStream) =>
   17     _out = out
   18 
   19   fun ref listening(listen: TCPListener ref) =>
   20     try
   21       (_host, _port) = listen.local_address().name()?
   22       _out.print("listening on " + _host + ":" + _port)
   23     else
   24       _out.print("couldn't get local address")
   25       listen.close()
   26     end
   27 
   28   fun ref not_listening(listen: TCPListener ref) =>
   29     _out.print("couldn't listen")
   30     listen.close()
   31 
   32   fun ref connected(listen: TCPListener ref): TCPConnectionNotify iso^ =>
   33     Server(_out)
   34 
   35 class Server is TCPConnectionNotify
   36   let _out: OutStream
   37 
   38   new iso create(out: OutStream) =>
   39     _out = out
   40 
   41   fun ref accepted(conn: TCPConnection ref) =>
   42     _out.print("connection accepted")
   43 
   44   fun ref received(conn: TCPConnection ref, data: Array[U8] iso,
   45     times: USize): Bool
   46   =>
   47     _out.print("data received, looping it back")
   48     conn.write("server says: ")
   49     conn.write(consume data)
   50     true
   51 
   52   fun ref closed(conn: TCPConnection ref) =>
   53     _out.print("server closed")
   54 
   55   fun ref connect_failed(conn: TCPConnection ref) =>
   56     _out.print("connect failed")