TCP sockets.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(ns foo.bar
  (:api dunaj)
  (:require [dunaj.concurrent.thread :refer [sleep]]
            [dunaj.resource.tcp :refer [tcp]]
            [dunaj.repl :refer [pst]]))

(with-io-scope
  (let [r (acquire! (tcp nil
                         :local-port 43214
                         :remote-port 43214
                         :reuse? true
                         :remote-address "localhost"))]
    (println! (config r))
    (write! r [-1 -2 -3 -4])
    (future
      (println! "monitor enter")
      (io!
       (try
         (dored [x (read! r)] (println! "received" x))
         (catch java.nio.channels.AsynchronousCloseException e
           (println! "monitor exit"))
         (catch java.lang.Exception e
           (println! "exception in monitor " (pst e))))))
    (sleep 300)))
;; {:uri #uri "tcp://0.0.0.0:43214@localhost:43214?reuse=true" :remote-address localhost :remote-port 43214 :local-address nil :local-port 43214 :batch-size nil :non-blocking? nil :keep-alive? false :in-buffer-size nil :out-buffer-size nil :linger nil :no-delay? false :reuse? true :selector-provider #<EPollSelectorProvider sun.nio.ch.EPollSelectorProvider@60efaefc>}
;; monitor enter
;; received -1
;; received -2
;; received -3
;; received -4
;; monitor exit
;;=> nil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns foo.bar
  (:api dunaj)
  (:require [dunaj.concurrent.thread :refer [sleep]]
            [dunaj.resource :refer [grab-scope release-scope!]]
            [dunaj.resource.tcp :refer [tcp tcp-server accept!]]
            [dunaj.repl :refer [pst]]))

(with-io-scope
    (let [s (acquire! (tcp-server nil
                                  :local-port 43215
                                  :reuse? true
                                  :local-address "localhost"))]
      (future
        (println! "monitor enter")
        (io!
         (try
           (while true
             (let [sc (accept! s)]
               (println! "connection accepted")
               (dored [x (read! sc)] (println! "received" x))
               (println! "connection end")))
           (catch java.nio.channels.AsynchronousCloseException e
             (println! "monitor exit"))
           (catch java.lang.Exception e
             (println! "exception in monitor") (pst e)))))

      (let [[c s]
            (->> (resource "tcp://localhost:43215")
                 acquire!
                 grab-scope)]
        (write! c [-1 -2 -3 -4])
        (write! c [-5 -6 -7])
        (release-scope! s))

      (let [[c s]
            (->> (resource "tcp://localhost:43215")
                 acquire!
                 grab-scope)]
        (write! c [0 1 2 4 5])
        (release-scope! s))

      (sleep 300)))
;; monitor enter
;; connection accepted
;; received -1
;; received -2
;; received -3
;; received -4
;; received -5
;; received -6
;; received -7
;; connection end
;; connection accepted
;; received 0
;; received 1
;; received 2
;; received 4
;; received 5
;; connection end
;; monitor exit
;;=> nil

accept!

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (accept! server)

Type signature:
  • (IServer) → nil

Returns incoming connection as TCP resource. Blocks or returns nil if there are no pending connections. Supports non-blockimg mode.

finish-connect!

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (finish-connect! res)

Type signature:
  • (TcpResource) → nil

Returns true if connection has been established, otherwise returns false. Must be called on client resource in non-blocking mode that has signalled that it is ready to finish the connection process.

See also: tcp, tcp-factory

tcp

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (tcp uri & {:as opts})

Type signature:
  • ((U nil String+ Uri) ⨯ Any) → IAcquirableFactory

Returns TCP resource factory with given uri and opts set.

See also: tcp-factory, tcp-server

tcp-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type IAcquirableFactory

TCP resource factory. Current options are:

  • :uri - resource uri

  • :remote-address - string, remote address

  • :remote-port - integer, remote port

  • :local-address - string, local address

  • :local-port - integer, local port

  • :batch-size - integer, batch size, low level

  • :non-blocking? - boolean, default false - open resource in non-blocking mode?

  • :keep-alive? - boolean, default false, TCP keepalive

  • :in-buffer-size - integer, input buffer size

  • :out-buffer-size - integer, output buffer size

  • :linger - integer, linger

  • :no-delay? - boolean, default false, TCP no delay

  • :reuse? - boolean, reuse port?

  • :selector-provider - nil (default) or host specific selector provider

See also: tcp, tcp-server-factory

tcp-server

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (tcp-server uri & {:as opts})

Type signature:
  • ((U nil String+ Uri) ⨯ Any) → IAcquirableFactory

Returns TCP server resource factory with given uri and opts set.

tcp-server-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type IAcquirableFactory

TCP server resource factory. Current options are:

  • :uri - resource uri

  • :local-address - string, local address

  • :local-port - integer, local port

  • :reuse? - boolean, reuse port?

  • :in-buffer-size - integer, input buffer size

  • :batch-size - integer, batch size, low level

  • :selector-provider - nil (default) or host specific selector provider

  • :non-blocking? - boolean, default false - open resource in non-blocking mode?