summaryrefslogtreecommitdiff
path: root/pr/src/cplus/rcnetio.cpp
blob: f511d81dc08407cd0e85a65b79cad252079d8d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
** Subclass implementation for streamed network I/O (ref: prio.h)
*/

#include "rcnetio.h"

#include <private/pprio.h>

RCNetStreamIO::~RCNetStreamIO()
    { PRStatus rv = (fd->methods->close)(fd); fd = NULL; }

RCNetStreamIO::RCNetStreamIO(): RCIO(RCIO::tcp)
    { fd = PR_NewTCPSocket(); }

RCNetStreamIO::RCNetStreamIO(PRIntn protocol): RCIO(RCIO::tcp)
    { fd = PR_Socket(PR_AF_INET, PR_SOCK_STREAM, protocol); }

RCIO* RCNetStreamIO::Accept(RCNetAddr* addr, const RCInterval& timeout)
{
    PRNetAddr peer;
    RCNetStreamIO* rcio = NULL;
    PRFileDesc* newfd = fd->methods->accept(fd, &peer, timeout);
    if (NULL != newfd)
    {
        rcio = new RCNetStreamIO();
        if (NULL != rcio)
        {
            *addr = &peer;
            rcio->fd = newfd;
        }
        else
            (void)(newfd->methods->close)(newfd);
    }
    return rcio;
}  /* RCNetStreamIO::Accept */

PRInt32 RCNetStreamIO::AcceptRead(
    RCIO **nd, RCNetAddr **raddr, void *buf,
    PRSize amount, const RCInterval& timeout)
{   
    PRNetAddr *from;
    PRFileDesc *accepted;
    PRInt32 rv = (fd->methods->acceptread)(
        fd, &accepted, &from, buf, amount, timeout);
    if (rv >= 0)
    {
        RCNetStreamIO *ns = new RCNetStreamIO();
        if (NULL != *nd) ns->fd = accepted;
        else {PR_Close(accepted); rv = -1; }
        *nd = ns;
    }
    return rv;
}  /* RCNetStreamIO::AcceptRead */

PRInt64 RCNetStreamIO::Available()
    { return (fd->methods->available64)(fd); }

PRStatus RCNetStreamIO::Bind(const RCNetAddr& addr)
    { return (fd->methods->bind)(fd, addr); }

PRStatus RCNetStreamIO::Connect(const RCNetAddr& addr, const RCInterval& timeout)
    { return (fd->methods->connect)(fd, addr, timeout); }

PRStatus RCNetStreamIO::GetLocalName(RCNetAddr *addr) const
{
    PRNetAddr local;
    PRStatus rv = (fd->methods->getsockname)(fd, &local);
    if (PR_SUCCESS == rv) *addr = &local;
    return rv;
}  /* RCNetStreamIO::GetLocalName */

PRStatus RCNetStreamIO::GetPeerName(RCNetAddr *addr) const
{
    PRNetAddr peer;
    PRStatus rv = (fd->methods->getpeername)(fd, &peer);
    if (PR_SUCCESS == rv) *addr = &peer;
    return rv;
}  /* RCNetStreamIO::GetPeerName */

PRStatus RCNetStreamIO::GetSocketOption(PRSocketOptionData *data) const
    { return (fd->methods->getsocketoption)(fd, data); }

PRStatus RCNetStreamIO::Listen(PRIntn backlog)
    { return (fd->methods->listen)(fd, backlog); }

PRInt16 RCNetStreamIO::Poll(PRInt16 in_flags, PRInt16 *out_flags)
    { return (fd->methods->poll)(fd, in_flags, out_flags); }

PRInt32 RCNetStreamIO::Read(void *buf, PRSize amount)
    { return (fd->methods->read)(fd, buf, amount); }

PRInt32 RCNetStreamIO::Recv(
    void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout)
    { return (fd->methods->recv)(fd, buf, amount, flags, timeout); }

PRInt32 RCNetStreamIO::Recvfrom(
    void *buf, PRSize amount, PRIntn flags,
    RCNetAddr* addr, const RCInterval& timeout)
{
    PRNetAddr peer;
    PRInt32 rv = (fd->methods->recvfrom)(
        fd, buf, amount, flags, &peer, timeout);
    if (-1 != rv) *addr = &peer;
    return rv;
}  /* RCNetStreamIO::Recvfrom */

PRInt32 RCNetStreamIO::Send(
    const void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout)
    { return (fd->methods->send)(fd, buf, amount, flags, timeout); }

PRInt32 RCNetStreamIO::Sendto(
    const void *buf, PRSize amount, PRIntn flags,
    const RCNetAddr& addr, const RCInterval& timeout)
    { return (fd->methods->sendto)(fd, buf, amount, flags, addr, timeout); }

PRStatus RCNetStreamIO::SetSocketOption(const PRSocketOptionData *data)
    { return (fd->methods->setsocketoption)(fd, data); }

PRStatus RCNetStreamIO::Shutdown(RCIO::ShutdownHow how)
    { return (fd->methods->shutdown)(fd, (PRIntn)how); }

PRInt32 RCNetStreamIO::TransmitFile(
    RCIO *source, const void *headers, PRSize hlen,
    RCIO::FileDisposition flags, const RCInterval& timeout)
{
    RCNetStreamIO *src = (RCNetStreamIO*)source;
    return (fd->methods->transmitfile)(
        fd, src->fd, headers, hlen, (PRTransmitFileFlags)flags, timeout); }

PRInt32 RCNetStreamIO::Write(const void *buf, PRSize amount)
    { return (fd->methods->write)(fd, buf, amount); }

PRInt32 RCNetStreamIO::Writev(
    const PRIOVec *iov, PRSize size, const RCInterval& timeout)
    { return (fd->methods->writev)(fd, iov, size, timeout); }
    
/*
** Invalid functions
*/

PRStatus RCNetStreamIO::Close()
    { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }

PRStatus RCNetStreamIO::FileInfo(RCFileInfo*) const
    { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }

PRStatus RCNetStreamIO::Fsync()
    { return (fd->methods->fsync)(fd); }

PRStatus RCNetStreamIO::Open(const char*, PRIntn, PRIntn)
    { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }

PRInt64 RCNetStreamIO::Seek(PRInt64, RCIO::Whence)
    { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }

/* RCNetStreamIO.cpp */