diff options
-rw-r--r-- | vdservice/vdi_port.h | 44 | ||||
-rw-r--r-- | vdservice/vdservice.cpp | 4 | ||||
-rw-r--r-- | vdservice/virtio_vdi_port.cpp (renamed from vdservice/vdi_port.cpp) | 28 |
3 files changed, 46 insertions, 30 deletions
diff --git a/vdservice/vdi_port.h b/vdservice/vdi_port.h index 8e057bb..2d2b2d2 100644 --- a/vdservice/vdi_port.h +++ b/vdservice/vdi_port.h @@ -38,27 +38,43 @@ typedef struct VDIPortBuffer { class VDIPort { public: - VDIPort(); - ~VDIPort(); - bool init(); - size_t ring_write(const void* buf, size_t size); - size_t write_ring_free_space(); - size_t ring_read(void* buf, size_t size); - size_t read_ring_size(); - size_t read_ring_continuous_remaining_size(); - unsigned get_num_events() { return 2; } - void fill_events(HANDLE *handle) { + virtual ~VDIPort() {} + virtual bool init() = 0; + virtual size_t ring_write(const void* buf, size_t size) = 0; + virtual size_t write_ring_free_space() = 0; + virtual size_t ring_read(void* buf, size_t size) = 0; + virtual size_t read_ring_size() = 0; + virtual size_t read_ring_continuous_remaining_size() = 0; + virtual unsigned get_num_events() = 0; + virtual void fill_events(HANDLE *handle) = 0; + virtual void handle_event(int event) = 0; + virtual int write() = 0; + virtual int read() = 0; +}; + +class VirtioVDIPort : public VDIPort { +public: + VirtioVDIPort(); + ~VirtioVDIPort(); + virtual bool init(); + virtual size_t ring_write(const void* buf, size_t size); + virtual size_t write_ring_free_space(); + virtual size_t ring_read(void* buf, size_t size); + virtual size_t read_ring_size(); + virtual size_t read_ring_continuous_remaining_size(); + virtual unsigned get_num_events() { return 2; } + virtual void fill_events(HANDLE *handle) { handle[0] = _write.overlap.hEvent; handle[1] = _read.overlap.hEvent; } - void handle_event(int event) { + virtual void handle_event(int event) { switch (event) { case 0: write_completion(); break; case 1: read_completion(); break; } } - int write(); - int read(); + virtual int write(); + virtual int read(); private: void write_completion(); @@ -66,7 +82,7 @@ private: int handle_error(); private: - static VDIPort* _singleton; + static VirtioVDIPort* _singleton; HANDLE _handle; VDIPortBuffer _write; VDIPortBuffer _read; diff --git a/vdservice/vdservice.cpp b/vdservice/vdservice.cpp index 72882ea..4c7785b 100644 --- a/vdservice/vdservice.cpp +++ b/vdservice/vdservice.cpp @@ -43,7 +43,7 @@ // This enum simplifies WaitForMultipleEvents for static // events, that is handles that are guranteed non NULL. // It doesn't include: -// VDIPort Handles - these are filled by an interface because +// VirtioVDIPort Handles - these are filled by an interface because // of variable handle number. // VDAgent handle - this can be 1 or 0 (NULL or not), so it is also added at // the end of VDService::_events @@ -434,7 +434,7 @@ bool VDService::execute() CloseHandle(pipe); return false; } - _vdi_port = new VDIPort(); + _vdi_port = new VirtioVDIPort(); if (!_vdi_port->init()) { delete _vdi_port; CloseHandle(pipe); diff --git a/vdservice/vdi_port.cpp b/vdservice/virtio_vdi_port.cpp index f4dfaeb..8a7cf19 100644 --- a/vdservice/vdi_port.cpp +++ b/vdservice/virtio_vdi_port.cpp @@ -26,9 +26,9 @@ #define MIN(a, b) ((a) > (b) ? (b) : (a)) -VDIPort* VDIPort::_singleton; +VirtioVDIPort* VirtioVDIPort::_singleton; -VDIPort::VDIPort() +VirtioVDIPort::VirtioVDIPort() : _handle (INVALID_HANDLE_VALUE) { ZeroMemory(&_write, offsetof(VDIPortBuffer, ring)); @@ -38,7 +38,7 @@ VDIPort::VDIPort() _singleton = this; } -VDIPort::~VDIPort() +VirtioVDIPort::~VirtioVDIPort() { if (_handle != INVALID_HANDLE_VALUE) { CloseHandle(_handle); @@ -51,7 +51,7 @@ VDIPort::~VDIPort() } } -bool VDIPort::init() +bool VirtioVDIPort::init() { _handle = CreateFile(VIOSERIAL_PORT_PATH, GENERIC_READ | GENERIC_WRITE , 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); @@ -72,12 +72,12 @@ bool VDIPort::init() return true; } -size_t VDIPort::write_ring_free_space() +size_t VirtioVDIPort::write_ring_free_space() { return (BUF_SIZE + _write.start - _write.end - 1) % BUF_SIZE; } -size_t VDIPort::ring_write(const void* buf, size_t size) +size_t VirtioVDIPort::ring_write(const void* buf, size_t size) { size_t free_size = (BUF_SIZE + _write.start - _write.end - 1) % BUF_SIZE; size_t n; @@ -98,7 +98,7 @@ size_t VDIPort::ring_write(const void* buf, size_t size) return size; } -int VDIPort::write() +int VirtioVDIPort::write() { int size; int ret; @@ -126,7 +126,7 @@ int VDIPort::write() return ret; } -void VDIPort::write_completion() +void VirtioVDIPort::write_completion() { DWORD bytes; @@ -142,12 +142,12 @@ void VDIPort::write_completion() _write.pending = false; } -size_t VDIPort::read_ring_size() +size_t VirtioVDIPort::read_ring_size() { return (BUF_SIZE + _read.end - _read.start) % BUF_SIZE; } -size_t VDIPort::read_ring_continuous_remaining_size() +size_t VirtioVDIPort::read_ring_continuous_remaining_size() { DWORD size; @@ -159,7 +159,7 @@ size_t VDIPort::read_ring_continuous_remaining_size() return size; } -size_t VDIPort::ring_read(void* buf, size_t size) +size_t VirtioVDIPort::ring_read(void* buf, size_t size) { size_t n; size_t m = 0; @@ -182,7 +182,7 @@ size_t VDIPort::ring_read(void* buf, size_t size) return n + m; } -int VDIPort::read() +int VirtioVDIPort::read() { int size; int ret; @@ -213,7 +213,7 @@ int VDIPort::read() return ret; } -void VDIPort::read_completion() +void VirtioVDIPort::read_completion() { DWORD bytes; @@ -227,7 +227,7 @@ void VDIPort::read_completion() _read.pending = false; } -int VDIPort::handle_error() +int VirtioVDIPort::handle_error() { switch (GetLastError()) { case ERROR_CONNECTION_INVALID: |