示例#1
0
文件: comm.go 项目: archwyrm/ghack
// Create a new client and start up send/receive goroutines.
func newClient(svc ServiceContext, cs chan<- Msg, conn net.Conn,
	l *protocol.Login) *client {
	send_ch := make(chan Msg)
	recv_ch := make(chan *protocol.Message)
	obs := createObserver(svc, send_ch)
	avatar, uid := AvatarFunc(svc, recv_ch)
	cl := &client{
		name:        *l.Name,
		permissions: proto.GetUint32(l.Permissions),
		conn:        conn,
		SendQueue:   send_ch,
		RecvQueue:   recv_ch,
		observer:    obs,
		avatar:      avatar,
	}
	go cl.RecvLoop(cs)
	go cl.SendLoop(cs)

	// Only attempt to assign control if a real avatar channel was returned,
	// otherwise the uid is just a dummy and should not be sent. This mostly
	// applies to tests.
	if avatar != nil {
		send_ch <- MsgAssignControl{uid, false}
	}
	return cl
}
// protoToItem converts a protocol buffer item to a Go struct.
func protoToItem(p *pb.MemcacheGetResponse_Item) *Item {
	return &Item{
		Key:        string(p.Key),
		Value:      p.Value,
		Flags:      proto.GetUint32(p.Flags),
		Expiration: proto.GetInt32(p.ExpiresInSeconds),
		casID:      proto.GetUint64(p.CasId),
	}
}