unix/SysCSStream.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
4 /* */
5 /* This program and the accompanying materials are made available under */
6 /* the terms of the Common Public License v1.0 which accompanies this */
7 /* distribution. A copy is also available at the following address: */
8 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
9 /* */
10 /* Redistribution and use in source and binary forms, with or */
11 /* without modification, are permitted provided that the following */
12 /* conditions are met: */
13 /* */
14 /* Redistributions of source code must retain the above copyright */
15 /* notice, this list of conditions and the following disclaimer. */
16 /* Redistributions in binary form must reproduce the above copyright */
17 /* notice, this list of conditions and the following disclaimer in */
18 /* the documentation and/or other materials provided with the distribution. */
19 /* */
20 /* Neither the name of Rexx Language Association nor the names */
21 /* of its contributors may be used to endorse or promote products */
22 /* derived from this software without specific prior written permission. */
23 /* */
24 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
25 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
26 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
27 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
28 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
29 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
30 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
31 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
32 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
33 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
34 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
35 /* */
36 /*----------------------------------------------------------------------------*/
37 
38 
39 #ifndef SysCSStream_Included
40 #define SysCSStream_Included
41 
42 #include <netinet/in.h>
43 
44 
45 // Client Server error codes
46 typedef enum
47 {
57 
59 {
60 public:
62  inline SysSocketConnection(int sock) : c(sock), errcode(CSERROR_OK), messageBuffer(NULL) { }
63  inline ~SysSocketConnection() { if (messageBuffer != NULL) { free(messageBuffer); } }
65  {
66  return errcode;
67  };
68  bool read(void *buf, size_t bufsize, size_t *bytesread);
69  bool write(void *buf, size_t bufsize, size_t *byteswritten);
70  bool write(void *buf, size_t bufsize, void*buf2, size_t buf2size, size_t *byteswritten);
71 
72 protected:
73  enum
74  {
75  // somewhat arbitrary. Should be large enough for "normal requests"
76  MAX_CACHED_BUFFER = 4096
77  };
78 
79  char *getMessageBuffer(size_t size);
80  void returnMessageBuffer(void *);
81 
82  int c; // stream socket
83  CSErrorCodeT errcode; // error status
84  char *messageBuffer; // a buffer for message sending
85 };
86 
87 
88 // This is the Client TCP/IP Stream class
90 {
91 public:
93  SysClientStream(const char *name);
94  SysClientStream(const char *host, int port);
97  {
98  return errcode;
99  };
100  bool open(const char *);
101  bool open(const char *, int);
102  bool close();
103  // the following APIs are usually not used but are here for completeness
104  // they should be called prior to calling the Open method
105  void setDomain(int newdomain)
106  {
107  domain = newdomain;
108  };
109  void setType(int newtype)
110  {
111  type = newtype;
112  };
113  void setProtocol(int newprotocol)
114  {
115  protocol = newprotocol;
116  };
117  inline bool isClean()
118  {
119  return errcode == CSERROR_OK;
120  }
121 
122 protected:
123  int domain; // the socket domain
124  int type; // the socket type
125  int protocol; // the socket protocol
126 };
127 
128 class SysServerStream;
129 
130 /**
131  * Class to manage a single instance of a server connection.
132  * These are created any time a server stream object accepts
133  * a connection.
134  */
136 {
137 public:
138  SysServerConnection(SysServerStream *s, int socket);
140 
141  bool isLocalConnection();
142  bool disconnect(void);
143 
144 protected:
146 };
147 
148 // This is the Server TCP/IP Stream class
150 {
151 protected:
153  int s; // server socket
154  int domain; // the socket domain
155  int type; // the socket type
156  int protocol; // the socket protocol
157  int backlog; // backlog for connecting clients
158 
159 public:
160  SysServerStream();
161  SysServerStream(const char *name);
162  SysServerStream(int port);
165  {
166  return errcode;
167  };
168  bool make(const char *);
169  bool make(int);
171  bool close();
172  // the following APIs are usually not used but are here for completeness
173  // they should be called prior to calling the Make method
174  inline void setDomain(int newdomain)
175  {
176  domain = newdomain;
177  };
178  inline void setType(int newtype)
179  {
180  type = newtype;
181  };
182  inline void setProtocol(int newprotocol)
183  {
184  protocol = newprotocol;
185  };
186  inline void setBackLog(int newbacklog)
187  {
188  backlog = newbacklog;
189  };
190  inline bool isClean()
191  {
192  return errcode == CSERROR_OK;
193  }
194 };
195 #endif
void setProtocol(int newprotocol)
void setType(int newtype)
bool open(const char *)
CSErrorCodeT getError(void)
void setDomain(int newdomain)
SysServerStream * server
SysServerConnection(SysServerStream *s, int socket)
CSErrorCodeT errcode
CSErrorCodeT getError(void)
void setType(int newtype)
bool make(const char *)
SysServerConnection * connect()
void setDomain(int newdomain)
void setProtocol(int newprotocol)
void setBackLog(int newbacklog)
void returnMessageBuffer(void *)
char * getMessageBuffer(size_t size)
bool write(void *buf, size_t bufsize, size_t *byteswritten)
bool read(void *buf, size_t bufsize, size_t *bytesread)
CSErrorCodeT getError(void)
CSErrorCodeT
@ CSERROR_CONNX_EXISTS
@ CSERROR_OPEN_FAILED
@ CSERROR_UNKNOWN
@ CSERROR_IO_FAILED
@ CSERROR_HOSTNAME_PORT
@ CSERROR_CONNX_FAILED
@ CSERROR_INTERNAL
@ CSERROR_OK