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#ifndef _PPSOCKET_H_
22#define _PPSOCKET_H_
23
24#include <string>
25
26#include <unistd.h>
27#include <netdb.h>
28#include <sys/socket.h>
29#include <arpa/inet.h>
30
31class bufferStore;
32class IOWatch;
33
38{
39
40public:
44 TCPSocket();
45
49 TCPSocket(const TCPSocket&);
50
54 virtual ~TCPSocket();
55
66 virtual bool connect(const char * const Peer, int PeerPort, const char * const Host = NULL, int HostPort = 0);
67
73 virtual bool reconnect();
74
82 virtual std::string toString();
83
92 virtual bool listen(const char * const Host, int Port);
93
102 TCPSocket *accept(std::string *Peer);
103
113 TCPSocket *accept(std::string *Peer, int cancellationFd);
114
123 bool dataToGet(int sec, int usec) const;
124
134 int getBufferStore(bufferStore &a, bool wait = true);
135
142 bool sendBufferStore(const bufferStore &a);
143
149 bool closeSocket(void);
150
159 bool bindSocket(const char * const Host, int Port);
160
175 bool bindInRange(const char * const Host, int Low, int High, int Retries);
176
185 bool linger(bool LingerOn, int LingerTime = 0);
186
195 bool getPeer(std::string *Peer, int *Port);
196
205 bool getHost(std::string *Host, int *Port);
206
214 void setWatch(IOWatch *watch);
215
216private:
220 virtual bool createSocket(void);
221
222 int getLastError(void) { return(m_LastError); }
223 bool setPeer(const char * const Peer, int Port);
224 bool setHost(const char * const Host, int Port);
225 int recv(void *buf, int len, int flags);
226 int send(const void * const buf, int len, int flags);
227
228 struct sockaddr m_HostAddr;
229 struct sockaddr m_PeerAddr;
235};
236
237#endif
A simple thread-safe wrapper for select()
Definition: iowatch.h:34
A class for dealing with sockets.
Definition: tcpsocket.h:38
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:234
int m_Socket
Definition: tcpsocket.h:230
int getLastError(void)
Definition: tcpsocket.h:222
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:231
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:228
TCPSocket()
Constructs a TCPSocket.
Definition: tcpsocket.cc:63
int m_LastError
Definition: tcpsocket.h:233
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
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:232
TCPSocket * accept(std::string *Peer)
Accept a connection; blocking, non-cancellable.
Definition: tcpsocket.cc:208
struct sockaddr m_PeerAddr
Definition: tcpsocket.h:229
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
A generic container for an array of bytes.
Definition: bufferstore.h:37
static rfsv * a
Definition: main.cc:53