forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.hpp
75 lines (69 loc) · 1.76 KB
/
device.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// solid/system/device.hpp
//
// Copyright (c) 2007, 2008 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#ifndef SYSTEM_DEVICE_HPP
#define SYSTEM_DEVICE_HPP
#ifdef SOLID_ON_WINDOWS
#include <WinSock2.h>
#include <Ws2tcpip.h>
#include <Windows.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include "solid/system/common.hpp"
namespace solid{
//! A wrapper for what on POSIX is a descriptor
class Device{
public:
#ifdef SOLID_ON_WINDOWS
typedef HANDLE DescriptorT;
#else
typedef int DescriptorT;
#endif
static const DescriptorT invalidDescriptor(){
#ifdef SOLID_ON_WINDOWS
return INVALID_HANDLE_VALUE;
#else
return -1;
#endif
}
Device(Device &&_dev);
//! The copy constructor which will grab the desc from the given device (like std::autoptr)
Device(DescriptorT _desc = invalidDescriptor());
~Device();
//! Read call
int read(char *_pb, size_t _bl);
//! Write call
int write(const char* _pb, size_t _bl);
//! Cancels existing io operations
bool cancel();
//! Close the device
void close();
//! Flush the device
void flush();
//! Check if the device is valid
bool ok()const{return desc != invalidDescriptor();}
Device& operator=(Device &&_dev);
//! The native descriptor associated to the socket
DescriptorT descriptor()const;
protected:
void descriptor(DescriptorT _desc);
private:
Device(const Device &_dev);
Device& operator=(const Device &_dev);
DescriptorT desc;
};
inline Device::DescriptorT Device::descriptor()const{return desc;}
inline void Device::descriptor(DescriptorT _desc){
desc = _desc;
}
}//namespace solid
#endif