Beispiel #1
0
func (s *session) sendPing() error {
	logger := Logger(s.ctx)
	now := time.Now()
	cmd, err := proto.MakeEvent(&proto.PingEvent{
		UnixTime:     proto.Time(now),
		NextUnixTime: proto.Time(now.Add(3 * KeepAlive / 2)),
	})
	if err != nil {
		logger.Printf("error: ping event: %s", err)
		return err
	}
	data, err := cmd.Encode()
	if err != nil {
		logger.Printf("error: ping event encode: %s", err)
		return err
	}

	if err := s.conn.WriteMessage(websocket.TextMessage, data); err != nil {
		logger.Printf("error: write ping event: %s", err)
		return err
	}

	s.expectedPingReply = now.Unix()
	s.outstandingPings++
	return nil
}
Beispiel #2
0
func (s *session) sendSnapshot(msgs []proto.Message, listing proto.Listing) error {
	for i, msg := range msgs {
		if msg.EncryptionKeyID != "" {
			dmsg, err := proto.DecryptMessage(msg, s.client.Authorization.MessageKeys)
			if err != nil {
				continue
			}
			msgs[i] = dmsg
		}
	}

	snapshot := &proto.SnapshotEvent{
		Identity:  s.Identity().ID(),
		SessionID: s.ID(),
		Version:   s.room.Version(),
		Listing:   listing,
		Log:       msgs,
	}

	event, err := proto.MakeEvent(snapshot)
	if err != nil {
		return err
	}
	s.outgoing <- event
	return nil
}
Beispiel #3
0
func (s *session) sendHello(roomIsPrivate, accountHasAccess bool) error {
	logger := Logger(s.ctx)
	event := &proto.HelloEvent{
		SessionView:      s.View(),
		AccountHasAccess: accountHasAccess,
		RoomIsPrivate:    roomIsPrivate,
		Version:          s.room.Version(),
	}
	if s.client.Account != nil {
		event.AccountView = s.client.Account.View(s.roomName)
	}
	event.ID = event.SessionView.ID
	cmd, err := proto.MakeEvent(event)
	if err != nil {
		logger.Printf("error: hello event: %s", err)
		return err
	}
	data, err := cmd.Encode()
	if err != nil {
		logger.Printf("error: hello event encode: %s", err)
		return err
	}

	if err := s.conn.WriteMessage(websocket.TextMessage, data); err != nil {
		logger.Printf("error: write hello event: %s", err)
		return err
	}

	return nil
}
Beispiel #4
0
func (s *session) sendHello(roomIsPrivate, accountHasAccess bool) error {
	logger := logging.Logger(s.ctx)
	event := &proto.HelloEvent{
		SessionView:      s.View(s.privilegeLevel()),
		AccountHasAccess: accountHasAccess,
		RoomIsPrivate:    roomIsPrivate,
		Version:          s.room.Version(),
	}
	if s.client.Account != nil {
		event.AccountView = &proto.PersonalAccountView{
			AccountView: *s.client.Account.View(s.roomName),
		}
		event.AccountView.Email, event.AccountEmailVerified = s.client.Account.Email()
	}
	event.ID = event.SessionView.ID
	cmd, err := proto.MakeEvent(event)
	if err != nil {
		logger.Printf("error: hello event: %s", err)
		return err
	}
	data, err := cmd.Encode()
	if err != nil {
		logger.Printf("error: hello event encode: %s", err)
		return err
	}

	if err := s.writeMessage(websocket.TextMessage, data); err != nil {
		logger.Printf("error: write hello event: %s", err)
		return err
	}

	return nil
}
Beispiel #5
0
func (s *session) sendSnapshot() error {
	snapshot, err := s.room.Snapshot(s.ctx, s, s.privilegeLevel(), 100)
	if err != nil {
		return err
	}

	s.identity.name = snapshot.Nick

	for i, msg := range snapshot.Log {
		if msg.EncryptionKeyID != "" {
			dmsg, err := proto.DecryptMessage(msg, s.client.Authorization.MessageKeys, s.privilegeLevel())
			if err != nil {
				continue
			}
			snapshot.Log[i] = dmsg
		}
	}

	s.identity.name = snapshot.Nick

	event, err := proto.MakeEvent(snapshot)
	if err != nil {
		return err
	}

	s.outgoing <- event
	return nil
}
Beispiel #6
0
func (s *session) sendDisconnect(reason string) error {
	event, err := proto.MakeEvent(&proto.DisconnectEvent{Reason: reason})
	if err != nil {
		return err
	}
	s.outgoing <- event
	return nil
}
Beispiel #7
0
func (s *session) sendBounce(reason string) error {
	bounce := &proto.BounceEvent{
		Reason: reason,
		// TODO: fill in AuthOptions
	}
	event, err := proto.MakeEvent(bounce)
	if err != nil {
		return err
	}
	s.outgoing <- event
	return nil
}
Beispiel #8
0
// invalidatePeer must be called with lock held
func (b *Backend) invalidatePeer(ctx scope.Context, id, era string) {
	logger := logging.Logger(ctx)
	packet, err := proto.MakeEvent(&proto.NetworkEvent{
		Type:      "partition",
		ServerID:  id,
		ServerEra: era,
	})
	if err != nil {
		logger.Printf("cluster: make network event error: %s", err)
		return
	}
	for _, lm := range b.listeners {
		if err := lm.Broadcast(ctx, packet); err != nil {
			logger.Printf("cluster: network event error: %s", err)
		}
	}
}