Skip to content

Commit

Permalink
Changes for windows
Browse files Browse the repository at this point in the history
Changes for windows:
1. Do not build usock_server on windows
2. Use uint8_t instead of u_int8_t
3. Added cast in setsocktopt
4. Changed size_t to std::size_t in some methods (to match to
declaration)

Change-Id: I1336413e541e25ee95c29074f29aa6952e0f5d97
Partial-Bug: #1737177
  • Loading branch information
Pawel Marchewka committed Dec 15, 2017
1 parent d3f0ce1 commit 442663f
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 39 deletions.
32 changes: 18 additions & 14 deletions io/SConscript
Expand Up @@ -3,7 +3,7 @@
#

# -*- mode: python; -*-

import sys
Import('BuildEnv')

env = BuildEnv.Clone()
Expand All @@ -16,13 +16,26 @@ SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)
except_env = BuildEnv.Clone()
except_env.Append(CPPPATH = env['TOP_INCLUDE'] + '/thrift')
except_env.Append(CPPPATH = [env['TOP']])
EventManagerSrc = except_env.Object('event_manager.o', 'event_manager.cc')
SslServerSrc = except_env.Object('ssl_server.o', 'ssl_server.cc')
EventManagerSrc = except_env.Object('event_manager.cc')
SslServerSrc = except_env.Object('ssl_server.cc')

usock_env = BuildEnv.Clone()
usock_env.CppEnableExceptions()
usock_env.Append(CPPPATH = env['TOP'])
usock_server = usock_env.Object('usock_server.o', 'usock_server.cc')
usock_server = usock_env.Object('usock_server.cc')

IoSrc = [
'io_utils.cc',
'ssl_session.cc',
'tcp_message_write.cc',
'tcp_server.cc',
'tcp_session.cc',
'udp_server.cc',
'process_signal.cc',
]

if sys.platform != 'win32':
IoSrc.append(usock_server)

# IO
contrail_common_io_doc_files = []
Expand All @@ -35,15 +48,6 @@ libio = env.Library('io',
SandeshGenSrcs +
EventManagerSrc +
SslServerSrc +
[
'io_utils.cc',
'ssl_session.cc',
'tcp_message_write.cc',
'tcp_server.cc',
'tcp_session.cc',
'udp_server.cc',
'usock_server',
'process_signal.cc',
])
IoSrc)
env.Install(env['TOP_LIB'], libio)
env.SConscript('test/SConscript', exports='BuildEnv', duplicate = 0)
2 changes: 1 addition & 1 deletion io/ssl_session.cc
Expand Up @@ -158,7 +158,7 @@ size_t SslSession::WriteSome(const uint8_t *data, size_t len,
}
}

void SslSession::AsyncWrite(const u_int8_t *data, size_t size) {
void SslSession::AsyncWrite(const uint8_t *data, size_t size) {
if (IsSslHandShakeSuccessLocked()) {
async_write(*ssl_socket_.get(), buffer(data, size),
bind(&TcpSession::AsyncWriteHandler,
Expand Down
2 changes: 1 addition & 1 deletion io/ssl_session.h
Expand Up @@ -69,7 +69,7 @@ class SslSession : public TcpSession {
boost::system::error_code *error);
std::size_t WriteSome(const uint8_t *data, std::size_t len,
boost::system::error_code *error);
void AsyncWrite(const u_int8_t *data, std::size_t size);
void AsyncWrite(const uint8_t *data, std::size_t size);

static void TriggerSslHandShakeInternal(SslSessionPtr ptr,
SslHandShakeCallbackHandler cb);
Expand Down
2 changes: 1 addition & 1 deletion io/tcp_message_write.cc
Expand Up @@ -172,7 +172,7 @@ void TcpMessageWriter::HandleWriteReady(error_code *error) {
}

void TcpMessageWriter::BufferAppend(const uint8_t *src, int bytes) {
u_int8_t *data = new u_int8_t[bytes];
uint8_t *data = new uint8_t[bytes];
memcpy(data, src, bytes);
mutable_buffer buffer = mutable_buffer(data, bytes);
buffer_queue_.push_back(buffer);
Expand Down
3 changes: 2 additions & 1 deletion io/tcp_server.cc
Expand Up @@ -9,6 +9,7 @@
#include <boost/asio/connect.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/bind.hpp>
#include <netinet/tcp.h>

#include "base/logging.h"
#include "io/event_manager.h"
Expand Down Expand Up @@ -471,7 +472,7 @@ int TcpServer::SetMd5SocketOption(int fd, uint32_t peer_ip,
memcpy(md5sig.tcpm_key, md5_password.c_str(), md5_password.size());
md5sig.tcpm_keylen = md5_password.size();
memcpy(&md5sig.tcpm_addr, &local_addr, sizeof(local_addr));
int retval = setsockopt(fd, IPPROTO_TCP, TCP_MD5SIG, &md5sig,
int retval = setsockopt(fd, IPPROTO_TCP, TCP_MD5SIG, (const char *)&md5sig,
sizeof(md5sig));
if (retval < 0) {
TCP_SERVER_LOG_ERROR(this, TCP_DIR_NA,
Expand Down
17 changes: 12 additions & 5 deletions io/tcp_session.cc
Expand Up @@ -115,7 +115,7 @@ TcpSession::~TcpSession() {
}

mutable_buffer TcpSession::AllocateBuffer(size_t buffer_size) {
u_int8_t *data = new u_int8_t[buffer_size];
uint8_t *data = new uint8_t[buffer_size];
mutable_buffer buffer = mutable_buffer(data, buffer_size);
buffer_queue_.push_back(buffer);
return buffer;
Expand Down Expand Up @@ -203,12 +203,12 @@ void TcpSession::AsyncReadSome() {
}
}

size_t TcpSession::WriteSome(const uint8_t *data, size_t len,
error_code *error) {
std::size_t TcpSession::WriteSome(const uint8_t *data, std::size_t len,
error_code *error) {
return socket()->write_some(buffer(data, len), *error);
}

void TcpSession::AsyncWrite(const u_int8_t *data, size_t size) {
void TcpSession::AsyncWrite(const uint8_t *data, std::size_t size) {
async_write(*socket(), buffer(data, size),
bind(&TcpSession::AsyncWriteHandler, TcpSessionPtr(this),
error, bytes_transferred));
Expand Down Expand Up @@ -464,7 +464,7 @@ void TcpSession::AsyncWriteInternal(TcpSessionPtr session) {
session->writer_->TriggerAsyncWrite();
}

bool TcpSession::Send(const u_int8_t *data, size_t size, size_t *sent) {
bool TcpSession::Send(const uint8_t *data, size_t size, size_t *sent) {
bool ret = true;
tbb::mutex::scoped_lock lock(mutex_);

Expand Down Expand Up @@ -837,6 +837,12 @@ error_code TcpSession::SetSocketKeepaliveOptions(int keepalive_time,
return ec;
}
#endif
// TCP_KEEPCNT and TCP_KEEPINTVL are not supported on windows. But boost tries to set them, causing
// an exception. See:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms738596(v=vs.85).aspx
// Issue similar to this one:
// https://git.openstack.org/cgit/openstack/python-keystoneclient/commit/?id=33b24a6984c8de2f26af7900202bb85b6b5db125
#ifndef _WIN32
#ifdef TCP_KEEPINTVL
typedef integer< IPPROTO_TCP, TCP_KEEPINTVL > keepalive_interval;
keepalive_interval keepalive_interval_option(keepalive_intvl);
Expand All @@ -857,6 +863,7 @@ error_code TcpSession::SetSocketKeepaliveOptions(int keepalive_time,
return ec;
}
#endif
#endif
#ifdef TCP_USER_TIMEOUT
typedef integer< IPPROTO_TCP, TCP_USER_TIMEOUT > tcp_user_timeout;
tcp_user_timeout tcp_user_timeout_option(tcp_user_timeout_val);
Expand Down
6 changes: 3 additions & 3 deletions io/tcp_session.h
Expand Up @@ -63,7 +63,7 @@ class TcpSession {
bool async_read_ready = true,
size_t buffer_send_size = TcpSession::kDefaultWriteBufferSize);
// Performs a non-blocking send operation.
virtual bool Send(const u_int8_t *data, size_t size, size_t *sent);
virtual bool Send(const uint8_t *data, size_t size, size_t *sent);

// Called by TcpServer to trigger async read.
virtual bool Connected(Endpoint remote);
Expand Down Expand Up @@ -198,7 +198,7 @@ class TcpSession {
boost::system::error_code *error);
virtual std::size_t WriteSome(const uint8_t *data, std::size_t len,
boost::system::error_code *error);
virtual void AsyncWrite(const u_int8_t *data, std::size_t size);
virtual void AsyncWrite(const uint8_t *data, std::size_t size);

virtual int reader_task_id() const {
return reader_task_id_;
Expand Down Expand Up @@ -304,7 +304,7 @@ inline void intrusive_ptr_release(TcpSession *session) {
class TcpMessageReader {
public:
typedef boost::asio::const_buffer Buffer;
typedef boost::function<bool(const u_int8_t *, size_t)> ReceiveCallback;
typedef boost::function<bool(const uint8_t *, size_t)> ReceiveCallback;

TcpMessageReader(TcpSession *session, ReceiveCallback callback);
virtual ~TcpMessageReader();
Expand Down
15 changes: 6 additions & 9 deletions io/test/event_manager_test.h
Expand Up @@ -8,7 +8,7 @@
#include "io/event_manager.h"

#include <boost/scoped_ptr.hpp>
#include <pthread.h>
#include <boost/thread.hpp>
#include <tbb/atomic.h>

#include "base/logging.h"
Expand All @@ -17,7 +17,7 @@
class ServerThread {
public:
explicit ServerThread(EventManager *evm)
: thread_id_(pthread_self()), evm_(evm), tbb_scheduler_(NULL) {
: evm_(evm), tbb_scheduler_(NULL) {
}
void Run() {
tbb_scheduler_.reset(
Expand All @@ -33,16 +33,13 @@ class ServerThread {
return NULL;
}
void Start() {
int res = pthread_create(&thread_id_, NULL, &ThreadRun, this);
assert(res == 0);
thread_.reset(new boost::thread(&ServerThread::Run, this));
}
void Join() {
int res = pthread_join(thread_id_, NULL);
assert(res == 0);
}

thread_->join();
}
private:
pthread_t thread_id_;
boost::scoped_ptr<boost::thread> thread_;
tbb::atomic<bool> running_;
EventManager *evm_;
boost::scoped_ptr<tbb::task_scheduler_init> tbb_scheduler_;
Expand Down
6 changes: 3 additions & 3 deletions io/udp_server.cc
Expand Up @@ -153,7 +153,7 @@ bool UdpServer::Initialize(udp::endpoint local_endpoint) {
}

mutable_buffer UdpServer::AllocateBuffer(std::size_t s) {
u_int8_t *p = new u_int8_t[s];
uint8_t *p = new uint8_t[s];
{
tbb::mutex::scoped_lock lock(mutex_);
pbuf_.push_back(p);
Expand All @@ -166,10 +166,10 @@ mutable_buffer UdpServer::AllocateBuffer() {
}

void UdpServer::DeallocateBuffer(const const_buffer &buffer) {
const u_int8_t *p = buffer_cast<const uint8_t *>(buffer);
const uint8_t *p = buffer_cast<const uint8_t *>(buffer);
{
tbb::mutex::scoped_lock lock(mutex_);
std::vector<u_int8_t *>::iterator f = std::find(pbuf_.begin(),
std::vector<uint8_t *>::iterator f = std::find(pbuf_.begin(),
pbuf_.end(), p);
if (f != pbuf_.end())
pbuf_.erase(f);
Expand Down
2 changes: 1 addition & 1 deletion io/udp_server.h
Expand Up @@ -106,7 +106,7 @@ class UdpServer {
std::string name_;
boost::asio::ip::udp::endpoint remote_endpoint_;
tbb::mutex mutex_;
std::vector<u_int8_t *> pbuf_;
std::vector<uint8_t *> pbuf_;
tbb::atomic<int> refcount_;
io::SocketStats stats_;

Expand Down

0 comments on commit 442663f

Please sign in to comment.