package udpapi
+import (
+ "strings"
+)
+
// Authenticates the supplied user with the supplied password. Blocks until we have a reply.
// Needed before almost any other API command can be used.
//
}
a.session = ""
- if udpkey != "" {
+ if udpKey != "" {
if err = a.encrypt(user, udpKey); err != nil {
return err
}
}
- r := <-a.SendRecv("AUTH", paramMap{
+ r := <-a.SendRecv("AUTH", ParamMap{
"user": user,
"pass": password,
"protover": 3,
//
// http://wiki.anidb.net/w/UDP_API_Definition#LOGOUT:_Logout
func (a *AniDBUDP) Logout() (err error) {
- r := <-a.SendRecv("LOGOUT", paramMap{})
+ r := <-a.SendRecv("LOGOUT", ParamMap{})
a.session = ""
return r.Error()
}
func (a *AniDBUDP) encrypt(user, udpKey string) (err error) {
- if reply := <-a.SendRecv("ENCRYPT", paramMap{"user": user, "type": 1}); reply.Error() != nil {
+ if reply := <-a.SendRecv("ENCRYPT", ParamMap{"user": user, "type": 1}); reply.Error() != nil {
return reply.Error()
} else {
switch reply.Code() {
package udpapi
import (
- "bytes"
"compress/zlib"
- "crypto/aes"
- "crypto/cipher"
- "crypto/md5"
"fmt"
"io"
- "io/ioutil"
"log"
"net"
"sort"
args["s"] = a.session
}
for k, v := range args {
- v = strings.Replace(v, "\n", "<br/>", -1)
- args[k] = strings.Replace(v, "&", "&", -1)
+ s := fmt.Sprint(v)
+ s = strings.Replace(s, "\n", "<br/>", -1)
+ args[k] = strings.Replace(s, "&", "&", -1)
}
+ ch := make(chan APIReply, 1)
+
if err := a.dial(); err != nil {
ch <- newErrorWrapper(err)
close(ch)
return ch
}
- ch := make(chan APIReply, 1)
-
a.routerLock.Lock()
a.tagRouter[tag] = ch
a.routerLock.Unlock()
p := makePacket([]byte(str), a.ecb)
- sendPacket(p, a.sendCh)
-}
-
-type packet struct {
- b []byte
- err error
- sent chan bool
+ return sendPacket(p, a.sendCh)
}
func (a *AniDBUDP) sendLoop() {
if c >= 720 && c < 799 {
// notices that need PUSHACK
id := strings.Fields(r.Text())[0]
- a.send("PUSHACK", paramMap{"nid": id})
+ a.send("PUSHACK", ParamMap{"nid": id})
a.Notifications <- r
} else if c == 799 {
package udpapi
+import (
+ "strconv"
+ "time"
+)
+
type UptimeReply struct {
APIReply
Uptime time.Duration
//
// http://wiki.anidb.net/w/UDP_API_Definition#UPTIME:_Retrieve_Server_Uptime
func (a *AniDBUDP) Uptime() <-chan UptimeReply {
- ch := make(chan *UptimeReply, 2)
+ ch := make(chan UptimeReply, 2)
go func() {
- reply := <-a.SendRecv("UPTIME", paramMap{})
+ reply := <-a.SendRecv("UPTIME", ParamMap{})
r := UptimeReply{APIReply: reply}
if r.Error() == nil {
//
// http://wiki.anidb.net/w/UDP_API_Definition#PING:_Ping_Command
func (a *AniDBUDP) Ping() <-chan PingReply {
- ch := make(chan *PingReply, 2)
+ ch := make(chan PingReply, 2)
go func() {
- reply := <-a.SendRecv("PING", paramMap{"nat": 1})
+ reply := <-a.SendRecv("PING", ParamMap{"nat": 1})
r := PingReply{APIReply: reply}
if r.Error() == nil {
"net"
)
+type packet struct {
+ b []byte
+ err error
+ sent chan bool
+}
+
func getPacket(conn *net.UDPConn, ecb *ecbState) (buf []byte, err error) {
buf = make([]byte, 1500)
n, err := conn.Read(buf)
import (
"errors"
- "reflect"
+ "fmt"
"strconv"
"strings"
)
-type apiError struct {
+type APIError struct {
Code int
Desc string
}
-func (err *apiError) Error() string {
+func (err *APIError) Error() string {
return fmt.Sprint(err.Code, err.Desc)
}
return ""
}
-func (_ *errorWrapper) Code() int {
+func (w *errorWrapper) Code() int {
switch e := w.err.(type) {
case *APIError:
return e.Code
text = strings.Join(parts[1:], " ")
}
- var e *apiError = nil
+ var e *APIError = nil
// 720-799 range is for notifications
// 799 is an API server shutdown notice, so I guess it's okay to be an error
if code < 200 || (code > 299 && code < 720) || code > 798 {
- e = &apiError{Code: int(code), Desc: text}
+ e = &APIError{Code: int(code), Desc: text}
}
return &genericReply{
"time"
)
-type packet struct {
- /*...*/
- sent chan bool
-}
-
type enqueuedPacket struct {
packet
queue chan packet
throttleDecInterval = 10 * time.Second
)
-func sendPacket(p packet, c chan packet) {
+func sendPacket(p packet, c chan packet) chan bool {
p.sent = make(chan bool, 2)
globalQueue.enqueue <- enqueuedPacket{packet: p, queue: c}
+ return p.sent
}
func (gq *sendQueueState) sendQueueDispatch() {