-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketHandler.go
166 lines (143 loc) · 4.87 KB
/
socketHandler.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net"
)
type JsonRpc struct {
Api string `json:"api"`
Name string `json:"name"`
Params map[string]interface{} `json:"params"`
}
type JsonRpcResult struct {
Name string `json:"name"`
Result interface{} `json:"result"`
}
type SocketReader struct {
Twitch *TwitchApi
Local *LocalApi
Listener net.Listener
TwitchFuncmap map[string]func(*TwitchApi, []byte) bytes.Buffer
LocalFuncmap map[string]func(*LocalApi, []byte) bytes.Buffer
}
// Properly create a new socket reader
func NewSocketReader(api *TwitchApi, chat *TwitchChat) *SocketReader {
read := new(SocketReader)
read.Twitch = api
read.Local = NewLocalApi("", api.auth, chat)
ln, err := net.Listen("tcp", ":1921")
if err != nil {
log.Panic("Could not open socketReader")
}
read.Listener = ln
// Load twitch api functions into a function map, so we can dispatch calls easily
read.TwitchFuncmap = make(map[string]func(*TwitchApi, []byte) bytes.Buffer)
read.TwitchFuncmap["getChannel"] = (*TwitchApi).getChannel
read.TwitchFuncmap["getChannelVideos"] = (*TwitchApi).getChannelVideos
read.TwitchFuncmap["getChannelFollows"] = (*TwitchApi).getChannelFollows
read.TwitchFuncmap["getChannelTeams"] = (*TwitchApi).getChannelTeams
read.TwitchFuncmap["getChannelBadges"] = (*TwitchApi).getChannelBadges
read.TwitchFuncmap["getEmotes"] = (*TwitchApi).getEmotes
read.TwitchFuncmap["getUserFollows"] = (*TwitchApi).getUserFollows
read.TwitchFuncmap["isUserFollowing"] = (*TwitchApi).isUserFollowing
read.TwitchFuncmap["getGames"] = (*TwitchApi).getGames
read.TwitchFuncmap["searchChannels"] = (*TwitchApi).searchChannels
read.TwitchFuncmap["searchStreams"] = (*TwitchApi).searchStreams
read.TwitchFuncmap["searchGames"] = (*TwitchApi).searchGames
read.TwitchFuncmap["getStream"] = (*TwitchApi).getStream
read.TwitchFuncmap["getFeaturedStreams"] = (*TwitchApi).getFeaturedStreams
read.TwitchFuncmap["getFollowedStreams"] = (*TwitchApi).getFollowedStreams
read.TwitchFuncmap["getFollowedGames"] = (*TwitchApi).getFollowedGames
// Load local api functions into a function map, so we can dispatch calls easily
read.LocalFuncmap = make(map[string]func(*LocalApi, []byte) bytes.Buffer)
read.LocalFuncmap["getStreamUrl"] = (*LocalApi).getStreamUrl
read.LocalFuncmap["getStreamDesc"] = (*LocalApi).getStreamDesc
read.LocalFuncmap["changeChat"] = (*LocalApi).changeChat
read.LocalFuncmap["isAuthenticated"] = (*LocalApi).isAuthenticated
return read
}
// Accept incomming connections
func (read *SocketReader) StartReader() {
fmt.Println("Started accepting")
for {
conn, err := read.Listener.Accept()
if err != nil {
log.Printf("SocketReader could not accept incomming connection: %s", err)
}
go read.HandleConnection(conn)
}
fmt.Println("No more accepting!")
}
// Handle each incoming connection
func (read *SocketReader) HandleConnection(conn net.Conn) {
// Read json data from the connection
var data bytes.Buffer
var total int
tmp := make([]byte, 256)
// Infinitely handle the connection until it's closed
Loop:
for {
num, err := conn.Read(tmp)
if err != nil {
if err != io.EOF {
log.Printf("SocketReader failed to read: %s", err)
}
break
}
total += num
// If the buffer was filled, try to read again to make sure there is no data left
for num == 256 {
data.Write(tmp)
num, err := conn.Read(tmp)
if err != nil {
if err != io.EOF {
log.Printf("SocketReader failed to read: %s", err)
}
break Loop
}
total += num
}
// Append what we have to the end of the message and handle it
data.Write(tmp)
read.DispatchConnection(conn, data.Bytes()[:total])
// Reset the message and message size
data.Reset()
total = 0
}
}
func (read *SocketReader) DispatchConnection(conn net.Conn, apiParams []byte) {
call := new(JsonRpc)
err := json.Unmarshal(apiParams, call)
//log.Print(err)
if err != nil {
log.Print("Socket reader could not parse command")
return
}
// Extract the json from the call parameters, and encode it as a string
command, _ := json.Marshal(call.Params)
// Dispatch function based on api
if call.Api == "local" {
result := read.LocalFuncmap[call.Name](read.Local, command)
var genericResult interface{}
json.Unmarshal(result.Bytes(), &genericResult)
resultJson := new(JsonRpcResult)
resultJson.Name = call.Name
resultJson.Result = genericResult
buf, _ := json.Marshal(resultJson)
conn.Write(buf)
} else if call.Api == "twitch" {
result := read.TwitchFuncmap[call.Name](read.Twitch, command)
var genericResult interface{}
json.Unmarshal(result.Bytes(), &genericResult)
// fmt.Println("Stuff:", string(result.Bytes()))
// fmt.Println("Stuff2:", genericResult)
resultJson := new(JsonRpcResult)
resultJson.Name = call.Name
resultJson.Result = genericResult
buf, _ := json.Marshal(resultJson)
conn.Write(buf)
}
}