Home | History | Annotate | Download | only in client
      1 /*
      2  *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
     12 #define WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
     13 #pragma once
     14 
     15 #include <map>
     16 #include <string>
     17 
     18 #include "webrtc/base/nethelpers.h"
     19 #include "webrtc/base/physicalsocketserver.h"
     20 #include "webrtc/base/scoped_ptr.h"
     21 #include "webrtc/base/signalthread.h"
     22 #include "webrtc/base/sigslot.h"
     23 
     24 typedef std::map<int, std::string> Peers;
     25 
     26 struct PeerConnectionClientObserver {
     27   virtual void OnSignedIn() = 0;  // Called when we're logged on.
     28   virtual void OnDisconnected() = 0;
     29   virtual void OnPeerConnected(int id, const std::string& name) = 0;
     30   virtual void OnPeerDisconnected(int peer_id) = 0;
     31   virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
     32   virtual void OnMessageSent(int err) = 0;
     33   virtual void OnServerConnectionFailure() = 0;
     34 
     35  protected:
     36   virtual ~PeerConnectionClientObserver() {}
     37 };
     38 
     39 class PeerConnectionClient : public sigslot::has_slots<>,
     40                              public rtc::MessageHandler {
     41  public:
     42   enum State {
     43     NOT_CONNECTED,
     44     RESOLVING,
     45     SIGNING_IN,
     46     CONNECTED,
     47     SIGNING_OUT_WAITING,
     48     SIGNING_OUT,
     49   };
     50 
     51   PeerConnectionClient();
     52   ~PeerConnectionClient();
     53 
     54   int id() const;
     55   bool is_connected() const;
     56   const Peers& peers() const;
     57 
     58   void RegisterObserver(PeerConnectionClientObserver* callback);
     59 
     60   void Connect(const std::string& server, int port,
     61                const std::string& client_name);
     62 
     63   bool SendToPeer(int peer_id, const std::string& message);
     64   bool SendHangUp(int peer_id);
     65   bool IsSendingMessage();
     66 
     67   bool SignOut();
     68 
     69   // implements the MessageHandler interface
     70   void OnMessage(rtc::Message* msg);
     71 
     72  protected:
     73   void DoConnect();
     74   void Close();
     75   void InitSocketSignals();
     76   bool ConnectControlSocket();
     77   void OnConnect(rtc::AsyncSocket* socket);
     78   void OnHangingGetConnect(rtc::AsyncSocket* socket);
     79   void OnMessageFromPeer(int peer_id, const std::string& message);
     80 
     81   // Quick and dirty support for parsing HTTP header values.
     82   bool GetHeaderValue(const std::string& data, size_t eoh,
     83                       const char* header_pattern, size_t* value);
     84 
     85   bool GetHeaderValue(const std::string& data, size_t eoh,
     86                       const char* header_pattern, std::string* value);
     87 
     88   // Returns true if the whole response has been read.
     89   bool ReadIntoBuffer(rtc::AsyncSocket* socket, std::string* data,
     90                       size_t* content_length);
     91 
     92   void OnRead(rtc::AsyncSocket* socket);
     93 
     94   void OnHangingGetRead(rtc::AsyncSocket* socket);
     95 
     96   // Parses a single line entry in the form "<name>,<id>,<connected>"
     97   bool ParseEntry(const std::string& entry, std::string* name, int* id,
     98                   bool* connected);
     99 
    100   int GetResponseStatus(const std::string& response);
    101 
    102   bool ParseServerResponse(const std::string& response, size_t content_length,
    103                            size_t* peer_id, size_t* eoh);
    104 
    105   void OnClose(rtc::AsyncSocket* socket, int err);
    106 
    107   void OnResolveResult(rtc::AsyncResolverInterface* resolver);
    108 
    109   PeerConnectionClientObserver* callback_;
    110   rtc::SocketAddress server_address_;
    111   rtc::AsyncResolver* resolver_;
    112   rtc::scoped_ptr<rtc::AsyncSocket> control_socket_;
    113   rtc::scoped_ptr<rtc::AsyncSocket> hanging_get_;
    114   std::string onconnect_data_;
    115   std::string control_data_;
    116   std::string notification_data_;
    117   std::string client_name_;
    118   Peers peers_;
    119   State state_;
    120   int my_id_;
    121 };
    122 
    123 #endif  // WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_
    124