Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gofmt #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 68 additions & 69 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,81 @@
package main

import (
"net"
"fmt"
"comm"
"time"
"io"
"miop"
"config"
//"strconv"
"comm"
"config"
"fmt"
"io"
"miop"
"net"
"time"
//"strconv"
)

func main() {
fmt.Printf("pls enter uid@product:\n")
var uid string
fmt.Scan(&uid)
var lid string
fmt.Printf("pls end last msgid:\n")
fmt.Scan(&lid)
fmt.Printf("pls enter uid@product:\n")
var uid string
fmt.Scan(&uid)
var lid string
fmt.Printf("pls end last msgid:\n")
fmt.Scan(&lid)

room := getRoom()
if "" == room {
fmt.Printf("no active room\n")
return
}
conn, err := net.Dial("tcp", room)
comm.CheckError(err, "connect error")

var buf [1024]byte

msg := miop.MiopDataPack(uint32(123), "i am message")
b := miop.MiopPack("token:7a99ec51ee732b0536e1f93e0bde7737\nu:"+uid+"\nlid:"+lid, msg)
fmt.Printf("send len: %d\n", len(b))
_, errr := conn.Write(b)
comm.CheckError(errr, "send error")
defer conn.Close()
i := 1
for {
rlen, err := conn.Read(buf[0:])
if nil != err {
if err == io.EOF {
println("receive end")
break
} else {
println("server close\n")
}
}
datas, _ := miop.MiopUnpack(buf[:rlen])
for _, data := range datas {
msgs := data.ParseData()
for _, rmsg := range msgs {
fmt.Printf("receieve: id:%d msg:%s\n", rmsg.Id, rmsg.Msg)
}
}
//fmt.Printf("receieve: %s \n", buf)
//i++
//says := uid + "say: " + strconv.Itoa(i)
//conn.Write([]byte(says))
time.Sleep(time.Duration(i) * time.Second)
}
room := getRoom()
if "" == room {
fmt.Printf("no active room\n")
return
}
conn, err := net.Dial("tcp", room)
comm.CheckError(err, "connect error")

}
var buf [1024]byte

msg := miop.MiopDataPack(uint32(123), "i am message")
b := miop.MiopPack("token:7a99ec51ee732b0536e1f93e0bde7737\nu:"+uid+"\nlid:"+lid, msg)
fmt.Printf("send len: %d\n", len(b))
_, errr := conn.Write(b)
comm.CheckError(errr, "send error")
defer conn.Close()
i := 1
for {
rlen, err := conn.Read(buf[0:])
if nil != err {
if err == io.EOF {
println("receive end")
break
} else {
println("server close\n")
}
}
datas, _ := miop.MiopUnpack(buf[:rlen])
for _, data := range datas {
msgs := data.ParseData()
for _, rmsg := range msgs {
fmt.Printf("receieve: id:%d msg:%s\n", rmsg.Id, rmsg.Msg)
}
}
//fmt.Printf("receieve: %s \n", buf)
//i++
//says := uid + "say: " + strconv.Itoa(i)
//conn.Write([]byte(says))
time.Sleep(time.Duration(i) * time.Second)
}

}

func getRoom() string {
conn, err := net.Dial("tcp", config.DispatcherHost)
comm.CheckError(err, "connect err")
defer conn.Close()

var buf [100]byte
rlen, err := conn.Read(buf[0:])

if nil != err {
if err != io.EOF {
comm.CheckError(err, "read error")
}
}
conn, err := net.Dial("tcp", config.DispatcherHost)
comm.CheckError(err, "connect err")
defer conn.Close()

var buf [100]byte
rlen, err := conn.Read(buf[0:])

if nil != err {
if err != io.EOF {
comm.CheckError(err, "read error")
}
}

return string(buf[0:rlen])

return string(buf[0:rlen])

}
143 changes: 71 additions & 72 deletions comm/comm.go
Original file line number Diff line number Diff line change
@@ -1,116 +1,115 @@
package comm

import (
"crypto/md5"
"encoding/hex"
"github.com/hoisie/redis"
"strconv"
"config"
"fmt"
"bytes"
"bytes"
"config"
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/hoisie/redis"
"strconv"
)

func CheckError(err error, msg string) {
if nil != err {
str := "ERROR: " + msg + " " + err.Error()
GetLogger(ERROR).Println(msg)
panic(str)
}
if nil != err {
str := "ERROR: " + msg + " " + err.Error()
GetLogger(ERROR).Println(msg)
panic(str)
}
}

func LogError(err error, msg string) {
if nil != err {
GetLogger(ERROR).Println(msg)
}
if nil != err {
GetLogger(ERROR).Println(msg)
}

}

var client redis.Client

func GetMsg(key string) []byte {
return GetData(key)
return GetData(key)
}

func GetMsgList(id int) ([][]byte){
id++
msgs, err := client.Zrangebyscore(config.BoardCastKey, float64(id), 10000)
LogError(err, "getmsglist error")
return msgs
func GetMsgList(id int) [][]byte {
id++
msgs, err := client.Zrangebyscore(config.BoardCastKey, float64(id), 10000)
LogError(err, "getmsglist error")
return msgs
}

func AddMsg(msg string) (bool, int) {
id := setMaxId()
fmt.Printf("get max id: %d\n", id)
mid := strconv.Itoa(id)
msg = mid + "@$@" + msg
ok, err := client.Zadd(config.BoardCastKey, []byte(msg), float64(id))
CheckError(err, "add msg error")
return ok, id
id := setMaxId()
fmt.Printf("get max id: %d\n", id)
mid := strconv.Itoa(id)
msg = mid + "@$@" + msg
ok, err := client.Zadd(config.BoardCastKey, []byte(msg), float64(id))
CheckError(err, "add msg error")
return ok, id
}

func setMaxId() int{
id := GetMsg("MSG_MAX_ID")
mid := 0
if nil != id {
mid, _ = strconv.Atoi(string(id))
}
mid++
err := client.Set("MSG_MAX_ID", []byte(strconv.Itoa(mid)))
CheckError(err, "set max id error")
return mid
func setMaxId() int {
id := GetMsg("MSG_MAX_ID")
mid := 0
if nil != id {
mid, _ = strconv.Atoi(string(id))
}
mid++
err := client.Set("MSG_MAX_ID", []byte(strconv.Itoa(mid)))
CheckError(err, "set max id error")
return mid
}

func SetSendMaxId() {
msgs, err := client.Zrevrange(config.BoardCastKey, 0, 0)
LogError(err, "set send max id error")
id := []byte("0")
if len(msgs) > 0 {
arrs := bytes.Split(msgs[0], []byte("@$@"))
if len(arrs) > 1 {
id = arrs[0]
}
}

client.Set("SEND_MSG_MAX_ID", id)
msgs, err := client.Zrevrange(config.BoardCastKey, 0, 0)
LogError(err, "set send max id error")
id := []byte("0")
if len(msgs) > 0 {
arrs := bytes.Split(msgs[0], []byte("@$@"))
if len(arrs) > 1 {
id = arrs[0]
}
}

client.Set("SEND_MSG_MAX_ID", id)
}

func GetSendMaxId() int {
id := GetMsg("SEND_MSG_MAX_ID")
mid := 0
if nil != id {
mid, _ = strconv.Atoi(string(id))
}
return mid
id := GetMsg("SEND_MSG_MAX_ID")
mid := 0
if nil != id {
mid, _ = strconv.Atoi(string(id))
}
return mid
}

func GetData(key string) []byte {
val, _ := client.Get(key)
return val
val, _ := client.Get(key)
return val
}

func SetData(msg string) {

}

func SaveData(key string, val []byte) bool {
err := client.Set(key, val)
if err != nil {
return false
}
return true
err := client.Set(key, val)
if err != nil {
return false
}
return true
}

func GetMd5(key string) string {
h := md5.New()
h.Write([]byte(key))
return hex.EncodeToString(h.Sum(nil))
h := md5.New()
h.Write([]byte(key))
return hex.EncodeToString(h.Sum(nil))
}


func MergeByte(b1, b2 []byte) []byte {
tmp := make([][]byte, 0)
tmp[0] = b1
tmp[1] = b2
return bytes.Join(tmp, []byte{})
tmp := make([][]byte, 0)
tmp[0] = b1
tmp[1] = b2

return bytes.Join(tmp, []byte{})
}
Loading