summaryrefslogtreecommitdiff
path: root/src/tcpsocket.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/tcpsocket.h')
-rw-r--r--src/tcpsocket.h116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/tcpsocket.h b/src/tcpsocket.h
new file mode 100644
index 0000000..3361e84
--- /dev/null
+++ b/src/tcpsocket.h
@@ -0,0 +1,116 @@
1/*
2 * Copyright (C) 2007-2010 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_TCP_SOCKET_H
9#define BU_TCP_SOCKET_H
10
11#include <stdint.h>
12
13#include "bu/stream.h"
14#include "bu/fstring.h"
15#include "bu/exceptionbase.h"
16
17namespace Bu
18{
19 subExceptionDeclBegin( TcpSocketException );
20 enum {
21 cRead,
22 cWrite,
23 cBadRead,
24 cClosed,
25 cTimeout
26 };
27 subExceptionDeclEnd();
28
29 /**
30 * Network socket stream class. This class provides a mechanism for
31 * communicating over a network using TCP/IP. It will provide other low
32 * level protocol and addressing support later on, but for now it's just
33 * standard STREAM TCP/IP sockets.
34 *
35 * Unlike system sockets, these sockets are opened by default in
36 * non-blocking mode, you can specify your own timeout for opening a socket,
37 * and a number of non-fatal error messages have been automatically handled
38 * and treated as standard no-data-available-yet situations on read.
39 *
40 * Please note that there is a condition that will occur eventually (at
41 * least on *nix systems) that will trigger a SIGPIPE condition. This
42 * will terminate your program immediately unless handled properly. Most
43 * people doing any connections with TcpSocket will want to put this in
44 * their program somewhere before they use it:
45 *@code
46 #include <signal.h>
47 ...
48 ...
49 ...
50 sigset( SIGPIPE, SIG_IGN ); // do this before you use a Bu::TcpSocket
51 @endcode
52 * When this is done, Bu::TcpSocket will simply throw a broken pipe
53 * exception just like every other error condition, allowing your program
54 * to handle it sanely.
55 *
56 *@ingroup Serving
57 *@ingroup Streams
58 */
59 class TcpSocket : public Stream
60 {
61 public:
62 TcpSocket( int nTcpSocket );
63 TcpSocket( const FString &sAddr, int nPort, int nTimeout=30,
64 bool bBlocking=true );
65 virtual ~TcpSocket();
66
67 virtual void close();
68 //virtual void read();
69 virtual size_t read( void *pBuf, size_t nBytes );
70 virtual size_t read( void *pBuf, size_t nBytes,
71 uint32_t nSec, uint32_t nUSec=0 );
72 virtual size_t write( const void *pBuf, size_t nBytes );
73 virtual size_t write( const void *pBuf, size_t nBytes,
74 uint32_t nSec, uint32_t nUSec=0 );
75 using Stream::write;
76
77 virtual long tell();
78 virtual void seek( long offset );
79 virtual void setPos( long pos );
80 virtual void setPosEnd( long pos );
81 virtual bool isEos();
82 virtual bool isOpen();
83
84 virtual void flush();
85
86 virtual bool canRead();
87 virtual bool canWrite();
88
89 virtual bool isReadable();
90 virtual bool isWritable();
91 virtual bool isSeekable();
92
93 virtual bool isBlocking();
94 virtual void setBlocking( bool bBlocking=true );
95
96 virtual void setSize( long iSize );
97
98 Bu::FString getAddress() const;
99 operator int() const;
100
101 private:
102 void setAddress();
103
104#ifdef WIN32
105 unsigned int nTcpSocket;
106#else
107 int nTcpSocket;
108#endif
109 bool bActive;
110 bool bBlocking;
111 FString sReadBuf;
112 FString sAddress;
113 };
114}
115
116#endif