This source file includes following definitions.
- weak_ptr_factory_
- Read
- ScheduleRead
- WriteNoLock
- ScheduleWriteNoLock
- OnInit
- OnShutdownNoLock
- OnFileCanReadWithoutBlocking
- OnFileCanWriteWithoutBlocking
- WaitToWrite
- Create
#include "mojo/system/raw_channel.h"
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <algorithm>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/posix/eintr_wrapper.h"
#include "base/synchronization/lock.h"
#include "build/build_config.h"
#include "mojo/embedder/platform_handle.h"
namespace mojo {
namespace system {
namespace {
class RawChannelPosix : public RawChannel,
public base::MessageLoopForIO::Watcher {
public:
RawChannelPosix(embedder::ScopedPlatformHandle handle);
virtual ~RawChannelPosix();
private:
virtual IOResult Read(size_t* bytes_read) OVERRIDE;
virtual IOResult ScheduleRead() OVERRIDE;
virtual IOResult WriteNoLock(size_t* bytes_written) OVERRIDE;
virtual IOResult ScheduleWriteNoLock() OVERRIDE;
virtual bool OnInit() OVERRIDE;
virtual void OnShutdownNoLock(
scoped_ptr<ReadBuffer> read_buffer,
scoped_ptr<WriteBuffer> write_buffer) OVERRIDE;
virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
void WaitToWrite();
embedder::ScopedPlatformHandle fd_;
scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> read_watcher_;
scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> write_watcher_;
bool pending_read_;
bool pending_write_;
base::WeakPtrFactory<RawChannelPosix> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(RawChannelPosix);
};
RawChannelPosix::RawChannelPosix(embedder::ScopedPlatformHandle handle)
: fd_(handle.Pass()),
pending_read_(false),
pending_write_(false),
weak_ptr_factory_(this) {
DCHECK(fd_.is_valid());
}
RawChannelPosix::~RawChannelPosix() {
DCHECK(!pending_read_);
DCHECK(!pending_write_);
DCHECK(!weak_ptr_factory_.HasWeakPtrs());
DCHECK(!read_watcher_.get());
DCHECK(!write_watcher_.get());
}
RawChannel::IOResult RawChannelPosix::Read(size_t* bytes_read) {
DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io());
DCHECK(!pending_read_);
char* buffer = NULL;
size_t bytes_to_read = 0;
read_buffer()->GetBuffer(&buffer, &bytes_to_read);
ssize_t read_result = HANDLE_EINTR(read(fd_.get().fd, buffer, bytes_to_read));
if (read_result > 0) {
*bytes_read = static_cast<size_t>(read_result);
return IO_SUCCEEDED;
}
if (read_result == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
if (read_result != 0)
PLOG(ERROR) << "read";
read_watcher_.reset();
return IO_FAILED;
}
return ScheduleRead();
}
RawChannel::IOResult RawChannelPosix::ScheduleRead() {
DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io());
DCHECK(!pending_read_);
pending_read_ = true;
return IO_PENDING;
}
RawChannel::IOResult RawChannelPosix::WriteNoLock(size_t* bytes_written) {
write_lock().AssertAcquired();
DCHECK(!pending_write_);
std::vector<WriteBuffer::Buffer> buffers;
write_buffer_no_lock()->GetBuffers(&buffers);
DCHECK(!buffers.empty());
ssize_t write_result = -1;
if (buffers.size() == 1) {
write_result = HANDLE_EINTR(
write(fd_.get().fd, buffers[0].addr, buffers[0].size));
} else {
const size_t kMaxBufferCount = 10;
iovec iov[kMaxBufferCount];
size_t buffer_count = std::min(buffers.size(), kMaxBufferCount);
for (size_t i = 0; i < buffer_count; ++i) {
iov[i].iov_base = const_cast<char*>(buffers[i].addr);
iov[i].iov_len = buffers[i].size;
}
#if defined(OS_MACOSX)
write_result = HANDLE_EINTR(writev(fd_.get().fd, iov, buffer_count));
#else
struct msghdr msg = {};
msg.msg_iov = iov;
msg.msg_iovlen = buffer_count;
write_result = HANDLE_EINTR(sendmsg(fd_.get().fd, &msg, MSG_NOSIGNAL));
#endif
}
if (write_result >= 0) {
*bytes_written = static_cast<size_t>(write_result);
return IO_SUCCEEDED;
}
if (errno != EAGAIN && errno != EWOULDBLOCK) {
PLOG(ERROR) << "write of size "
<< write_buffer_no_lock()->GetTotalBytesToWrite();
return IO_FAILED;
}
return ScheduleWriteNoLock();
}
RawChannel::IOResult RawChannelPosix::ScheduleWriteNoLock() {
write_lock().AssertAcquired();
DCHECK(!pending_write_);
if (base::MessageLoop::current() != message_loop_for_io()) {
message_loop_for_io()->PostTask(
FROM_HERE,
base::Bind(&RawChannelPosix::WaitToWrite,
weak_ptr_factory_.GetWeakPtr()));
pending_write_ = true;
return IO_PENDING;
}
if (message_loop_for_io()->WatchFileDescriptor(
fd_.get().fd, false, base::MessageLoopForIO::WATCH_WRITE,
write_watcher_.get(), this)) {
pending_write_ = true;
return IO_PENDING;
}
return IO_FAILED;
}
bool RawChannelPosix::OnInit() {
DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io());
DCHECK(!read_watcher_.get());
read_watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher());
DCHECK(!write_watcher_.get());
write_watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher());
if (!message_loop_for_io()->WatchFileDescriptor(fd_.get().fd, true,
base::MessageLoopForIO::WATCH_READ, read_watcher_.get(), this)) {
read_watcher_.reset();
write_watcher_.reset();
return false;
}
return true;
}
void RawChannelPosix::OnShutdownNoLock(
scoped_ptr<ReadBuffer> ,
scoped_ptr<WriteBuffer> ) {
DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io());
write_lock().AssertAcquired();
read_watcher_.reset();
write_watcher_.reset();
pending_read_ = false;
pending_write_ = false;
DCHECK(fd_.is_valid());
fd_.reset();
weak_ptr_factory_.InvalidateWeakPtrs();
}
void RawChannelPosix::OnFileCanReadWithoutBlocking(int fd) {
DCHECK_EQ(fd, fd_.get().fd);
DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io());
if (!pending_read_) {
NOTREACHED();
return;
}
pending_read_ = false;
size_t bytes_read = 0;
IOResult result = Read(&bytes_read);
if (result != IO_PENDING)
OnReadCompleted(result == IO_SUCCEEDED, bytes_read);
DCHECK(!read_watcher_.get() || pending_read_);
}
void RawChannelPosix::OnFileCanWriteWithoutBlocking(int fd) {
DCHECK_EQ(fd, fd_.get().fd);
DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io());
IOResult result = IO_FAILED;
size_t bytes_written = 0;
{
base::AutoLock locker(write_lock());
DCHECK(pending_write_);
pending_write_ = false;
result = WriteNoLock(&bytes_written);
}
if (result != IO_PENDING)
OnWriteCompleted(result == IO_SUCCEEDED, bytes_written);
}
void RawChannelPosix::WaitToWrite() {
DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io());
DCHECK(write_watcher_.get());
if (!message_loop_for_io()->WatchFileDescriptor(
fd_.get().fd, false, base::MessageLoopForIO::WATCH_WRITE,
write_watcher_.get(), this)) {
{
base::AutoLock locker(write_lock());
DCHECK(pending_write_);
pending_write_ = false;
}
OnWriteCompleted(false, 0);
}
}
}
scoped_ptr<RawChannel> RawChannel::Create(
embedder::ScopedPlatformHandle handle) {
return scoped_ptr<RawChannel>(new RawChannelPosix(handle.Pass()));
}
}
}