-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
169 lines (150 loc) · 4.1 KB
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//rektcorder is a recorder and player for destiny.gg chat logs
//Copyright (C) 2013 Eric Culp
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Affero General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU Affero General Public License for more details.
//
//You should have received a copy of the GNU Affero General Public License
//along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"encoding/json"
"errors"
"log"
"reflect"
"strconv"
"time"
"code.google.com/p/go.net/websocket"
"github.com/wsxiaoys/terminal/color"
)
var (
ErrMessageNoHeader = errors.New("No header in message")
ErrMessageUnknownHeader = errors.New("Unknown header in message")
)
var Codec = websocket.Codec{marshall, unmarshall}
func unmarshall(msg []byte, payloadType byte, v interface{}) error {
debugf("receive msg: %s\n", msg)
i := bytes.IndexByte(msg, byte(' '))
if i == -1 {
return ErrMessageNoHeader
}
header := string(msg[0:i])
body := msg[i+1 : len(msg)]
var err error = ErrMessageNoHeader
switch header {
case "MSG":
var o Msg
err = json.Unmarshal(body, &o)
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(o))
case "PING":
var o Ping
err = json.Unmarshal(body, &o)
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(o))
case "PONG":
var o Pong
err = json.Unmarshal(body, &o)
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(o))
case "NAMES":
var o Names
err = json.Unmarshal(body, &o)
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(o))
case "JOIN":
var o Join
err = json.Unmarshal(body, &o)
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(o))
case "QUIT":
var o Quit
err = json.Unmarshal(body, &o)
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(o))
case "MUTE":
var o Mute
err = json.Unmarshal(body, &o)
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(o))
case "BROADCAST":
var o Broadcast
err = json.Unmarshal(body, &o)
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(o))
default:
log.Printf("Unknown header %s\n", header)
log.Printf(" Body: %s\n", body)
err = ErrMessageUnknownHeader
}
return err
}
func marshall(v interface{}) (msg []byte, payloadType byte, err error) {
switch v.(type) {
case Pong:
b, err := json.Marshal(v)
if err != nil {
return nil, 0, err
}
b = append([]byte("PONG "), b...)
debugf("send msg: %s\n", string(b))
return b, websocket.TextFrame, nil
}
return nil, 0, errors.New("unknown payload type")
}
type Msg struct {
Nick string `json:"nick"`
Features []string `json:"features"`
Timestamp Time `json:"timestamp"`
Data string `json:"data"`
}
func (m *Msg) Print() {
color.Printf("<@{!b}%s@{|}>: %s\n", m.Nick, m.Data)
}
type User struct {
Nick string `json:"nick"`
Features []string `json:"features"`
}
type Names struct {
Users []User `json:"users"`
}
type Ping struct {
Data int64 `json:"data"`
}
type Pong struct {
Data int64 `json:"data"`
}
type Join struct {
Nick string `json:"nick"`
Features []string `json:"features"`
Timestamp Time `json:"timestamp"`
}
type Quit struct {
Nick string `json:"nick"`
Features []string `json:"features"`
Timestamp Time `json:"timestamp"`
}
type Mute struct {
Nick string `json:"nick"` // the nick who muted
Features []string `json:"features"`
Timestamp Time `json:"timestamp"`
Data string `json:"data"` // the nick to mute
}
type Broadcast struct {
Timestamp Time `json:"timestamp"`
Data string `json:"data"`
}
type Time struct {
time.Time
}
func (t *Time) UnmarshalJSON(data []byte) error {
i, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return err
}
t.Time = time.Unix(i/1000, (i%1000)*1000000)
return nil
}
func (t Time) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(t.UnixNano()/1000000, 10)), nil
}