101 lines
3.1 KiB
C
Raw Normal View History

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>
#include <cstdint>
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>
2018-09-27 14:56:48 -07:00
2019-01-05 21:02:55 -08:00
#ifdef _WIN32
#include <basetsd.h>
#ifdef _MSC_VER
2019-01-05 21:02:55 -08:00
typedef SSIZE_T ssize_t;
#endif
#endif
2019-01-05 21:02:55 -08:00
2018-12-14 16:28:11 -08:00
#include "IXCancellationRequest.h"
#include "IXProgressCallback.h"
#include "IXSelectInterrupt.h"
namespace ix
2018-09-27 14:56:48 -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
// Functions to check whether there is activity on the socket
PollResultType poll(int timeoutMs = kDefaultPollTimeout);
bool wakeUpFromPoll(uint64_t wakeUpCode);
bool isWakeUpFromPollSupported();
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
virtual bool accept(std::string& errMsg);
virtual bool connect(const std::string& host,
2018-09-27 14:56:48 -07:00
int port,
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();
virtual ssize_t send(char* buffer, size_t length);
2020-01-12 22:30:16 -08:00
ssize_t send(const std::string& buffer);
virtual ssize_t recv(void* buffer, size_t length);
2018-09-27 14:56:48 -07: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 OnChunkCallback& onChunkCallback,
2019-05-30 08:46:50 -07:00
const CancellationRequest& isCancellationRequested);
2019-01-04 17:28:13 -08:00
static int getErrno();
2019-05-06 19:13:42 +03:00
static bool isWaitNeeded();
static void closeSocket(int fd);
2018-10-08 21:42:45 -07:00
static PollResultType poll(bool readyToRead,
int timeoutMs,
int sockfd,
const SelectInterruptPtr& selectInterrupt);
2018-09-27 14:56:48 -07:00
protected:
std::atomic<int> _sockfd;
std::mutex _socketMutex;
2019-01-24 12:42:49 -08:00
static bool readSelectInterruptRequest(const SelectInterruptPtr& selectInterrupt,
PollResultType* pollResult);
2019-01-24 12:42:49 -08:00
private:
static const int kDefaultPollTimeout;
static const int kDefaultPollNoTimeout;
SelectInterruptPtr _selectInterrupt;
2018-09-27 14:56:48 -07:00
};
2019-05-30 08:46:50 -07:00
} // namespace ix