2018-09-27 14:56:48 -07:00
|
|
|
/*
|
|
|
|
* IXSocket.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-05-30 08:46:50 -07:00
|
|
|
#include <atomic>
|
2018-09-27 14:56:48 -07:00
|
|
|
#include <functional>
|
2019-05-30 08:46:50 -07:00
|
|
|
#include <memory>
|
2018-09-27 14:56:48 -07:00
|
|
|
#include <mutex>
|
2019-05-30 08:46:50 -07:00
|
|
|
#include <string>
|
2019-03-02 21:11:16 -08:00
|
|
|
#include <vector>
|
2018-09-27 14:56:48 -07:00
|
|
|
|
2020-08-05 14:47:03 -07:00
|
|
|
// For kqueue
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-05 21:02:55 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <BaseTsd.h>
|
|
|
|
typedef SSIZE_T ssize_t;
|
2019-05-06 22:22:57 +03:00
|
|
|
|
|
|
|
#undef EWOULDBLOCK
|
|
|
|
#undef EAGAIN
|
|
|
|
#undef EINPROGRESS
|
|
|
|
#undef EBADF
|
|
|
|
#undef EINVAL
|
|
|
|
|
|
|
|
// map to WSA error codes
|
2019-05-30 08:46:50 -07:00
|
|
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
|
|
|
#define EAGAIN WSATRY_AGAIN
|
|
|
|
#define EINPROGRESS WSAEINPROGRESS
|
|
|
|
#define EBADF WSAEBADF
|
|
|
|
#define EINVAL WSAEINVAL
|
2019-05-06 22:22:57 +03:00
|
|
|
|
2019-01-05 21:02:55 -08:00
|
|
|
#endif
|
|
|
|
|
2018-12-14 16:28:11 -08:00
|
|
|
#include "IXCancellationRequest.h"
|
2019-03-02 21:11:16 -08:00
|
|
|
#include "IXProgressCallback.h"
|
2018-11-01 17:02:49 -07:00
|
|
|
|
2019-02-20 18:59:07 -08:00
|
|
|
namespace ix
|
2018-09-27 14:56:48 -07:00
|
|
|
{
|
2019-03-14 18:37:38 -07:00
|
|
|
class SelectInterrupt;
|
2020-03-24 10:00:41 -07:00
|
|
|
using SelectInterruptPtr = std::unique_ptr<SelectInterrupt>;
|
2019-03-14 18:37:38 -07:00
|
|
|
|
2019-03-19 09:29:57 -07:00
|
|
|
enum class PollResultType
|
2019-01-24 12:42:49 -08:00
|
|
|
{
|
2019-05-30 08:46:50 -07:00
|
|
|
ReadyForRead = 0,
|
|
|
|
ReadyForWrite = 1,
|
|
|
|
Timeout = 2,
|
|
|
|
Error = 3,
|
|
|
|
SendRequest = 4,
|
|
|
|
CloseRequest = 5
|
2019-01-24 12:42:49 -08:00
|
|
|
};
|
|
|
|
|
2019-05-30 08:46:50 -07:00
|
|
|
class Socket
|
|
|
|
{
|
2018-09-27 14:56:48 -07:00
|
|
|
public:
|
2018-12-29 21:53:33 -08:00
|
|
|
Socket(int fd = -1);
|
2018-09-27 14:56:48 -07:00
|
|
|
virtual ~Socket();
|
2019-03-14 18:37:38 -07:00
|
|
|
bool init(std::string& errorMsg);
|
2018-09-27 14:56:48 -07:00
|
|
|
|
2019-03-18 14:25:27 -07:00
|
|
|
// Functions to check whether there is activity on the socket
|
2019-05-09 18:21:05 +02:00
|
|
|
PollResultType poll(int timeoutMs = kDefaultPollTimeout);
|
2019-05-13 22:16:49 -07:00
|
|
|
bool wakeUpFromPoll(uint64_t wakeUpCode);
|
2019-03-18 14:25:27 -07:00
|
|
|
|
2019-03-18 17:54:51 -07:00
|
|
|
PollResultType isReadyToWrite(int timeoutMs);
|
|
|
|
PollResultType isReadyToRead(int timeoutMs);
|
2018-09-27 14:56:48 -07:00
|
|
|
|
|
|
|
// Virtual methods
|
2019-10-01 15:43:37 -07:00
|
|
|
virtual bool accept(std::string& errMsg);
|
|
|
|
|
2020-03-22 19:36:29 -07:00
|
|
|
virtual bool connect(const std::string& host,
|
2018-09-27 14:56:48 -07:00
|
|
|
int port,
|
2018-12-09 17:56:20 -08:00
|
|
|
std::string& errMsg,
|
2018-12-14 16:28:11 -08:00
|
|
|
const CancellationRequest& isCancellationRequested);
|
2018-09-27 14:56:48 -07:00
|
|
|
virtual void close();
|
|
|
|
|
2019-01-05 20:53:50 -08:00
|
|
|
virtual ssize_t send(char* buffer, size_t length);
|
2020-01-12 22:30:16 -08:00
|
|
|
ssize_t send(const std::string& buffer);
|
2019-01-05 20:53:50 -08:00
|
|
|
virtual ssize_t recv(void* buffer, size_t length);
|
2018-09-27 14:56:48 -07:00
|
|
|
|
2019-01-02 07:45:07 -08:00
|
|
|
// Blocking and cancellable versions, working with socket that can be set
|
|
|
|
// to non blocking mode. Used during HTTP upgrade.
|
2019-05-30 08:46:50 -07:00
|
|
|
bool readByte(void* buffer, const CancellationRequest& isCancellationRequested);
|
|
|
|
bool writeBytes(const std::string& str, const CancellationRequest& isCancellationRequested);
|
|
|
|
|
|
|
|
std::pair<bool, std::string> readLine(const CancellationRequest& isCancellationRequested);
|
|
|
|
std::pair<bool, std::string> readBytes(size_t length,
|
|
|
|
const OnProgressCallback& onProgressCallback,
|
|
|
|
const CancellationRequest& isCancellationRequested);
|
2019-01-02 07:45:07 -08:00
|
|
|
|
2019-01-04 17:28:13 -08:00
|
|
|
static int getErrno();
|
2019-05-06 19:13:42 +03:00
|
|
|
static bool isWaitNeeded();
|
2019-05-06 22:22:57 +03:00
|
|
|
static void closeSocket(int fd);
|
2018-10-08 21:42:45 -07:00
|
|
|
|
2019-06-25 15:41:39 -07:00
|
|
|
static PollResultType poll(bool readyToRead,
|
|
|
|
int timeoutMs,
|
|
|
|
int sockfd,
|
2020-03-24 10:00:41 -07:00
|
|
|
const SelectInterruptPtr& selectInterrupt);
|
2019-06-25 15:41:39 -07:00
|
|
|
|
|
|
|
|
2019-03-13 23:09:45 -07:00
|
|
|
// Used as special codes for pipe communication
|
2019-03-14 15:03:57 -07:00
|
|
|
static const uint64_t kSendRequest;
|
|
|
|
static const uint64_t kCloseRequest;
|
2019-03-13 23:09:45 -07:00
|
|
|
|
2018-09-27 14:56:48 -07:00
|
|
|
protected:
|
|
|
|
std::atomic<int> _sockfd;
|
|
|
|
std::mutex _socketMutex;
|
2019-01-24 12:42:49 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
static const int kDefaultPollTimeout;
|
|
|
|
static const int kDefaultPollNoTimeout;
|
2019-03-02 21:11:16 -08:00
|
|
|
|
|
|
|
// Buffer for reading from our socket. That buffer is never resized.
|
|
|
|
std::vector<uint8_t> _readBuffer;
|
|
|
|
static constexpr size_t kChunkSize = 1 << 15;
|
2019-03-13 23:09:45 -07:00
|
|
|
|
2020-03-24 10:00:41 -07:00
|
|
|
SelectInterruptPtr _selectInterrupt;
|
2020-08-05 14:47:03 -07:00
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
int _kqueuefd;
|
|
|
|
#endif
|
2018-09-27 14:56:48 -07:00
|
|
|
};
|
2019-05-30 08:46:50 -07:00
|
|
|
} // namespace ix
|