nsprpub/pr/src/cplus/rcnetio.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 ** Subclass implementation for streamed network I/O (ref: prio.h)
michael@0 8 */
michael@0 9
michael@0 10 #include "rcnetio.h"
michael@0 11
michael@0 12 #include <private/pprio.h>
michael@0 13
michael@0 14 RCNetStreamIO::~RCNetStreamIO()
michael@0 15 { PRStatus rv = (fd->methods->close)(fd); fd = NULL; }
michael@0 16
michael@0 17 RCNetStreamIO::RCNetStreamIO(): RCIO(RCIO::tcp)
michael@0 18 { fd = PR_NewTCPSocket(); }
michael@0 19
michael@0 20 RCNetStreamIO::RCNetStreamIO(PRIntn protocol): RCIO(RCIO::tcp)
michael@0 21 { fd = PR_Socket(PR_AF_INET, PR_SOCK_STREAM, protocol); }
michael@0 22
michael@0 23 RCIO* RCNetStreamIO::Accept(RCNetAddr* addr, const RCInterval& timeout)
michael@0 24 {
michael@0 25 PRNetAddr peer;
michael@0 26 RCNetStreamIO* rcio = NULL;
michael@0 27 PRFileDesc* newfd = fd->methods->accept(fd, &peer, timeout);
michael@0 28 if (NULL != newfd)
michael@0 29 {
michael@0 30 rcio = new RCNetStreamIO();
michael@0 31 if (NULL != rcio)
michael@0 32 {
michael@0 33 *addr = &peer;
michael@0 34 rcio->fd = newfd;
michael@0 35 }
michael@0 36 else
michael@0 37 (void)(newfd->methods->close)(newfd);
michael@0 38 }
michael@0 39 return rcio;
michael@0 40 } /* RCNetStreamIO::Accept */
michael@0 41
michael@0 42 PRInt32 RCNetStreamIO::AcceptRead(
michael@0 43 RCIO **nd, RCNetAddr **raddr, void *buf,
michael@0 44 PRSize amount, const RCInterval& timeout)
michael@0 45 {
michael@0 46 PRNetAddr *from;
michael@0 47 PRFileDesc *accepted;
michael@0 48 PRInt32 rv = (fd->methods->acceptread)(
michael@0 49 fd, &accepted, &from, buf, amount, timeout);
michael@0 50 if (rv >= 0)
michael@0 51 {
michael@0 52 RCNetStreamIO *ns = new RCNetStreamIO();
michael@0 53 if (NULL != *nd) ns->fd = accepted;
michael@0 54 else {PR_Close(accepted); rv = -1; }
michael@0 55 *nd = ns;
michael@0 56 }
michael@0 57 return rv;
michael@0 58 } /* RCNetStreamIO::AcceptRead */
michael@0 59
michael@0 60 PRInt64 RCNetStreamIO::Available()
michael@0 61 { return (fd->methods->available64)(fd); }
michael@0 62
michael@0 63 PRStatus RCNetStreamIO::Bind(const RCNetAddr& addr)
michael@0 64 { return (fd->methods->bind)(fd, addr); }
michael@0 65
michael@0 66 PRStatus RCNetStreamIO::Connect(const RCNetAddr& addr, const RCInterval& timeout)
michael@0 67 { return (fd->methods->connect)(fd, addr, timeout); }
michael@0 68
michael@0 69 PRStatus RCNetStreamIO::GetLocalName(RCNetAddr *addr) const
michael@0 70 {
michael@0 71 PRNetAddr local;
michael@0 72 PRStatus rv = (fd->methods->getsockname)(fd, &local);
michael@0 73 if (PR_SUCCESS == rv) *addr = &local;
michael@0 74 return rv;
michael@0 75 } /* RCNetStreamIO::GetLocalName */
michael@0 76
michael@0 77 PRStatus RCNetStreamIO::GetPeerName(RCNetAddr *addr) const
michael@0 78 {
michael@0 79 PRNetAddr peer;
michael@0 80 PRStatus rv = (fd->methods->getpeername)(fd, &peer);
michael@0 81 if (PR_SUCCESS == rv) *addr = &peer;
michael@0 82 return rv;
michael@0 83 } /* RCNetStreamIO::GetPeerName */
michael@0 84
michael@0 85 PRStatus RCNetStreamIO::GetSocketOption(PRSocketOptionData *data) const
michael@0 86 { return (fd->methods->getsocketoption)(fd, data); }
michael@0 87
michael@0 88 PRStatus RCNetStreamIO::Listen(PRIntn backlog)
michael@0 89 { return (fd->methods->listen)(fd, backlog); }
michael@0 90
michael@0 91 PRInt16 RCNetStreamIO::Poll(PRInt16 in_flags, PRInt16 *out_flags)
michael@0 92 { return (fd->methods->poll)(fd, in_flags, out_flags); }
michael@0 93
michael@0 94 PRInt32 RCNetStreamIO::Read(void *buf, PRSize amount)
michael@0 95 { return (fd->methods->read)(fd, buf, amount); }
michael@0 96
michael@0 97 PRInt32 RCNetStreamIO::Recv(
michael@0 98 void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout)
michael@0 99 { return (fd->methods->recv)(fd, buf, amount, flags, timeout); }
michael@0 100
michael@0 101 PRInt32 RCNetStreamIO::Recvfrom(
michael@0 102 void *buf, PRSize amount, PRIntn flags,
michael@0 103 RCNetAddr* addr, const RCInterval& timeout)
michael@0 104 {
michael@0 105 PRNetAddr peer;
michael@0 106 PRInt32 rv = (fd->methods->recvfrom)(
michael@0 107 fd, buf, amount, flags, &peer, timeout);
michael@0 108 if (-1 != rv) *addr = &peer;
michael@0 109 return rv;
michael@0 110 } /* RCNetStreamIO::Recvfrom */
michael@0 111
michael@0 112 PRInt32 RCNetStreamIO::Send(
michael@0 113 const void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout)
michael@0 114 { return (fd->methods->send)(fd, buf, amount, flags, timeout); }
michael@0 115
michael@0 116 PRInt32 RCNetStreamIO::Sendto(
michael@0 117 const void *buf, PRSize amount, PRIntn flags,
michael@0 118 const RCNetAddr& addr, const RCInterval& timeout)
michael@0 119 { return (fd->methods->sendto)(fd, buf, amount, flags, addr, timeout); }
michael@0 120
michael@0 121 PRStatus RCNetStreamIO::SetSocketOption(const PRSocketOptionData *data)
michael@0 122 { return (fd->methods->setsocketoption)(fd, data); }
michael@0 123
michael@0 124 PRStatus RCNetStreamIO::Shutdown(RCIO::ShutdownHow how)
michael@0 125 { return (fd->methods->shutdown)(fd, (PRIntn)how); }
michael@0 126
michael@0 127 PRInt32 RCNetStreamIO::TransmitFile(
michael@0 128 RCIO *source, const void *headers, PRSize hlen,
michael@0 129 RCIO::FileDisposition flags, const RCInterval& timeout)
michael@0 130 {
michael@0 131 RCNetStreamIO *src = (RCNetStreamIO*)source;
michael@0 132 return (fd->methods->transmitfile)(
michael@0 133 fd, src->fd, headers, hlen, (PRTransmitFileFlags)flags, timeout); }
michael@0 134
michael@0 135 PRInt32 RCNetStreamIO::Write(const void *buf, PRSize amount)
michael@0 136 { return (fd->methods->write)(fd, buf, amount); }
michael@0 137
michael@0 138 PRInt32 RCNetStreamIO::Writev(
michael@0 139 const PRIOVec *iov, PRSize size, const RCInterval& timeout)
michael@0 140 { return (fd->methods->writev)(fd, iov, size, timeout); }
michael@0 141
michael@0 142 /*
michael@0 143 ** Invalid functions
michael@0 144 */
michael@0 145
michael@0 146 PRStatus RCNetStreamIO::Close()
michael@0 147 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
michael@0 148
michael@0 149 PRStatus RCNetStreamIO::FileInfo(RCFileInfo*) const
michael@0 150 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
michael@0 151
michael@0 152 PRStatus RCNetStreamIO::Fsync()
michael@0 153 { return (fd->methods->fsync)(fd); }
michael@0 154
michael@0 155 PRStatus RCNetStreamIO::Open(const char*, PRIntn, PRIntn)
michael@0 156 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
michael@0 157
michael@0 158 PRInt64 RCNetStreamIO::Seek(PRInt64, RCIO::Whence)
michael@0 159 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
michael@0 160
michael@0 161 /* RCNetStreamIO.cpp */
michael@0 162
michael@0 163

mercurial