                      PrintWriter    BufferedWriter OutputStreamWriter
                      DataInputStream  BufferedInputStream
                      DataOutputStream BufferedOutputStream)))
-
-(defmacro- safe [& body]
-  `(try ~@body (catch Exception e# e#)))
 
 (defn- embedded-query [dba q]
   (let [[q-type q-opts] q]

                in     (PushbackReader. (BufferedReader. (InputStreamReader.  (.getInputStream  socket))))]
      (.setKeepAlive socket true)
      (loop []
        (let [read-result (text-read-query in io/eof)]
          (when-not (identical? io/eof read-result)
            (if (instance? Exception read-result)
              (text-write-exception out read-result)
              (let [query-result (process-query dba read-result)]
                (if (instance? Exception query-result)
                  (text-write-exception out query-result)
                  (text-write-result out query-result))))
            (recur)))))
    (catch Exception e
      (stacktrace/pst-on System/err false e)
      (.println System/err))))

(defn- binary-read-query [#^DataInputStream in eof-val]
  (safe
    (io/dis-read in eof-val)))

(defn- binary-write-exception [#^DataOutputStream out e]
  (io/dos-write out [1 (stacktrace/pst-str e)]))

(defn- binary-write-result [#^DataOutputStream out result]
  (io/dos-write out [0 result]))

(defn- binary-handler [dba #^Socket socket]
  (try

                in     (DataInputStream.  (BufferedInputStream.  (.getInputStream  socket)))]
      (.setKeepAlive socket true)
      (loop []
        (try
          (let [query  (io/dis-read in io/eof)
                result (process-query dba query)]
            (io/dos-write out [0 result]))
          (catch Exception e
            (io/dos-write out [1 (stacktrace/pst-str e)])))
        (recur)))
    (catch Exception e
      (stacktrace/pst-on System/err false e)
      (.println System/err))))
