本文整理汇总了Golang中misc/packet.Pack函数的典型用法代码示例。如果您正苦于以下问题:Golang Pack函数的具体用法?Golang Pack怎么用?Golang Pack使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Pack函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: stats_sender
func stats_sender() {
_accum_buffer := make(map[string]map[string]int32)
_update_buffer := make(map[string]map[string]string)
stats_timer := make(chan int32, 100)
stats_timer <- 1
for {
select {
case req := <-AccumQueue:
if _, ok := _accum_buffer[req.F_lang]; !ok {
val := make(map[string]int32)
val[req.F_key] = 0
_accum_buffer[req.F_lang] = val
}
val := _accum_buffer[req.F_lang]
val[req.F_key] += req.F_value
_accum_buffer[req.F_lang] = val
case req := <-UpdateQueue:
if _, ok := _update_buffer[req.F_lang]; !ok {
val := make(map[string]string)
val[req.F_key] = ""
_update_buffer[req.F_lang] = val
}
val := _update_buffer[req.F_lang]
val[req.F_key] = req.F_value
_update_buffer[req.F_lang] = val
case <-stats_timer:
INFO("Stats Buffer:", len(_accum_buffer), len(_update_buffer))
// 累计
accum := SET_ADDS_REQ{}
for accum.F_lang, _ = range _accum_buffer {
for accum.F_key, accum.F_value = range _accum_buffer[accum.F_lang] {
Send(packet.Pack(Code["set_adds_req"], &accum, nil))
}
}
_accum_buffer = make(map[string]map[string]int32)
// 更新
update := SET_UPDATE_REQ{}
for update.F_lang, _ = range _update_buffer {
for update.F_key, update.F_value = range _update_buffer[update.F_lang] {
Send(packet.Pack(Code["set_update_req"], &update, nil))
}
}
_update_buffer = make(map[string]map[string]string)
// FINI
config := cfg.Get()
period := STATS_COLLECT_PERIOD
if config["stats_collect_period"] != "" {
period, _ = strconv.Atoi(config["stats_collect_period"])
}
timer.Add(0, time.Now().Unix()+int64(period), stats_timer)
runtime.GC()
}
}
}
示例2: SendToUser
func (gs *centerGs) SendToUser(userId int64, api int16, tbl interface{}) {
payload := packet.Pack(api, tbl, nil)
msg := protocol.PKT_forward_msg_info{
F_size: int32(len(payload)),
F_session_id: protocol.NIL_SESSION_ID,
F_user_id: userId,
F_msg: string(payload),
}
gs.session.Send(packet.Pack(protocol.FORWARD_MSG_NTF, &msg, nil))
}
示例3: AllianceBoardCast
// 发送公会广播信息(通过Gate广播,所以这条转发消息发给任意GS都可以)。
func AllianceBoardCast(asId int32, api int16, tbl interface{}) {
payload := packet.Pack(api, tbl, nil)
for _, gs := range gsMap {
msg := protocol.PKT_gs_alliance_boardcast_info{
F_alliance_id: asId,
F_msg: string(payload),
}
gs.session.Send(packet.Pack(protocol.GS_ALLIANCE_BOARDCAST_NTF,
&msg, nil))
return
}
}
示例4: BenchmarkAgent
func BenchmarkAgent(b *testing.B) {
fmt.Println("Benchmark", b.N)
for i := 0; i < b.N; i++ {
addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:8888")
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
U := user_login_info{}
U.F_user_name = fmt.Sprintf("test%v", i)
U.F_mac_addr = fmt.Sprintf("mac%v", i)
pkt := packet.Pack(Code["user_login_req"], &U, nil)
writer := packet.Writer()
writer.WriteU16(uint16(len(pkt) + 4))
writer.WriteU32(0)
writer.WriteRawBytes(pkt)
ret := make([]byte, 100)
conn.Write(writer.Data())
conn.Read(ret)
}
}
示例5: P_forward_req
func P_forward_req(hostid int32, pkt *packet.Packet) []byte {
tbl, _ := PKT_FORWARDIPC(pkt)
object := &IPCObject{}
err := json.Unmarshal(tbl.F_IPC, object)
if err != nil {
log.Println("decode forward IPCObject error")
return nil
}
// if user is online, send to the server, or else send to database
state := core.State(object.DestID)
//fmt.Println(tbl.F_dest_id, tbl.F_IPC)
switch state {
case core.ON_PROT, core.ON_FREE:
host := core.Host(object.DestID)
ch := ForwardChan(host)
if ch != nil {
ch <- tbl.F_IPC
} else {
forward_tbl.Push(object)
}
default:
forward_tbl.Push(object)
}
ret := INT{F_v: 1}
return packet.Pack(-1, &ret, nil)
}
示例6: P_rank_list_req
func P_rank_list_req(sess *Session, reader *packet.Packet) (ret []byte, err error) {
ids, scores, err := ipc.GetList(1, -1)
if err != nil {
return nil, err
}
out := rank_list{}
out.F_items = make([]rank_list_item, len(ids))
for k, v := range ids {
info, _ := ipc.GetInfo(v)
out.F_items[k].F_id = v
out.F_items[k].F_name = info.Name
out.F_items[k].F_rank = scores[k]
out.F_items[k].F_state = info.State
t := int32(info.ProtectTime - time.Now().Unix())
if t > 0 {
out.F_items[k].F_protect_time = int32(t)
} else {
out.F_items[k].F_protect_time = 0
}
}
writer := packet.Writer()
return packet.Pack(Code["rank_list_ack"], out, writer), nil
}
示例7: P_cancel_req
func P_cancel_req(reader *packet.Packet) []byte {
tbl, _ := PKT_CANCEL_EVENT(reader)
core.Cancel(tbl.F_event_id)
ret := INT{1}
return packet.Pack(-1, &ret, nil)
}
示例8: P_add_req
func P_add_req(reader *packet.Packet) []byte {
tbl, _ := PKT_ADD_EVENT(reader)
event_id := core.Add(tbl.F_type, tbl.F_user_id, tbl.F_timeout, tbl.F_params)
ret := INT{event_id}
return packet.Pack(-1, &ret, nil)
}
示例9: P_get_seed_req
// 密钥交换
func P_get_seed_req(sess *Session, reader *packet.Packet) []byte {
tbl, _ := PKT_seed_info(reader)
// KEY1
X1, E1 := dh.DHExchange()
KEY1 := dh.DHKey(X1, big.NewInt(int64(tbl.F_client_send_seed)))
// KEY2
X2, E2 := dh.DHExchange()
KEY2 := dh.DHKey(X2, big.NewInt(int64(tbl.F_client_receive_seed)))
ret := seed_info{int32(E1.Int64()), int32(E2.Int64())}
// 服务器加密种子是客户端解密种子
encoder, err := rc4.NewCipher([]byte(fmt.Sprintf("%v%v", SALT, KEY2)))
if err != nil {
log.Critical(err)
return nil
}
decoder, err := rc4.NewCipher([]byte(fmt.Sprintf("%v%v", SALT, KEY1)))
if err != nil {
log.Critical(err)
return nil
}
sess.Encoder = encoder
sess.Decoder = decoder
sess.Flag |= SESS_KEYEXCG
return packet.Pack(Code["get_seed_ack"], ret, nil)
}
示例10: P_add_req
func P_add_req(reader *packet.Packet) []byte {
tbl, _ := PKT_ADD_REQ(reader)
ret := INT{0}
fmt.Println(tbl)
return packet.Pack(-1, &ret, nil)
}
示例11: P_user_login_req
func P_user_login_req(sess *Session, reader *packet.Packet) (ret []byte, err error) {
tbl, _ := PKT_user_login_info(reader)
writer := packet.Writer()
failed := command_result_pack{F_rst: 0}
success := user_snapshot{}
//------------------------------------------------
config := cfg.Get()
version, _ := strconv.Atoi(config["version"])
if tbl.F_client_version != int32(version) {
ret = packet.Pack(Code["user_login_faild_ack"], failed, writer)
return
}
if tbl.F_new_user == 0 {
if user_tbl.LoginMAC(sess.User.Mac, &sess.User) {
ipc.Register(sess, sess.User.Id)
_fill_user_snapshot(&sess.User, &success)
ret = packet.Pack(Code["user_login_succeed_ack"], success, writer)
return
} else {
ret = packet.Pack(Code["user_login_faild_ack"], failed, writer)
return
}
} else {
// register to db & online user
sess.User.Name = tbl.F_user_name
sess.User.Mac = tbl.F_mac_addr
sess.User.CreatedAt = time.Now()
if user_tbl.New(&sess.User) {
ipc.Register(sess, sess.User.Id)
// TODO: add user
//ranklist.AddUser(&sess.User)
_fill_user_snapshot(&sess.User, &success)
ret = packet.Pack(Code["user_login_succeed_ack"], success, writer)
return
} else {
ret = packet.Pack(Code["user_login_faild_ack"], failed, writer)
return
}
}
return
}
示例12: TestAgent
func TestAgent(t *testing.T) {
log.Println("Connecting to GS")
addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:8888")
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
U := &user_login_info{}
U.F_user_name = "test1"
U.F_mac_addr = "mac1"
pkt := packet.Pack(Code["user_login_req"], U, nil)
writer := packet.Writer()
writer.WriteU16(uint16(len(pkt) + 4))
writer.WriteU32(0)
writer.WriteRawBytes(pkt)
conn.Write(writer.Data())
ret := make([]byte, 100)
n, _ := conn.Read(ret)
fmt.Printf("%q\n", ret[:n])
// talk
msg := &talk{}
msg.F_user = "test1"
msg.F_msg = "hello world"
pkt = packet.Pack(Code["talk_req"], msg, nil)
writer = packet.Writer()
writer.WriteU16(uint16(len(pkt) + 4))
writer.WriteU32(0)
writer.WriteRawBytes(pkt)
conn.Write(writer.Data())
n, _ = conn.Read(ret)
fmt.Printf("%q\n", ret[:n])
}
示例13: P_changescore_req
func P_changescore_req(hostid int32, pkt *packet.Packet) ([]byte, error) {
tbl, _ := PKT_CHGSCORE(pkt)
ret := INT{F_v: 0}
if ranklist.ChangeScore(tbl.F_id, tbl.F_oldscore, tbl.F_newscore) {
ret.F_v = 1
}
return packet.Pack(Code["changescore_ack"], ret, nil), nil
}
示例14: P_logout_req
func P_logout_req(hostid int32, pkt *packet.Packet) ([]byte, error) {
tbl, _ := PKT_ID(pkt)
ret := INT{F_v: 0}
if ranklist.Logout(tbl.F_id) {
ret.F_v = 1
}
return packet.Pack(Code["logout_ack"], ret, nil), nil
}
示例15: IPC_talk
func IPC_talk(sess *Session, obj *IPCObject) []byte {
var str string
err := json.Unmarshal(obj.Object, &str)
if err == nil {
ret := &talk{F_user: sess.User.Name, F_msg: str}
return packet.Pack(Code["talk_notify"], ret, nil)
}
return nil
}