Skip to content

Instantly share code, notes, and snippets.

@aphyr
Created December 2, 2011 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aphyr/1421821 to your computer and use it in GitHub Desktop.
Save aphyr/1421821 to your computer and use it in GitHub Desktop.
(ns ufold.core
(:use clojure.contrib.logging))
(use 'lamina.core 'aleph.tcp 'gloss.core)
(require 'gloss.io)
(use 'protobuf)
(defprotobuf Msg Ufold$Msg)
(defn dump-bytes [bytes]
(log :info ["Bytes are " (seq bytes)]))
(defn decode [s]
(let [buffer (gloss.io/contiguous s)]
(let [bytes (byte-array (.remaining buffer))]
(.get buffer bytes 0 (alength bytes))
(protobuf-load Msg bytes))))
(defn encode [message]
(protobuf-dump message))
(defn echo-handler [channel client-info]
(receive-all channel (fn [buffer]
(when buffer
(let [msg (decode buffer)]
(enqueue channel (encode msg)))))))
(defn start-server []
(start-tcp-server echo-handler
{:port 5555,
:frame (finite-block :int32)}))
(defn start-client []
(wait-for-result
(tcp-client {
:host "localhost",
:port 5555,
:frame (finite-block :int32)})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment