|
69 | 69 | ([session]
|
70 | 70 | (wait session (System/currentTimeMillis)))
|
71 | 71 | ([session start-time]
|
72 |
| - (let [ms-until-next-keepalive (* (send-keepalive session) 1000) |
73 |
| - block-directions (libssh2-session/block-directions (:session session)) |
74 |
| - block-for-read (boolean (bit-and block-directions libssh2/SESSION_BLOCK_INBOUND)) |
75 |
| - block-for-write (boolean (bit-and block-directions libssh2/SESSION_BLOCK_OUTBOUND)) |
76 |
| - libssh2-timeout-ms (libssh2-session/get-timeout (:session session)) |
77 |
| - select-until (partial select session block-for-read block-for-write) |
78 |
| - select-result (cond (and (< 0 libssh2-timeout-ms) |
79 |
| - (or (= 0 ms-until-next-keepalive) |
80 |
| - (> ms-until-next-keepalive libssh2-timeout-ms))) |
81 |
| - (let [elapsed (- (System/currentTimeMillis) start-time)] |
82 |
| - (when (> elapsed libssh2-timeout-ms) |
83 |
| - (handle-errors session libssh2/ERROR_TIMEOUT)) |
84 |
| - (select-until (- libssh2-timeout-ms elapsed))) |
| 72 | + (when (and session (:session session) (> 0 (:socket session))) |
| 73 | + (let [ms-until-next-keepalive (* (send-keepalive session) 1000) |
| 74 | + block-directions (libssh2-session/block-directions (:session session)) |
| 75 | + block-for-read (boolean (bit-and block-directions libssh2/SESSION_BLOCK_INBOUND)) |
| 76 | + block-for-write (boolean (bit-and block-directions libssh2/SESSION_BLOCK_OUTBOUND)) |
| 77 | + libssh2-timeout-ms (libssh2-session/get-timeout (:session session)) |
| 78 | + select-until (partial select session block-for-read block-for-write) |
| 79 | + select-result (cond (and (< 0 libssh2-timeout-ms) |
| 80 | + (or (= 0 ms-until-next-keepalive) |
| 81 | + (> ms-until-next-keepalive libssh2-timeout-ms))) |
| 82 | + (let [elapsed (- (System/currentTimeMillis) start-time)] |
| 83 | + (when (> elapsed libssh2-timeout-ms) |
| 84 | + (handle-errors session libssh2/ERROR_TIMEOUT)) |
| 85 | + (select-until (- libssh2-timeout-ms elapsed))) |
85 | 86 |
|
86 |
| - (< 0 ms-until-next-keepalive) |
87 |
| - (select-until ms-until-next-keepalive) |
| 87 | + (< 0 ms-until-next-keepalive) |
| 88 | + (select-until ms-until-next-keepalive) |
88 | 89 |
|
89 |
| - :else |
90 |
| - (select-until 0))] |
91 |
| - (when (>= 0 select-result) |
92 |
| - (handle-errors session libssh2/ERROR_TIMEOUT))))) |
| 90 | + :else |
| 91 | + (select-until 0))] |
| 92 | + (when (>= 0 select-result) |
| 93 | + (handle-errors session libssh2/ERROR_TIMEOUT)))))) |
93 | 94 |
|
94 | 95 | (defmacro block
|
95 | 96 | "Turn a non-blocking call that returns EAGAIN into a blocking one."
|
96 | 97 | [session & body]
|
97 |
| - `(let [start-time# (-> (System/currentTimeMillis) (/ 1000.0) Math/round)] |
| 98 | + `(let [start-time# (System/currentTimeMillis)] |
98 | 99 | (while (= libssh2/ERROR_EAGAIN (do ~@body))
|
99 | 100 | (handle-errors ~session
|
100 | 101 | (wait ~session start-time#)))))
|
101 | 102 |
|
102 | 103 | (defmacro block-return
|
103 | 104 | "Similar to block, but for functions that return a pointer"
|
104 | 105 | [session & body]
|
105 |
| - `(let [start-time# (-> (System/currentTimeMillis) (/ 1000.0) Math/round)] |
| 106 | + `(let [start-time# (System/currentTimeMillis)] |
106 | 107 | (loop [result# (do ~@body)]
|
107 | 108 | (if (nil? result#)
|
108 | 109 | (let [errno# (libssh2-session/last-errno (:session ~session))]
|
|
0 commit comments