Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10)

Unified Diff: rpc/jsoncodec/conn.go

Issue 9410043: rpc: refactor
Patch Set: rpc: refactor Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « rpc/jsoncodec/codec_test.go ('k') | rpc/methods.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: rpc/jsoncodec/conn.go
=== added file 'rpc/jsoncodec/conn.go'
--- rpc/jsoncodec/conn.go 1970-01-01 00:00:00 +0000
+++ rpc/jsoncodec/conn.go 2013-05-16 17:31:36 +0000
@@ -0,0 +1,58 @@
+package jsoncodec
+
+import (
+ "code.google.com/p/go.net/websocket"
+ "encoding/json"
+ "launchpad.net/juju-core/rpc"
+ "net"
+)
+
+// NewWebsocket returns an rpc codec that uses the given websocket
+// connection to send and receive messages.
+func NewWebsocket(conn *websocket.Conn) rpc.Codec {
+ return New(wsJSONConn{conn})
+}
+
+type wsJSONConn struct {
+ conn *websocket.Conn
+}
+
+func (conn wsJSONConn) Send(msg interface{}) error {
+ return websocket.JSON.Send(conn.conn, msg)
+}
+
+func (conn wsJSONConn) Receive(msg interface{}) error {
+ return websocket.JSON.Receive(conn.conn, msg)
+}
+
+func (conn wsJSONConn) Close() error {
+ return conn.conn.Close()
+}
+
+// NewNet returns an rpc codec that uses the given net
+// connection to send and receive messages.
+func NewNet(conn net.Conn) rpc.Codec {
+ return New(&netConn{
+ enc: json.NewEncoder(conn),
+ dec: json.NewDecoder(conn),
+ conn: conn,
+ })
+}
+
+type netConn struct {
+ enc *json.Encoder
+ dec *json.Decoder
+ conn net.Conn
+}
+
+func (conn *netConn) Send(msg interface{}) error {
+ return conn.enc.Encode(msg)
+}
+
+func (conn *netConn) Receive(msg interface{}) error {
+ return conn.dec.Decode(msg)
+}
+
+func (conn *netConn) Close() error {
+ return conn.conn.Close()
+}
« no previous file with comments | « rpc/jsoncodec/codec_test.go ('k') | rpc/methods.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b