1

So I'm writing a game and using the Google Play Services framework to send and receive data.

I have to implement the IRealTimeEventListener interface and override the functions below to receive incoming network data.

class EventHandler : public gpg::IRealTimeEventListener
private:

  //IRealTimeEventListener
  void OnRoomStatusChanged(gpg::RealTimeRoom const &room) override;
  void OnConnectedSetChanged(gpg::RealTimeRoom const &room) override;
  void OnP2PConnected(gpg::RealTimeRoom const &room, gpg::MultiplayerParticipant const &participant) override;
  void OnP2PDisconnected(gpg::RealTimeRoom const &room, gpg::MultiplayerParticipant const &participant) override;
  void OnParticipantStatusChanged(gpg::RealTimeRoom const &room, gpg::MultiplayerParticipant const &participant) override;
  void OnDataReceived(gpg::RealTimeRoom const &room, gpg::MultiplayerParticipant const &from_participant, std::vector<uint8_t> data, bool is_reliable)
      override;

My question is how is this functionality implemented?

I have used the Observer pattern before, the Subject would call a collection of registered Observers and pass an event BUT I have to register the Observer first with the Subject.

In the case of the Google Play Services library there is NO Registration I simply implement the interface and start receiving data.

gpg.framework/Headers/i_real_time_event_listener.h

#ifndef GPG_I_REAL_TIME_EVENT_LISTENER_H_
#define GPG_I_REAL_TIME_EVENT_LISTENER_H_

#include <vector>
#include "gpg/multiplayer_participant.h"
#include "gpg/real_time_room.h"

namespace gpg {

class GPG_EXPORT IRealTimeEventListener {
 public:
  virtual ~IRealTimeEventListener() {}
  virtual void OnRoomStatusChanged(RealTimeRoom const &room) = 0;
  virtual void OnConnectedSetChanged(RealTimeRoom const &room) = 0;
  virtual void OnP2PConnected(RealTimeRoom const &room, MultiplayerParticipant const &participant) = 0;
  virtual void OnP2PDisconnected(RealTimeRoom const &room, MultiplayerParticipant const &participant) = 0;
  virtual void OnParticipantStatusChanged(RealTimeRoom const &room, MultiplayerParticipant const &participant) = 0;
  virtual void OnDataReceived(RealTimeRoom const &room, MultiplayerParticipant const &from_participant, std::vector<uint8_t> data, bool is_reliable) = 0;
};

}  // namespace gpg

#endif  // GPG_I_REAL_TIME_EVENT_LISTENER_H_

How can the functions of the interface be called by the library when my event handler does not register with a subject inside the library?

  • We would need a little more code to be able to answer it. What is most likely happening is that you are passing your EventHandler as parameter somewhere. The library is then using some kind of composition and calling your EventHandler's appropriate methods when needed. – soueuls Jan 20 '17 at 10:11
  • Thats just it, Im not passing my EventHandler as a parameter anywhere, I use Googles StateManager to initialise the service. After its been initialised and ready to use I can create EventHandler objects at runtime and because they implement IRealTimeEventListener they get called, no registration or passing as parameters. – David Jan 20 '17 at 12:36
  • I thought that maybe the IRealTimeEventListener had a constructor that was registering itself with the subject in the library (.a) file. But this isnt the case. – David Jan 20 '17 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.