Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / kits / network / libnetapi / Socket.cpp
blob987549a7be97efb7c587453526d8bedd20c18775
1 /*
2 * Copyright 2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <Socket.h>
10 //#define TRACE_SOCKET
11 #ifdef TRACE_SOCKET
12 # define TRACE(x...) printf(x)
13 #else
14 # define TRACE(x...) ;
15 #endif
18 BSocket::BSocket()
23 BSocket::BSocket(const BNetworkAddress& peer, bigtime_t timeout)
25 Connect(peer, timeout);
29 BSocket::BSocket(const BSocket& other)
31 BAbstractSocket(other)
36 BSocket::~BSocket()
41 status_t
42 BSocket::Bind(const BNetworkAddress& local)
44 return BAbstractSocket::Bind(local, SOCK_STREAM);
48 status_t
49 BSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout)
51 return BAbstractSocket::Connect(peer, SOCK_STREAM, timeout);
55 // #pragma mark - BDataIO implementation
58 ssize_t
59 BSocket::Read(void* buffer, size_t size)
61 ssize_t bytesReceived = recv(Socket(), buffer, size, 0);
62 if (bytesReceived < 0) {
63 TRACE("%p: BSocket::Read() error: %s\n", this, strerror(errno));
64 return errno;
67 return bytesReceived;
71 ssize_t
72 BSocket::Write(const void* buffer, size_t size)
74 ssize_t bytesSent = send(Socket(), buffer, size, 0);
75 if (bytesSent < 0) {
76 TRACE("%p: BSocket::Write() error: %s\n", this, strerror(errno));
77 return errno;
80 return bytesSent;
84 // #pragma mark - private
87 void
88 BSocket::_SetTo(int fd, const BNetworkAddress& local,
89 const BNetworkAddress& peer)
91 Disconnect();
93 fInitStatus = B_OK;
94 fSocket = fd;
95 fLocal = local;
96 fPeer = peer;
98 TRACE("%p: accepted from %s to %s\n", this, local.ToString().c_str(),
99 peer.ToString().c_str());