Skip to content

Commit

Permalink
Merge ea691aa into 2779984
Browse files Browse the repository at this point in the history
  • Loading branch information
nightrune committed Dec 12, 2014
2 parents 2779984 + ea691aa commit 6c7d7b9
Show file tree
Hide file tree
Showing 35 changed files with 182 additions and 54 deletions.
2 changes: 1 addition & 1 deletion common/base/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#else
#include <sys/resource.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion common/base/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#ifdef _WIN32
#define VC_EXTRALEAN
#include <Windows.h>
#include <ola/win/CleanWindows.h>
#include <io.h>
#else
#include <syslog.h>
Expand Down
2 changes: 1 addition & 1 deletion common/file/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <string.h>
#ifdef _WIN32
#define VC_EXTRALEAN
#include <Windows.h>
#include <ola/win/CleanWindows.h>
#endif

#if HAVE_CONFIG_H
Expand Down
2 changes: 1 addition & 1 deletion common/http/HTTPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <ola/web/JsonWriter.h>

#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#endif

#include <fstream>
Expand Down
42 changes: 36 additions & 6 deletions common/io/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#endif

#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#include <Winioctl.h>
#else
#include <sys/ioctl.h>
Expand Down Expand Up @@ -136,9 +136,9 @@ int ToFD(const DescriptorHandle &handle) {
}

/**
* Helper function to create a annonymous pipe
* Helper function to create a anonymous pipe
* @param handle_pair a 2 element array which is updated with the handles
* @return true if successfull, false otherwise.
* @return true if successful, false otherwise.
*/
bool CreatePipe(DescriptorHandle handle_pair[2]) {
#ifdef _WIN32
Expand Down Expand Up @@ -207,7 +207,6 @@ bool CreatePipe(DescriptorHandle handle_pair[2]) {
}



// BidirectionalFileDescriptor
// ------------------------------------------------
void BidirectionalFileDescriptor::PerformRead() {
Expand Down Expand Up @@ -323,7 +322,8 @@ ssize_t ConnectedDescriptor::Send(const uint8_t *buffer,

ssize_t bytes_sent;
#ifdef _WIN32
if (WriteDescriptor().m_type == PIPE_DESCRIPTOR) {
if (WriteDescriptor().m_type == PIPE_DESCRIPTOR ||
WriteDescriptor().m_type == SERIAL_DESCRIPTOR) {
DWORD bytes_written = 0;
if (!WriteFile(ToHandle(WriteDescriptor()),
buffer,
Expand Down Expand Up @@ -431,7 +431,8 @@ int ConnectedDescriptor::Receive(uint8_t *buffer,

while (data_read < size) {
#ifdef _WIN32
if (ReadDescriptor().m_type == PIPE_DESCRIPTOR) {
if (ReadDescriptor().m_type == PIPE_DESCRIPTOR ||
ReadDescriptor().m_type == SERIAL_DESCRIPTOR) {
if (!ReadDescriptor().m_async_data_size) {
OLA_WARN << "No async data buffer for descriptor " << ReadDescriptor();
return -1;
Expand Down Expand Up @@ -730,5 +731,34 @@ bool DeviceDescriptor::Close() {
m_handle = INVALID_DESCRIPTOR;
return ret == 0;
}


#ifdef _WIN32
bool WindowsSerialDescriptor::Init(const std::string &path) {
if (m_handle != INVALID_DESCRIPTOR) {
return false;
}

HANDLE serial_file = CreateFile(path.c_str(), GENERIC_READ | GENERIC_WRITE,
0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);

if (!serial_file) {
// error opening port; abort
OLA_WARN << "Could not open device: " << path;
return false;
}

m_handle.m_type = SERIAL_DESCRIPTOR;
m_handle.m_handle.m_handle = serial_file;
return true;
}

bool WindowsSerialDescriptor::Close() {
if (m_handle != INVALID_DESCRIPTOR) {
CloseHandle(ToHandle(m_handle));
}
return true;
}
#endif // _WIN32
} // namespace io
} // namespace ola
2 changes: 1 addition & 1 deletion common/io/DescriptorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <string>

#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#endif

#include "ola/Callback.h"
Expand Down
2 changes: 1 addition & 1 deletion common/io/SelectPoller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#ifdef _WIN32
// Pull in fd_set and related definitions.
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#endif

#include "common/io/SelectPoller.h"
Expand Down
2 changes: 1 addition & 1 deletion common/io/SelectServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "ola/io/SelectServer.h"

#ifdef _WIN32
#include <winsock2.h>
#include <ola/win/CleanWinSock2.h>
#else
#include <sys/select.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion common/io/SelectServerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#endif

#include <cppunit/extensions/HelperMacros.h>
Expand Down
9 changes: 6 additions & 3 deletions common/io/WindowsPoller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>


#include <algorithm>
Expand Down Expand Up @@ -185,7 +185,8 @@ bool WindowsPoller::AddWriteDescriptor(WriteFileDescriptor *descriptor) {
}

if ((descriptor->WriteDescriptor().m_type != SOCKET_DESCRIPTOR) &&
(descriptor->WriteDescriptor().m_type != PIPE_DESCRIPTOR)) {
(descriptor->WriteDescriptor().m_type != PIPE_DESCRIPTOR) &&
(descriptor->WriteDescriptor().m_type != SERIAL_DESCRIPTOR)) {
OLA_WARN << "Cannot add descriptor " << descriptor << " for writing.";
return false;
}
Expand Down Expand Up @@ -321,6 +322,7 @@ bool WindowsPoller::Poll(TimeoutManager *timeout_manager,
EventHolder* event_holder;

switch (descriptor->type) {
case SERIAL_DESCRIPTOR:
case PIPE_DESCRIPTOR:
if (descriptor->connected_descriptor) {
descriptor_handle =
Expand Down Expand Up @@ -514,7 +516,8 @@ bool WindowsPoller::Poll(TimeoutManager *timeout_manager,
if (!descriptor->connected_descriptor) {
continue;
}
if (descriptor->type != PIPE_DESCRIPTOR) {
if (descriptor->type != PIPE_DESCRIPTOR &&
descriptor->type != SERIAL_DESCRIPTOR) {
continue;
}
DescriptorHandle handle =
Expand Down
2 changes: 1 addition & 1 deletion common/io/WindowsPoller.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <ola/io/Descriptor.h>

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <ola/win/CleanWindows.h>

#include <map>
#include <utility>
Expand Down
2 changes: 1 addition & 1 deletion common/network/IPV4Address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif

#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#include <ola/win/CleanWinSock2.h>
#ifndef in_addr_t
#define in_addr_t uint32_t
#endif
Expand Down
2 changes: 1 addition & 1 deletion common/network/IPV4AddressTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <string>
#include <vector>
#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#ifndef in_addr_t
#define in_addr_t uint32_t
#endif
Expand Down
2 changes: 1 addition & 1 deletion common/network/MACAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#endif

#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
struct ether_addr {
unsigned char octet[ola::network::MACAddress::LENGTH];
};
Expand Down
2 changes: 1 addition & 1 deletion common/network/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
typedef uint32_t in_addr_t;
// Iphlpapi.h depends on Winsock2.h
#define WIN_32_LEAN_AND_MEAN
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#include <Iphlpapi.h>
#else
#include <netinet/in.h>
Expand Down
2 changes: 1 addition & 1 deletion common/network/NetworkUtilsInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#ifdef HAVE_WINSOCK2_H
#define VC_EXTRALEAN
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#endif

#ifdef HAVE_ARPA_INET_H
Expand Down
2 changes: 1 addition & 1 deletion common/network/NetworkUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <cppunit/extensions/HelperMacros.h>

#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#endif

#include <string>
Expand Down
2 changes: 1 addition & 1 deletion common/network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#endif

#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#include <Ws2tcpip.h>
#include <winioctl.h>
#include <Mswsock.h>
Expand Down
2 changes: 1 addition & 1 deletion common/network/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <unistd.h>

#ifdef _WIN32
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#include <Ws2tcpip.h>
#include <winioctl.h>
#else
Expand Down
2 changes: 1 addition & 1 deletion common/network/WindowsInterfacePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

typedef int socklen_t;
#include <winsock2.h>
#include <ola/win/CleanWinSock2.h>
#include <Lm.h>
#include <iphlpapi.h>
#include <unistd.h>
Expand Down
2 changes: 1 addition & 1 deletion common/utils/ClockTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
#include <ola/win/CleanWindows.h>
#endif

#include "ola/Clock.h"
Expand Down
2 changes: 1 addition & 1 deletion include/ola/http/HTTPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <stdlib.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#else
#include <sys/select.h>
#include <sys/socket.h>
Expand Down
26 changes: 25 additions & 1 deletion include/ola/io/Descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ namespace io {
enum DescriptorType {
GENERIC_DESCRIPTOR = 0, // Catch-all type without special handling
SOCKET_DESCRIPTOR, // WinSock socket
PIPE_DESCRIPTOR // Named Pipe handle
PIPE_DESCRIPTOR, // Named Pipe handle
SERIAL_DESCRIPTOR // Windows Serial Descriptor
};

// Consider this to be an opaque type.
Expand Down Expand Up @@ -561,6 +562,29 @@ class DeviceDescriptor: public ConnectedDescriptor {
DISALLOW_COPY_AND_ASSIGN(DeviceDescriptor);
};


#ifdef _WIN32
class WindowsSerialDescriptor : public ConnectedDescriptor {

public:
explicit WindowsSerialDescriptor() : m_handle(INVALID_DESCRIPTOR) {}
~WindowsSerialDescriptor() { Close(); }

bool Init(const std::string &path);
bool Close();

DescriptorHandle ReadDescriptor() const { return m_handle; }
DescriptorHandle WriteDescriptor() const { return m_handle; }

protected:
bool IsSocket() const { return false; }

private:
DescriptorHandle m_handle;
DISALLOW_COPY_AND_ASSIGN(WindowsSerialDescriptor);
};
#endif

/**@}*/
} // namespace io
} // namespace ola
Expand Down
2 changes: 1 addition & 1 deletion include/ola/network/SocketAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <stdint.h>
#ifdef _WIN32
#define VC_EXTRALEAN
#include <Winsock2.h>
#include <ola/win/CleanWinSock2.h>
#else
#include <sys/socket.h>
#endif
Expand Down
36 changes: 36 additions & 0 deletions include/ola/win/CleanWinSock2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* CleanWinSock2.h
* A common header that removes many of the name-space clutter that windows.h
* and WinSock2.h creates
* Copyright (C) 2014 Sean Sill
*/

#ifndef INCLUDE_OLA_CLEANWINSOCK2_H_
#define INCLUDE_OLA_CLEANWINSOCK2_H_

#ifdef _WIN32
#include <Winsock2.h>
// Some preprocessor magic to reduce Windows.h namespace pollution
# ifdef AddPort
# undef AddPort
# endif
# ifdef SendMessage
# undef SendMessage
# endif
#endif // _WIN32
#endif /* INCLUDE_OLA_CLEANWINSOCK2_H_ */

Loading

0 comments on commit 6c7d7b9

Please sign in to comment.