summaryrefslogtreecommitdiff
path: root/src/uTPCore.h
blob: 838d050076f88e2d4b0669cf4452a9f84af1abcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef D_UTP_CORE_H
#define D_UTP_CORE_H

#include <memory>
#include <array>
#include <vector>

#include "utp.h"

#include "Command.h"
#include "wallclock.h"
#include "SocketCore.h"

namespace aria2 {

class DownloadEngine;

class uTPCore : public SocketCore {
private:
  utp_socket* s_;
  std::vector<char> buf_;
  size_t pos_, end_;
  int errNum_;

  bool isWritable_;
  bool needDrainRead_;

  bool readCheck_;
  bool writeCheck_;
  Command* command_;

  static utp_context* ctx_;
  static utp_context* ctx6_;
  static std::shared_ptr<SocketCore> sock_;
  static std::shared_ptr<SocketCore> sock6_;
  static Timer start_;

public:
  uTPCore(utp_socket* s = nullptr);
  virtual ~uTPCore();

  static void initContext(uint16_t port, DownloadEngine* e);
  static void destroyContext();

  virtual void establishConnection(const std::string& host, uint16_t port,
                                   bool tcpNodelay = true) CXX11_OVERRIDE;
  virtual bool isOpen() const CXX11_OVERRIDE { return s_ != nullptr; };
  virtual bool isWritable(time_t timeout) CXX11_OVERRIDE;
  virtual bool isReadable(time_t timeout) CXX11_OVERRIDE;
  virtual ssize_t writeData(const void* data, size_t len) CXX11_OVERRIDE;
  virtual ssize_t writeVector(a2iovec* iov, size_t iovcnt) CXX11_OVERRIDE;
  virtual void readData(void* data, size_t& len) CXX11_OVERRIDE;
  virtual void closeConnection() CXX11_OVERRIDE;
  virtual std::string getSocketError() const CXX11_OVERRIDE;

  void setRWCheck(Command* command, int is_add, int is_read);

  static uint64 getMilliseconds(utp_callback_arguments* arg);
  static uint64 getMicroseconds(utp_callback_arguments* arg);
  static uint64 getReadBufferSize(utp_callback_arguments* arg);

  static uint64 writeLogCallback(utp_callback_arguments* arg);
  static uint64 sendtoCallback(utp_callback_arguments* arg);
  static uint64 acceptCallback(utp_callback_arguments* arg);
  static uint64 stateChangeCallback(utp_callback_arguments* arg);
  static uint64 readCallback(utp_callback_arguments* arg);
  static uint64 errorCallback(utp_callback_arguments* arg);
};

class uTPListenCommand : public Command {
private:
  utp_context* ctx_;
  std::shared_ptr<SocketCore> sock_;
  DownloadEngine* e_;

public:
  uTPListenCommand(cuid_t cuid, DownloadEngine* e, utp_context* ctx,
                   std::shared_ptr<SocketCore> sock);

  virtual ~uTPListenCommand();

  virtual bool execute() CXX11_OVERRIDE;
};

} // namespace aria2

#endif // D_UTP_CORE_H