Twitch SDK (Internal)
socket.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
21 #pragma once
22 
26 #include <array>
27 #include <memory>
28 
29 #define SOCKET_SUCCEEDED(ec) ( TTV_SUCCEEDED(ec) || (ec == TTV_EC_SOCKET_EWOULDBLOCK) )
30 #define SOCKET_FAILED(ec) ( TTV_FAILED(ec) && (ec != TTV_EC_SOCKET_EWOULDBLOCK) )
31 
32 namespace ttv
33 {
34  class ISocket;
35  class IWebSocket;
36  class ISocketFactory;
37  class IWebSocketFactory;
38  class BufferedSocket;
39 
51 
65 
77  TTV_ErrorCode RegisterSocketFactory(const std::shared_ptr<ISocketFactory>& factory);
78 
88  TTV_ErrorCode UnregisterSocketFactory(const std::shared_ptr<ISocketFactory>& factory);
89 
101  TTV_ErrorCode RegisterWebSocketFactory(const std::shared_ptr<IWebSocketFactory>& factory);
102 
112  TTV_ErrorCode UnregisterWebSocketFactory(const std::shared_ptr<IWebSocketFactory>& factory);
113 
125  TTV_ErrorCode IsSocketProtocolSupported(const std::string& protocol);
126 
138  TTV_ErrorCode IsWebSocketProtocolSupported(const std::string& protocol);
139 
155  TTV_ErrorCode CreateSocket(const std::string& uri, std::shared_ptr<ISocket>& result);
156 
172  TTV_ErrorCode CreateWebSocket(const std::string& uri, std::shared_ptr<IWebSocket>& result);
173 }
174 
175 
182 {
183 public:
187  virtual ~ISocket() = default;
188 
199  virtual TTV_ErrorCode Connect() = 0;
200 
209  virtual TTV_ErrorCode Disconnect() = 0;
210 
225  virtual TTV_ErrorCode Send(const uint8_t* buffer, size_t length);
226 
244  virtual TTV_ErrorCode Send(const uint8_t* buffer, size_t length, size_t& sent) = 0;
245 
261  virtual TTV_ErrorCode Recv(uint8_t* buffer, size_t length, size_t& received) = 0;
262 
268  virtual uint64_t TotalSent() = 0;
269 
275  virtual uint64_t TotalReceived() = 0;
276 
282  virtual bool Connected() = 0;
283 };
284 
285 
291 {
292 public:
296  enum class MessageType
297  {
299  None,
301  Binary,
303  Text,
305  Unknown
306  };
307 
308 public:
312  virtual ~IWebSocket() = default;
313 
324  virtual TTV_ErrorCode Connect() = 0;
325 
334  virtual TTV_ErrorCode Disconnect() = 0;
335 
351  virtual TTV_ErrorCode Send(MessageType type, const uint8_t* buffer, size_t length) = 0;
352 
372  virtual TTV_ErrorCode Recv(MessageType& type, uint8_t* buffer, size_t length, size_t& received) = 0;
373 
389  virtual TTV_ErrorCode Peek(MessageType& type, size_t& length) = 0;
390 
396  virtual bool Connected() = 0;
397 };
398 
399 
406 {
407 public:
411  virtual ~ISocketFactory() = default;
412 
420  virtual bool IsProtocolSupported(const std::string& protocol) = 0;
421 
432  virtual TTV_ErrorCode CreateSocket(const std::string& uri, std::shared_ptr<ISocket>& result) = 0;
433 };
434 
435 
443 {
444 public:
448  virtual ~IWebSocketFactory() = default;
449 
457  virtual bool IsProtocolSupported(const std::string& protocol) = 0;
458 
469  virtual TTV_ErrorCode CreateWebSocket(const std::string& uri, std::shared_ptr<IWebSocket>& result) = 0;
470 };
471 
472 
478 {
479 public:
480  BufferedSocket();
481  ~BufferedSocket();
482 
483  void Bind(const std::shared_ptr<ISocket>& socket);
484 
487 
494  TTV_ErrorCode Send(const uint8_t* buffer, size_t length, bool cache);
495  TTV_ErrorCode Recv(uint8_t* buffer, size_t length, size_t& received, uint64_t maxWaitForBufferFill);
496  TTV_ErrorCode FlushCache();
497  TTV_ErrorCode SetBlockingMode(bool blockingMode);
498 
499  uint64_t TotalSent();
500  uint64_t TotalReceived();
501 
505  TTV_ErrorCode GetAverageSendBitRate(uint64_t measurementWindow, uint64_t& bitsPerSecond) const;
506 
510  TTV_ErrorCode GetCongestionLevel(uint64_t measurementWindow, double& congestionLevel) const;
511 
512  bool Connected();
513 
514 private:
515  // Buffer up to 65k, which is the maximum size of a TCP packet.
516  static const size_t kMaxBufferSize = 0x10000;
517 
518  TTV_ErrorCode DoSend(const uint8_t* buffer, size_t length);
519 
520  std::shared_ptr<ISocket> mSocket;
521  uint64_t mLastFlushTime;
522  size_t mCachePos;
523  std::array<uint8_t, kMaxBufferSize> mCache;
524 
526 
527  bool mBlocking;
528 };
TTV_ErrorCode IsWebSocketProtocolSupported(const std::string &protocol)
TTV_ErrorCode ShutdownSocketLibrary()
TTV_ErrorCode CreateSocket(const std::string &uri, std::shared_ptr< ISocket > &result)
std::shared_ptr< ISocket > mSocket
Definition: socket.h:520
Definition: socket.h:442
virtual uint64_t TotalSent()=0
TTV_ErrorCode InitializeSocketLibrary()
TTV_ErrorCode RegisterSocketFactory(const std::shared_ptr< ISocketFactory > &factory)
virtual TTV_ErrorCode Disconnect()=0
Definition: socket.h:290
uint64_t mLastFlushTime
Definition: socket.h:521
virtual bool Connected()=0
TTV_ErrorCode CreateWebSocket(const std::string &uri, std::shared_ptr< IWebSocket > &result)
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
If neither Partner nor Affiliate.
Definition: socket.h:477
SocketTracker mTracker
Definition: socket.h:525
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
size_t mCachePos
Definition: socket.h:522
Definition: socket.h:181
Definition: socket.h:405
bool mBlocking
Definition: socket.h:527
Definition: sockettracker.h:22
std::array< uint8_t, kMaxBufferSize > mCache
Definition: socket.h:523
virtual TTV_ErrorCode Send(const uint8_t *buffer, size_t length)
TTV_ErrorCode UnregisterSocketFactory(const std::shared_ptr< ISocketFactory > &factory)
virtual uint64_t TotalReceived()=0
TTV_ErrorCode UnregisterWebSocketFactory(const std::shared_ptr< IWebSocketFactory > &factory)
TTV_ErrorCode RegisterWebSocketFactory(const std::shared_ptr< IWebSocketFactory > &factory)
TTV_ErrorCode IsSocketProtocolSupported(const std::string &protocol)
virtual ~ISocket()=default
virtual TTV_ErrorCode Connect()=0
MessageType
Definition: socket.h:296
virtual TTV_ErrorCode Recv(uint8_t *buffer, size_t length, size_t &received)=0