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
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
2018-10-01 14:46:11 -07:00
|
|
|
#include <atomic>
|
2019-03-02 21:11:16 -08:00
|
|
|
#include <vector>
|
2019-03-14 18:55:33 -07:00
|
|
|
#include <memory>
|
2018-09-27 14:56:48 -07:00
|
|
|
|
2019-01-05 21:02:55 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <BaseTsd.h>
|
|
|
|
typedef SSIZE_T ssize_t;
|
|
|
|
#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;
|
|
|
|
|
2019-01-24 12:42:49 -08:00
|
|
|
enum PollResultType
|
|
|
|
{
|
|
|
|
PollResultType_ReadyForRead = 0,
|
2019-03-18 14:25:27 -07:00
|
|
|
PollResultType_ReadyForWrite = 1,
|
|
|
|
PollResultType_Timeout = 2,
|
|
|
|
PollResultType_Error = 3,
|
|
|
|
PollResultType_SendRequest = 4,
|
|
|
|
PollResultType_CloseRequest = 5
|
2019-01-24 12:42:49 -08:00
|
|
|
};
|
|
|
|
|
2018-09-27 14:56:48 -07:00
|
|
|
class Socket {
|
|
|
|
public:
|
2019-01-24 12:42:49 -08:00
|
|
|
using OnPollCallback = std::function<void(PollResultType)>;
|
2018-09-27 14:56:48 -07:00
|
|
|
|
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
|
|
|
|
|
|
|
void configure();
|
|
|
|
|
2019-03-18 14:25:27 -07:00
|
|
|
// Functions to check whether there is activity on the socket
|
|
|
|
void poll(const OnPollCallback& onPollCallback,
|
|
|
|
int timeoutSecs = kDefaultPollTimeout);
|
|
|
|
bool wakeUpFromPoll(uint8_t wakeUpCode);
|
|
|
|
|
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-02-20 18:59:07 -08:00
|
|
|
virtual bool connect(const std::string& url,
|
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);
|
|
|
|
virtual ssize_t send(const std::string& buffer);
|
|
|
|
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.
|
|
|
|
bool readByte(void* buffer,
|
|
|
|
const CancellationRequest& isCancellationRequested);
|
|
|
|
bool writeBytes(const std::string& str,
|
|
|
|
const CancellationRequest& isCancellationRequested);
|
2019-03-02 11:01:51 -08:00
|
|
|
|
|
|
|
std::pair<bool, std::string> readLine(
|
|
|
|
const CancellationRequest& isCancellationRequested);
|
|
|
|
std::pair<bool, std::string> readBytes(
|
|
|
|
size_t length,
|
2019-03-02 21:11:16 -08:00
|
|
|
const OnProgressCallback& onProgressCallback,
|
2019-03-02 11:01:51 -08:00
|
|
|
const CancellationRequest& isCancellationRequested);
|
2019-01-02 07:45:07 -08:00
|
|
|
|
2019-01-04 17:28:13 -08:00
|
|
|
static int getErrno();
|
2018-10-08 21:42:45 -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:
|
2018-10-08 21:42:45 -07:00
|
|
|
void closeSocket(int fd);
|
|
|
|
|
2018-09-27 14:56:48 -07:00
|
|
|
std::atomic<int> _sockfd;
|
|
|
|
std::mutex _socketMutex;
|
2019-01-24 12:42:49 -08:00
|
|
|
|
|
|
|
private:
|
2019-03-18 22:00:08 -07:00
|
|
|
PollResultType select(bool readyToRead, int timeoutMs);
|
|
|
|
|
2019-01-24 12:42:49 -08:00
|
|
|
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
|
|
|
|
2019-03-14 18:37:38 -07:00
|
|
|
std::shared_ptr<SelectInterrupt> _selectInterrupt;
|
2018-09-27 14:56:48 -07:00
|
|
|
};
|
|
|
|
}
|