2018-09-27 23:56:48 +02: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 23:46:11 +02:00
|
|
|
#include <atomic>
|
2019-03-03 06:11:16 +01:00
|
|
|
#include <vector>
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-01-06 06:02:55 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <BaseTsd.h>
|
|
|
|
typedef SSIZE_T ssize_t;
|
|
|
|
#endif
|
|
|
|
|
2018-12-15 01:28:11 +01:00
|
|
|
#include "IXCancellationRequest.h"
|
2019-03-03 06:11:16 +01:00
|
|
|
#include "IXProgressCallback.h"
|
2019-03-14 07:09:45 +01:00
|
|
|
#include "IXEventFd.h"
|
2018-11-02 01:02:49 +01:00
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
namespace ix
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2019-01-24 21:42:49 +01:00
|
|
|
enum PollResultType
|
|
|
|
{
|
|
|
|
PollResultType_ReadyForRead = 0,
|
|
|
|
PollResultType_Timeout = 1,
|
2019-03-14 07:09:45 +01:00
|
|
|
PollResultType_Error = 2,
|
|
|
|
PollResultType_SendRequest = 3,
|
|
|
|
PollResultType_CloseRequest = 4
|
2019-01-24 21:42:49 +01:00
|
|
|
};
|
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
class Socket {
|
|
|
|
public:
|
2019-01-24 21:42:49 +01:00
|
|
|
using OnPollCallback = std::function<void(PollResultType)>;
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2018-12-30 06:53:33 +01:00
|
|
|
Socket(int fd = -1);
|
2018-09-27 23:56:48 +02:00
|
|
|
virtual ~Socket();
|
|
|
|
|
|
|
|
void configure();
|
|
|
|
|
2019-03-14 07:09:45 +01:00
|
|
|
PollResultType select(int timeoutSecs, int timeoutMs);
|
2019-01-24 21:42:49 +01:00
|
|
|
virtual void poll(const OnPollCallback& onPollCallback,
|
|
|
|
int timeoutSecs = kDefaultPollTimeout);
|
2019-03-14 07:09:45 +01:00
|
|
|
virtual bool wakeUpFromPoll(uint8_t wakeUpCode);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
// Virtual methods
|
2019-02-21 03:59:07 +01:00
|
|
|
virtual bool connect(const std::string& url,
|
2018-09-27 23:56:48 +02:00
|
|
|
int port,
|
2018-12-10 02:56:20 +01:00
|
|
|
std::string& errMsg,
|
2018-12-15 01:28:11 +01:00
|
|
|
const CancellationRequest& isCancellationRequested);
|
2018-09-27 23:56:48 +02:00
|
|
|
virtual void close();
|
|
|
|
|
2019-01-06 05:53:50 +01: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 23:56:48 +02:00
|
|
|
|
2019-01-02 16:45:07 +01: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 20:01:51 +01:00
|
|
|
|
|
|
|
std::pair<bool, std::string> readLine(
|
|
|
|
const CancellationRequest& isCancellationRequested);
|
|
|
|
std::pair<bool, std::string> readBytes(
|
|
|
|
size_t length,
|
2019-03-03 06:11:16 +01:00
|
|
|
const OnProgressCallback& onProgressCallback,
|
2019-03-02 20:01:51 +01:00
|
|
|
const CancellationRequest& isCancellationRequested);
|
2019-01-02 16:45:07 +01:00
|
|
|
|
2019-01-05 02:28:13 +01:00
|
|
|
static int getErrno();
|
2018-10-09 06:42:45 +02:00
|
|
|
static bool init(); // Required on Windows to initialize WinSocket
|
|
|
|
static void cleanup(); // Required on Windows to cleanup WinSocket
|
|
|
|
|
2019-03-14 07:09:45 +01:00
|
|
|
// Used as special codes for pipe communication
|
|
|
|
static const uint8_t kSendRequest;
|
|
|
|
static const uint8_t kCloseRequest;
|
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
protected:
|
2018-10-09 06:42:45 +02:00
|
|
|
void closeSocket(int fd);
|
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
std::atomic<int> _sockfd;
|
|
|
|
std::mutex _socketMutex;
|
2019-01-24 21:42:49 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
static const int kDefaultPollTimeout;
|
|
|
|
static const int kDefaultPollNoTimeout;
|
2019-03-03 06:11:16 +01: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-14 07:09:45 +01:00
|
|
|
|
|
|
|
EventFd _eventfd;
|
2018-09-27 23:56:48 +02:00
|
|
|
};
|
|
|
|
}
|