1f2895a469
* Fix #323: Missing SelectInterrupt implementation for Windows Using WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents to emulate poll() with an interrupt-event. * Cleanup * Fixed incomplete comment. * Switched ifdefs to support other Unixes with pipe file descriptors * Fixed: SelectInterrupt fallback code for getFd()==-1 && getEvent()==nullptr converted a PollResultType::Timeout into a ReadyForRead causing the HttpClient to fail because it uses a hard-coded "SelectInterrupt" instance that doesn't implement getFd() and getEvent(). * Fixed gcc compile errors * - HttpClient now uses the SelectInterruptFactory - Fixed wrong ix::poll result when using Windows WSA functions * We must deselect the networkevents from the socket event. Otherwise the socket will report states that aren't there.
97 lines
3.0 KiB
C++
97 lines
3.0 KiB
C++
/*
|
|
* IXSocket.h
|
|
* Author: Benjamin Sergeant
|
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
|
|
#ifdef _WIN32
|
|
#include <basetsd.h>
|
|
typedef SSIZE_T ssize_t;
|
|
#endif
|
|
|
|
#include "IXCancellationRequest.h"
|
|
#include "IXProgressCallback.h"
|
|
#include "IXSelectInterrupt.h"
|
|
|
|
namespace ix
|
|
{
|
|
enum class PollResultType
|
|
{
|
|
ReadyForRead = 0,
|
|
ReadyForWrite = 1,
|
|
Timeout = 2,
|
|
Error = 3,
|
|
SendRequest = 4,
|
|
CloseRequest = 5
|
|
};
|
|
|
|
class Socket
|
|
{
|
|
public:
|
|
Socket(int fd = -1);
|
|
virtual ~Socket();
|
|
bool init(std::string& errorMsg);
|
|
|
|
// Functions to check whether there is activity on the socket
|
|
PollResultType poll(int timeoutMs = kDefaultPollTimeout);
|
|
bool wakeUpFromPoll(uint64_t wakeUpCode);
|
|
bool isWakeUpFromPollSupported();
|
|
|
|
PollResultType isReadyToWrite(int timeoutMs);
|
|
PollResultType isReadyToRead(int timeoutMs);
|
|
|
|
// Virtual methods
|
|
virtual bool accept(std::string& errMsg);
|
|
|
|
virtual bool connect(const std::string& host,
|
|
int port,
|
|
std::string& errMsg,
|
|
const CancellationRequest& isCancellationRequested);
|
|
virtual void close();
|
|
|
|
virtual ssize_t send(char* buffer, size_t length);
|
|
ssize_t send(const std::string& buffer);
|
|
virtual ssize_t recv(void* buffer, size_t length);
|
|
|
|
// 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);
|
|
|
|
std::pair<bool, std::string> readLine(const CancellationRequest& isCancellationRequested);
|
|
std::pair<bool, std::string> readBytes(size_t length,
|
|
const OnProgressCallback& onProgressCallback,
|
|
const CancellationRequest& isCancellationRequested);
|
|
|
|
static int getErrno();
|
|
static bool isWaitNeeded();
|
|
static void closeSocket(int fd);
|
|
|
|
static PollResultType poll(bool readyToRead,
|
|
int timeoutMs,
|
|
int sockfd,
|
|
const SelectInterruptPtr& selectInterrupt);
|
|
|
|
protected:
|
|
std::atomic<int> _sockfd;
|
|
std::mutex _socketMutex;
|
|
|
|
static bool readSelectInterruptRequest(const SelectInterruptPtr& selectInterrupt,
|
|
PollResultType* pollResult);
|
|
|
|
private:
|
|
static const int kDefaultPollTimeout;
|
|
static const int kDefaultPollNoTimeout;
|
|
|
|
SelectInterruptPtr _selectInterrupt;
|
|
};
|
|
} // namespace ix
|