private Message DoProcess(Message messageStream, int tryCount) { TcpConnection c = null; try { // Check if there's a connection in the pool already, c = connector.GetConnection(address); lock (c) { // Write the message. char code = '\0'; if (serviceType == ServiceType.Manager) { code = 'm'; } else if (serviceType == ServiceType.Root) { code = 'r'; } else if (serviceType == ServiceType.Block) { code = 'b'; } else if (serviceType == ServiceType.Admin) { code = 'a'; } BinaryWriter writer = new BinaryWriter(c.Stream, Encoding.Unicode); writer.Write(code); IMessageSerializer serializer = new BinaryRpcMessageSerializer(); serializer.Serialize(messageStream, c.Stream); writer.Flush(); Message response = serializer.Deserialize(c.Stream, MessageType.Response); if (response is MessageStream) { return response; } else { return new ResponseMessage((RequestMessage) messageStream, (ResponseMessage) response); } } } catch (Exception e) { // If this is a 'connection reset by peer' error, wipe the connection // from the cache and retry connection, if (tryCount == 0 && (e is SocketException || e is EndOfStreamException)) { connector.InvalidateConnection(address); // And retry, return DoProcess(messageStream, tryCount + 1); } MessageError error; if (e is EndOfStreamException) { error = new MessageError(new Exception("EOF (is net password correct?)", e)); } else { // Report this error as a msg_stream fault, error = new MessageError(new Exception(e.Message, e)); } Message responseMessage; if (messageStream is MessageStream) { responseMessage = new MessageStream(MessageType.Response); ResponseMessage inner = new ResponseMessage(); inner.Arguments.Add(error); ((MessageStream)responseMessage).AddMessage(inner); } else { responseMessage = ((RequestMessage) messageStream).CreateResponse(); responseMessage.Arguments.Add(error); } return responseMessage; } finally { if (c != null) connector.ReleaseConnection(c); } }
private Message DoProcess(Message messageStream, int tryCount) { try { HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(address.ToUri()); lock (request) { // Write the message. request.Headers["Service-Type"] = serviceType.ToString(); if (!String.IsNullOrEmpty(connector.userName) && !String.IsNullOrEmpty(connector.password)) request.Credentials = new NetworkCredential(connector.userName, connector.password); request.Method = "POST"; Stream output = request.GetRequestStream(); connector.MessageSerializer.Serialize(messageStream, output); output.Flush(); output.Close(); HttpWebResponse response = (HttpWebResponse) request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) throw new InvalidOperationException(); Stream input = response.GetResponseStream(); ResponseMessage baseResponse = (ResponseMessage) connector.MessageSerializer.Deserialize(input, MessageType.Response); return new ResponseMessage((RequestMessage)messageStream, baseResponse); } } catch (Exception e) { if (tryCount == 0 && e is WebException) // retry ... return DoProcess(messageStream, tryCount + 1); MessageError error; if (e is WebException) { error = new MessageError(new Exception("Web Error: maybe a timeout in the request.", e)); } else { // Report this error as a msg_stream fault, error = new MessageError(new Exception(e.Message, e)); } Message responseMessage; if (messageStream is MessageStream) { responseMessage = MessageStream.NewResponse(); ResponseMessage errorMessage = new ResponseMessage("error"); errorMessage.Arguments.Add(error); ((MessageStream)responseMessage).AddMessage(errorMessage); } else { responseMessage = ((RequestMessage) messageStream).CreateResponse("error"); responseMessage.Arguments.Add(error); } return responseMessage; } }
public MessageErrorException(MessageError error) { this.error = error; }