diff options
author | Mike Buland <eichlan@xagasoft.com> | 2023-07-28 21:18:56 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2023-07-28 21:18:56 -0700 |
commit | 915005e218b5d00939b548de65ce6354f7acb487 (patch) | |
tree | 2f624a37f86f97cfd61c1995df7e4368b462bcac /src/stable/sockettcp.h | |
parent | e43a2cac32cb773994b11a3d964ec4acc372d273 (diff) | |
download | libbu++-915005e218b5d00939b548de65ce6354f7acb487.tar.gz libbu++-915005e218b5d00939b548de65ce6354f7acb487.tar.bz2 libbu++-915005e218b5d00939b548de65ce6354f7acb487.tar.xz libbu++-915005e218b5d00939b548de65ce6354f7acb487.zip |
Completely redesigned Server and Client.
Like, seriously, they're almost completely different.
Diffstat (limited to 'src/stable/sockettcp.h')
-rw-r--r-- | src/stable/sockettcp.h | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/stable/sockettcp.h b/src/stable/sockettcp.h new file mode 100644 index 0000000..3fc14ef --- /dev/null +++ b/src/stable/sockettcp.h | |||
@@ -0,0 +1,128 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2019 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_SOCKET_TCP_H | ||
9 | #define BU_SOCKET_TCP_H | ||
10 | |||
11 | #include <stdint.h> | ||
12 | |||
13 | #include "bu/config.h" | ||
14 | #include "bu/socket.h" | ||
15 | #include "bu/string.h" | ||
16 | #include "bu/exceptionbase.h" | ||
17 | |||
18 | namespace Bu | ||
19 | { | ||
20 | subExceptionDeclBegin( SocketTcpException ); | ||
21 | enum { | ||
22 | cRead, | ||
23 | cWrite, | ||
24 | cBadRead, | ||
25 | cClosed, | ||
26 | cTimeout | ||
27 | }; | ||
28 | subExceptionDeclEnd(); | ||
29 | |||
30 | /** | ||
31 | * Network socket stream class. This class provides a mechanism for | ||
32 | * communicating over a network using TCP/IP. It will provide other low | ||
33 | * level protocol and addressing support later on, but for now it's just | ||
34 | * standard STREAM TCP/IP sockets. | ||
35 | * | ||
36 | * Unlike system sockets, these sockets are opened by default in | ||
37 | * non-blocking mode, you can specify your own timeout for opening a socket, | ||
38 | * and a number of non-fatal error messages have been automatically handled | ||
39 | * and treated as standard no-data-available-yet situations on read. | ||
40 | * | ||
41 | * Please note that there is a condition that will occur eventually (at | ||
42 | * least on *nix systems) that will trigger a SIGPIPE condition. This | ||
43 | * will terminate your program immediately unless handled properly. Most | ||
44 | * people doing any connections with SocketTcp will want to put this in | ||
45 | * their program somewhere before they use it: | ||
46 | *@code | ||
47 | #include <signal.h> | ||
48 | ... | ||
49 | ... | ||
50 | ... | ||
51 | sigset( SIGPIPE, SIG_IGN ); // do this before you use a Bu::SocketTcp | ||
52 | @endcode | ||
53 | * When this is done, Bu::SocketTcp will simply throw a broken pipe | ||
54 | * exception just like every other error condition, allowing your program | ||
55 | * to handle it sanely. | ||
56 | * | ||
57 | *@ingroup Serving | ||
58 | *@ingroup Streams | ||
59 | */ | ||
60 | class SocketTcp : public Socket | ||
61 | { | ||
62 | public: | ||
63 | #ifdef WIN32 | ||
64 | typedef unsigned int handle; | ||
65 | #else | ||
66 | typedef int handle; | ||
67 | #endif | ||
68 | |||
69 | SocketTcp( handle nSocketTcp ); | ||
70 | SocketTcp( const String &sAddr, int nPort, int nTimeout=30, | ||
71 | bool bBlocking=true ); | ||
72 | virtual ~SocketTcp(); | ||
73 | |||
74 | virtual void close(); | ||
75 | virtual size read( void *pBuf, size nBytes ); | ||
76 | virtual size read( void *pBuf, size nBytes, | ||
77 | uint32_t nSec, uint32_t nUSec=0 ); | ||
78 | virtual size write( const void *pBuf, size nBytes ); | ||
79 | virtual size write( const void *pBuf, size nBytes, | ||
80 | uint32_t nSec, uint32_t nUSec=0 ); | ||
81 | using Stream::write; | ||
82 | |||
83 | virtual size tell(); | ||
84 | virtual void seek( size offset ); | ||
85 | virtual void setPos( size pos ); | ||
86 | virtual void setPosEnd( size pos ); | ||
87 | virtual bool isEos(); | ||
88 | virtual bool isOpen(); | ||
89 | |||
90 | virtual void flush(); | ||
91 | |||
92 | virtual bool canRead(); | ||
93 | virtual bool canWrite(); | ||
94 | |||
95 | virtual bool isReadable(); | ||
96 | virtual bool isWritable(); | ||
97 | virtual bool isSeekable(); | ||
98 | |||
99 | virtual bool isBlocking(); | ||
100 | virtual void setBlocking( bool bBlocking=true ); | ||
101 | |||
102 | virtual void setSize( size iSize ); | ||
103 | |||
104 | Bu::String getAddress() const; | ||
105 | operator handle() const; | ||
106 | |||
107 | handle getHandle() const; | ||
108 | handle takeHandle(); | ||
109 | |||
110 | virtual size getSize() const; | ||
111 | virtual size getBlockSize() const; | ||
112 | virtual Bu::String getLocation() const; | ||
113 | |||
114 | virtual bool getFd( int &rFdOut ) const; | ||
115 | |||
116 | private: | ||
117 | void setAddress(); | ||
118 | |||
119 | handle nSocketTcp; | ||
120 | |||
121 | bool bActive; | ||
122 | bool bBlocking; | ||
123 | String sReadBuf; | ||
124 | String sAddress; | ||
125 | }; | ||
126 | } | ||
127 | |||
128 | #endif | ||