windows/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 #ifndef SysCSStream_Included
39 #define SysCSStream_Included
40 
41 // Client Server error codes
42 typedef enum
43 {
53 
55 {
56 public:
58  inline SysSocketConnection(SOCKET sock) : c(sock), errcode(CSERROR_OK), messageBuffer(NULL) { }
59  inline ~SysSocketConnection() { if (messageBuffer != NULL) { free(messageBuffer); } }
61  {
62  return errcode;
63  };
64  bool read(void *buf, size_t bufsize, size_t *bytesread);
65  bool write(void *buf, size_t bufsize, size_t *byteswritten);
66  bool write(void *buf, size_t bufsize, void*buf2, size_t buf2size, size_t *byteswritten);
67 
68 protected:
69  enum
70  {
71  // somewhat arbitrary. Should be large enough for "normal requests"
72  MAX_CACHED_BUFFER = 4096
73  };
74 
75  char *getMessageBuffer(size_t size);
76  void returnMessageBuffer(void *);
77 
78  SOCKET c; // stream socket
79  CSErrorCodeT errcode; // error status
80  char *messageBuffer; // a buffer for message sending
81 };
82 
83 
84 // This is the Client TCP/IP Stream class
86 {
87 public:
89  SysClientStream(const char *name);
90  SysClientStream(const char *host, int port);
92  bool open(const char *);
93  bool open(const char *, int);
94  bool close();
95  // the following APIs are usually not used but are here for completeness
96  // they should be called prior to calling the Open method
97  void setDomain(int newdomain)
98  {
99  domain = newdomain;
100  };
101  void setType(int newtype)
102  {
103  type = newtype;
104  };
105  void setProtocol(int newprotocol)
106  {
107  protocol = newprotocol;
108  };
109  inline bool isClean()
110  {
111  return errcode == CSERROR_OK;
112  }
113 
114 protected:
115  int domain; // the socket domain
116  int type; // the socket type
117  int protocol; // the socket protocol
118 };
119 
120 class SysServerStream;
121 
122 /**
123  * Class to manage a single instance of a server connection.
124  * These are created any time a server stream object accepts
125  * a connection.
126  */
128 {
129 public:
130  SysServerConnection(SysServerStream *s, SOCKET socket);
132 
134  bool disconnect(void);
135 
136 protected:
138  void returnMessageBuffer(void *);
139 
141 };
142 
143 // This is the Server TCP/IP Stream class
144 class SysServerStream
145 {
146 protected:
148  SOCKET s; // server socket
149  int domain; // the socket domain
150  int type; // the socket type
151  int protocol; // the socket protocol
152  int backlog; // backlog for connecting clients
153 
154 public:
156  SysServerStream(const char *name);
157  SysServerStream(int port);
160  {
161  return errcode;
162  };
163  bool make(const char *);
164  bool make(int);
166  bool close();
167  // the following APIs are usually not used but are here for completeness
168  // they should be called prior to calling the Make method
169  inline void setDomain(int newdomain)
170  {
171  domain = newdomain;
172  };
173  inline void setType(int newtype)
174  {
175  type = newtype;
176  };
177  inline void setProtocol(int newprotocol)
178  {
179  protocol = newprotocol;
180  };
181  inline void setBackLog(int newbacklog)
182  {
183  backlog = newbacklog;
184  };
185  inline bool isClean()
186  {
187  return errcode == CSERROR_OK;
188  }
189 };
190 
191 #endif
void setProtocol(int newprotocol)
SysClientStream(const char *name)
void setType(int newtype)
bool open(const char *)
SysClientStream(const char *host, int port)
void setDomain(int newdomain)
bool open(const char *, int)
SysServerStream * server
void returnMessageBuffer(void *)
char * getMessageBuffer()
SysServerConnection(SysServerStream *s, int socket)
bool disconnect(void)
CSErrorCodeT errcode
CSErrorCodeT getError(void)
SysServerStream(const char *name)
void setType(int newtype)
bool make(int)
bool make(const char *)
void setDomain(int newdomain)
SysServerStream(int port)
void setProtocol(int newprotocol)
void setBackLog(int newbacklog)
SysServerConnection * connect()
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)
bool write(void *buf, size_t bufsize, void *buf2, size_t buf2size, size_t *byteswritten)
SysSocketConnection(SOCKET sock)
CSErrorCodeT
@ CSERROR_CONNX_EXISTS
@ CSERROR_OPEN_FAILED
@ CSERROR_UNKNOWN
@ CSERROR_IO_FAILED
@ CSERROR_HOSTNAME_PORT
@ CSERROR_CONNX_FAILED
@ CSERROR_INTERNAL
@ CSERROR_OK