plptools
Loading...
Searching...
No Matches
tcpsocket.h
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19 *
20 */
21#pragma once
22
23#include <string>
24
25#include <unistd.h>
26#include <netdb.h>
27#include <sys/socket.h>
28#include <arpa/inet.h>
29
30class BufferStore;
31class IOWatch;
32
37{
38
39public:
43 TCPSocket();
44
48 TCPSocket(const TCPSocket&);
49
53 virtual ~TCPSocket();
54
65 virtual bool connect(const char * const Peer, int PeerPort, const char * const Host = NULL, int HostPort = 0);
66
72 virtual bool reconnect();
73
81 virtual std::string toString();
82
91 virtual bool listen(const char * const Host, int Port);
92
101 TCPSocket *accept(std::string *Peer);
102
112 TCPSocket *accept(std::string *Peer, int cancellationFd);
113
122 bool dataToGet(int sec, int usec) const;
123
133 int getBufferStore(BufferStore &a, bool wait = true);
134
141 bool sendBufferStore(const BufferStore &a);
142
148 bool closeSocket(void);
149
158 bool bindSocket(const char * const Host, int Port);
159
174 bool bindInRange(const char * const Host, int Low, int High, int Retries);
175
184 bool linger(bool LingerOn, int LingerTime = 0);
185
194 bool getPeer(std::string *Peer, int *Port);
195
204 bool getHost(std::string *Host, int *Port);
205
213 void setWatch(IOWatch *watch);
214
215private:
219 virtual bool createSocket(void);
220
221 int getLastError(void) { return(m_LastError); }
222 bool setPeer(const char * const Peer, int Port);
223 bool setHost(const char * const Host, int Port);
224 int recv(void *buf, int len, int flags);
225 int send(const void * const buf, int len, int flags);
226
227 struct sockaddr m_HostAddr;
228 struct sockaddr m_PeerAddr;
234};
A generic container for an array of bytes.
Definition: bufferstore.h:36
A simple thread-safe wrapper for select()
Definition: iowatch.h:34
A class for dealing with sockets.
Definition: tcpsocket.h:37
bool bindInRange(const char *const Host, int Low, int High, int Retries)
Tries repeated binds to a local address and port.
Definition: tcpsocket.cc:425
IOWatch * myWatch
Definition: tcpsocket.h:233
int m_Socket
Definition: tcpsocket.h:229
int getLastError(void)
Definition: tcpsocket.h:221
bool closeSocket(void)
Closes the connection.
Definition: tcpsocket.cc:374
bool linger(bool LingerOn, int LingerTime=0)
Sets the linger parameter of the socket.
Definition: tcpsocket.cc:473
int m_Port
Definition: tcpsocket.h:230
bool setPeer(const char *const Peer, int Port)
Definition: tcpsocket.cc:523
void setWatch(IOWatch *watch)
Registers an IOWatch for this socket.
Definition: tcpsocket.cc:89
virtual bool listen(const char *const Host, int Port)
Starts listening.
Definition: tcpsocket.cc:183
bool getPeer(std::string *Peer, int *Port)
Retrieves peer information.
Definition: tcpsocket.cc:556
virtual bool reconnect()
Reopens the connection after closing it.
Definition: tcpsocket.cc:100
bool setHost(const char *const Host, int Port)
Definition: tcpsocket.cc:574
struct sockaddr m_HostAddr
Definition: tcpsocket.h:227
TCPSocket()
Constructs a TCPSocket.
Definition: tcpsocket.cc:63
int m_LastError
Definition: tcpsocket.h:232
virtual bool createSocket(void)
Creates the socket.
Definition: tcpsocket.cc:502
bool getHost(std::string *Host, int *Port)
Retrieves local information.
Definition: tcpsocket.cc:609
virtual std::string toString()
Retrieve a string representation of the TCPSocket.
Definition: tcpsocket.cc:128
int send(const void *const buf, int len, int flags)
Definition: tcpsocket.cc:363
bool bindSocket(const char *const Host, int Port)
Binds to a local address and port.
Definition: tcpsocket.cc:388
bool m_Bound
Definition: tcpsocket.h:231
TCPSocket * accept(std::string *Peer)
Accept a connection; blocking, non-cancellable.
Definition: tcpsocket.cc:208
int getBufferStore(BufferStore &a, bool wait=true)
Receive data into a BufferStore .
Definition: tcpsocket.cc:289
bool sendBufferStore(const BufferStore &a)
Sends data from a BufferStore .
Definition: tcpsocket.cc:325
struct sockaddr m_PeerAddr
Definition: tcpsocket.h:228
int recv(void *buf, int len, int flags)
Definition: tcpsocket.cc:352
bool dataToGet(int sec, int usec) const
Check and optionally wait for incoming data.
Definition: tcpsocket.cc:277
virtual ~TCPSocket()
Destructor.
Definition: tcpsocket.cc:78
virtual bool connect(const char *const Peer, int PeerPort, const char *const Host=NULL, int HostPort=0)
Connects to a given host.
Definition: tcpsocket.cc:153
static RFSV * a
Definition: main.cc:55