Skip to content
Repos

DevPlaceApp

C++ 1.0 KB43 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md FR-NET-01..06, NFR-THR-01. All HTTP(S) goes through aiohttpcpp on one worker thread.
// Callbacks run on the UI thread via UiQueue.
#include <memory>
#include <string>

#include "net/HttpTypes.hpp"

namespace dp {

class SocketSession;

class NetworkService {
 public:
  static NetworkService& instance();

  void start();
  void stop();

  // UI thread. The callback runs on the UI thread unless cancelled (then it never runs).
  CancelPtr send(HttpRequest request, HttpCallback done);

  // Offline state: true after a connector failure, cleared on the next success.
  bool offline() const { return offline_; }
  std::size_t pendingRequests() const;

  // Real network (test hook): override base URL, e.g. local aiohttpcpp server.
  void setBaseUrl(std::string url);
  const std::string& baseUrl() const { return baseUrl_; }

  struct Impl;

 private:
  NetworkService();
  ~NetworkService();
  friend class SocketSession;

  std::unique_ptr<Impl> impl_;
  std::string baseUrl_;
  bool offline_ = false;
};

}  // namespace dp
129.4ms