Created
December 2, 2011 04:57
-
-
Save aphyr/1421821 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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