ServiceMessage.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 
39 #include "ServiceMessage.hpp"
40 #include "ServiceException.hpp"
41 #include "SysAPIManager.hpp"
42 
43 
45 {
48  session = 0;
49  parameter1 = 0;
50  parameter2 = 0;
51  parameter3 = 0;
52  parameter4 = 0;
53  parameter5 = 0;
56  messageData = NULL;
58  retainMessageData = false;
59  nameArg[0] = '\0';
60  userid[0] = '\0';
61 }
62 
63 /**
64  * Read a message from the server-side stream.
65  *
66  * @param server The server stream that has already received a connection message.
67  */
69 {
70  size_t actual = 0;
71  size_t required = sizeof(ServiceMessage);
72  size_t offset = 0;
73  while (required > 0)
74  {
75  if (!connection->read(((char *)this) + offset, required, &actual) || actual == 0)
76  {
77  throw new ServiceException(SERVER_FAILURE, "ServiceMessage::readMessage() Failure reading service message");
78  }
79  required -= actual;
80  offset += actual;
81  }
82 
83  // does this message have extra data associated with it?
84  if (messageDataLength != 0)
85  {
86  // allocate a data buffer for the extra information and read it in.
88  if (messageData == NULL)
89  {
90  // this will close this connection and raise an error back in the caller
91  throw new ServiceException(SERVER_FAILURE, "ServiceMessage::readMessage() Failure allocating message buffer");
92  }
93  required = messageDataLength;
94  offset = 0;
95  while (required > 0)
96  {
97  if (!connection->read(((char *)messageData) + offset, required, &actual) || actual == 0)
98  {
100  // make sure these are cleared out
101  messageData = NULL;
102  messageDataLength = 0;
103  throw new ServiceException(SERVER_FAILURE, "ServiceMessage::readMessage() Failure reading service message");
104  }
105  // add in the count
106  required -= actual;
107  offset += actual;
108  }
109  // this is a releasable value
110  retainMessageData = false;
111  }
112 }
113 
114 
115 /**
116  * Write a server side message result back to the client.
117  *
118  * @param server The server message stream used to receive the original message.
119  */
121 {
122  size_t expected = sizeof(ServiceMessage) + messageDataLength;
123  size_t actual = 0;
124  if (!connection->write((void *)this, sizeof(ServiceMessage), messageData, messageDataLength, &actual) || actual != expected)
125  {
126  freeMessageData();
127  throw new ServiceException(SERVER_FAILURE, "ServiceMessage::writeResult() Failure writing service message result");
128  }
129  // we might be sending a copy of data that's still resident in the connection-> If
130  // we are, then don't delete the message data after doing the send.
131  // free the message data after the send.
132  freeMessageData();
133 }
134 
135 
136 /**
137  * Write a message over to the connection->
138  *
139  * @param pipe The pipe we've opened to write the message.
140  */
142 {
143  size_t actual = 0;
144  size_t expected = sizeof(ServiceMessage) + messageDataLength;
145  if (!pipe.write((void *)this, sizeof(ServiceMessage), messageData, messageDataLength, &actual) || actual != expected)
146  {
147  freeMessageData();
148  throw new ServiceException(SERVER_FAILURE, "ServiceMessage::writeResult() Failure writing service message result");
149  }
150  // make sure we free and release any attached data before proceeding
151  freeMessageData();
152 }
153 
154 
155 /**
156  * Read a message result back from a server message.
157  *
158  * @param pipe The connection used to send the original message.
159  */
161 {
162  size_t actual = 0;
163  size_t required = sizeof(ServiceMessage);
164  size_t offset = 0;
165  while (required > 0)
166  {
167  if (!pipe.read(((char *)this) + offset, required, &actual) || actual == 0)
168  {
169  throw new ServiceException(SERVER_FAILURE, "ServiceMessage::readResult() Failure reading service message");
170  }
171  required -= actual;
172  offset += actual;
173  }
174  // the server sets this value to what is required required on its end. We allocated this memory
175  // when we read it in on this end, so mark it for freeing initially. If it should not
176  // be freed, the sender of the message needs to take ownership.
177  retainMessageData = false;
178 
179  // handle any errors that the server side might have raised.
181 
182  // does this message have extra data associated with it?
183  if (messageDataLength != 0)
184  {
185  // allocate a data buffer for the extra information and read it in.
186  // we add a courtesy null terminator on this message, so we read one extra bit
188  if (messageData == NULL)
189  {
190  // this will close this connection and raise an error back in the caller
191  throw new ServiceException(SERVER_FAILURE, "ServiceMessage::readResult() Failure allocating message buffer");
192  }
193  ((char *)messageData)[messageDataLength] = '\0';
194  required = messageDataLength;
195  offset = 0;
196  while (required > 0)
197  {
198  if (!pipe.read(((char *)messageData) + offset, required, &actual) || actual == 0)
199  {
201  throw new ServiceException(SERVER_FAILURE, "ServiceMessage::readResult() Failure reading service message");
202  }
203  // remove the amount read
204  required -= actual;
205  offset += actual;
206  }
207  }
208  else
209  {
210  // make sure this is nulled out
211  messageData = NULL;
212  }
213 }
214 
215 
216 /**
217  * Allocate a buffer for result information returned by a service
218  * message. This is allocated using the Rexx API allocation
219  * routine so that the memory can be handed out and deallocated
220  * by an API caller.
221  *
222  * @param length The length to allocate.
223  *
224  * @return A data pointer with the result memory.
225  */
227 {
228  void *data = SysAPIManager::allocateMemory(length);
229  if (data == NULL)
230  {
231  throw new ServiceException(MEMORY_ERROR, "ServiceMessage::allocateResultMemory() Failure allocating result memory");
232  }
233  return data;
234 }
235 
236 /**
237  * Free result memory in the case of a read failure.
238  *
239  * @param data The data to be released.
240  */
242 {
244 }
@ MEMORY_ERROR
@ SERVER_FAILURE
@ NO_ERROR_CODE
@ MESSAGE_OK
@ CONNECTION_ACTIVE
@ APIManager
ServerManager messageTarget
void readResult(SysClientStream &server)
void readMessage(SysServerConnection *server)
uintptr_t parameter3
ServerOperation operation
char userid[MAX_USERID_LENGTH]
char nameArg[NAMESIZE]
static void releaseResultMemory(void *mem)
void writeResult(SysServerConnection *server)
uintptr_t parameter1
uintptr_t parameter4
static void * allocateResultMemory(size_t length)
void writeMessage(SysClientStream &server)
uintptr_t parameter5
ServiceReturn result
uintptr_t parameter2
static void * allocateMemory(size_t length)
static void releaseMemory(void *p)
bool write(void *buf, size_t bufsize, size_t *byteswritten)
bool read(void *buf, size_t bufsize, size_t *bytesread)