PageRenderTime 147ms CodeModel.GetById 71ms app.highlight 7ms RepoModel.GetById 63ms app.codeStats 5ms

/mordor/streams/socket.cpp

http://github.com/mozy/mordor
C++ | 87 lines | 73 code | 13 blank | 1 comment | 2 complexity | 0d7a8c1a8baf0954aea3ed617badaf99 MD5 | raw file
 1// Copyright (c) 2009 - Mozy, Inc.
 2
 3#include "socket.h"
 4
 5#include "buffer.h"
 6#include "mordor/assert.h"
 7#include "mordor/socket.h"
 8
 9namespace Mordor {
10
11SocketStream::SocketStream(Socket::ptr socket, bool own)
12: m_socket(socket),
13  m_own(own)
14{
15    MORDOR_ASSERT(socket);
16}
17
18void
19SocketStream::close(CloseType type)
20{
21    if (m_socket && m_own) {
22        int how;
23        switch(type) {
24            case READ:
25                how = SHUT_RD;
26                break;
27            case WRITE:
28                how = SHUT_WR;
29                break;
30            default:
31                how = SHUT_RDWR;
32                break;
33        }
34        m_socket->shutdown(how);
35    }
36}
37
38size_t
39SocketStream::read(Buffer &buffer, size_t length)
40{
41    std::vector<iovec> iovs = buffer.writeBuffers(length);
42    size_t result = m_socket->receive(&iovs[0], iovs.size());
43    buffer.produce(result);
44    return result;
45}
46
47size_t
48SocketStream::read(void *buffer, size_t length)
49{
50    return m_socket->receive(buffer, length);
51}
52
53void
54SocketStream::cancelRead()
55{
56    m_socket->cancelReceive();
57}
58
59size_t
60SocketStream::write(const Buffer &buffer, size_t length)
61{
62    const std::vector<iovec> iovs = buffer.readBuffers(length);
63    size_t result = m_socket->send(&iovs[0], iovs.size());
64    MORDOR_ASSERT(result > 0);
65    return result;
66}
67
68size_t
69SocketStream::write(const void *buffer, size_t length)
70{
71    return m_socket->send(buffer, length);
72}
73
74void
75SocketStream::cancelWrite()
76{
77    m_socket->cancelSend();
78}
79
80boost::signals2::connection
81SocketStream::onRemoteClose(
82    const boost::signals2::slot<void ()> &slot)
83{
84    return m_socket->onRemoteClose(slot);
85}
86
87}