/** <summary>Writer thread.</summary> */ private void writePackets() { try { bool take = true; while (true) { MemoryStream msg; if (take) { msg = q.Take(); take = false; } else { q.TryTake(out msg); } if (msg == null) { take = true; lock (outStream) { outStream.Flush(); } continue; } lock (outStream) { msg.WriteTo(outStream); } } } catch (IOException e) { if (!closed) { Dbg.WriteLine("Failed to write data to socket (will close connection)" + " [addr=" + ServerAddress + ", e=" + e.Message + "]"); } } catch (Exception e) { if (!closed) { Dbg.WriteLine("Unexpected throwable in connection writer thread (will close connection)" + " [addr={0}, e={1}]", ServerAddress, e); } } finally { U.Async(() => Close(false)); } }
/** * <summary> * Finishes user request to the grid.</summary> * * <param name="fut">Future used to pass result to the user.</param> * <param name="resp">Incoming response message.</param> */ private void finishRequest(GridClientFuture fut, GridClientResponse resp) { switch (resp.Status) { case GridClientResponseStatus.Success: fut.Done(resp.Result); break; case GridClientResponseStatus.Failed: var error = resp.ErrorMessage; if (error == null) { error = "Unknown server error"; } fut.Fail(() => { throw new GridClientException(error); }); break; case GridClientResponseStatus.AuthFailure: fut.Fail(() => { throw new GridClientAuthenticationException("Authentication failed (session expired?)" + " errMsg=" + resp.ErrorMessage + ", [srv=" + ServerAddress + "]"); }); U.Async(() => Close(false)); break; case GridClientResponseStatus.AuthorizationFailure: fut.Fail(() => { throw new GridClientException("Client authorization failed: " + resp.ErrorMessage); }); break; default: fut.Fail(() => { throw new GridClientException("Unknown server response status code: " + resp.Status); }); break; } pendingReqs.Remove(resp.RequestId); }
/** * <summary> * Creates new future and passes it to the MakeJettyRequest.</summary> * * <param name="args">Request parameters.</param> * <param name="destNodeId">Node ID to route request to.</param> * <param name="converter">Response converter to pass into generated future.</param> * <returns>Future.</returns> * <exception cref="GridClientClosedException">If client was manually closed.</exception> */ private IGridClientFuture <T> MakeJettyRequest <T>(Guid destNodeId, IDictionary <String, Object> args, Func <Object, T> converter) { Dbg.Assert(args != null); Dbg.Assert(args.ContainsKey("cmd")); if (destNodeId != null && destNodeId != Guid.Empty) { args.Add("destId", destNodeId.ToString()); } var fut = new GridClientFuture <T>(); fut.DoneConverter = converter; busyLock.AcquireReaderLock(Timeout.Infinite); try { if (closed) { throw new GridClientConnectionResetException("Failed to perform request (connection was closed" + " before request is sent): " + ServerAddress); } lock (pendingReqs) { pendingReqs.Add(fut); } U.Async(() => { try { OnResponse(args, fut, LoadString(args, sessionToken)); } catch (Exception e) { fut.Fail(() => { throw new GridClientException(e.Message, e); }); } }); return(fut); } finally { busyLock.ReleaseReaderLock(); } }
/** <summary>Reader thread.</summary> */ private void readPackets() { try { bool running = true; while (running) { // Note that only this thread removes futures from map. // So if we see closed condition, it is safe to check map size since no more futures // will be added to the map. if (closed) { // Exit if either all requests processed or we do not wait for completion. if (!waitCompletion) { break; } lock (pendingReqs) { if (pendingReqs.Count == 0) { break; } } } // Header. int symbol; try { symbol = readByte(); } catch (TimeoutException) { checkPing(); continue; } // Connection closed. if (symbol == -1) { Dbg.WriteLine("Connection closed by remote host " + "[srvAddr=" + ServerAddress + ", symbol=" + symbol + "]"); break; } // Check for correct header. if ((byte)symbol != (byte)0x90) { Dbg.WriteLine("Failed to parse incoming message (unexpected header received, will close) " + "[srvAddr=" + ServerAddress + ", symbol=" + symbol + "]"); break; } // Packet. MemoryStream buf = new MemoryStream(); int len = 0; while (true) { try { symbol = readByte(); } catch (TimeoutException) { checkPing(); continue; } if (symbol == -1) { running = false; break; } byte b = (byte)symbol; buf.WriteByte(b); if (len == 0) { if (buf.Length == 4) { len = U.BytesToInt32(buf.ToArray(), 0); // Reset buffer. buf.SetLength(0); if (len == 0) { // Ping received. lastPingRcvTime = U.Now; break; } } } else { if (buf.Length == len) { if (len < 40) { Dbg.WriteLine("Invalid packet received [len=" + len + ", buf=" + buf + "]"); break; } buf.Position = 0; // Rewind buffer. byte[] head = new byte[40]; byte[] packet = new byte[len - head.Length]; buf.Read(head, 0, head.Length); buf.Read(packet, 0, packet.Length); GridClientResponse msg = marshaller.Unmarshal <GridClientResponse>(packet); msg.RequestId = U.BytesToInt64(head, 0); msg.ClientId = U.BytesToGuid(head, 8); msg.DestNodeId = U.BytesToGuid(head, 24); // Reset buffer. buf.SetLength(0); len = 0; lastPacketRcvTime = U.Now; handleResponse(msg); break; } } } } } catch (IOException e) { if (!closed) { Dbg.WriteLine("Failed to read data from remote host (will close connection)" + " [addr=" + ServerAddress + ", e=" + e.Message + "]"); } } catch (Exception e) { Dbg.WriteLine("Unexpected throwable in connection reader thread (will close connection)" + " [addr={0}, e={1}]", ServerAddress, e); } finally { U.Async(() => Close(false)); } }
/** <summary>Reader thread.</summary> */ private void readPackets() { try { bool running = true; byte[] lenByte = new byte[4]; byte[] head = new byte[40]; while (running) { // Note that only this thread removes futures from map. // So if we see closed condition, it is safe to check map size since no more futures // will be added to the map. if (closed) { // Exit if either all requests processed or we do not wait for completion. if (!waitCompletion) { break; } if (pendingReqs.Count == 0) { break; } } // Header. int symbol; try { if (lastReadTimedOut) { lastReadTimedOut = false; if (isSslStream) { // Recover SSL stream state after socket exception. skipSslDataRecordHeader(); } } symbol = inStream.ReadByte(); } catch (Exception e) { if (e.InnerException is SocketException) { e = e.InnerException; } var sockEx = e as SocketException; if (sockEx != null && sockEx.ErrorCode == 10060) { checkPing(); lastReadTimedOut = true; continue; } // All other exceptions are interpreted as stream ends. throw; } // Connection closed. if (symbol == -1) { Dbg.WriteLine("Connection closed by remote host " + "[srvAddr=" + ServerAddress + ", symbol=" + symbol + "]"); break; } // Check for correct header. if ((byte)symbol != (byte)0x90) { Dbg.WriteLine("Failed to parse incoming message (unexpected header received, will close) " + "[srvAddr=" + ServerAddress + ", symbol=" + symbol + "]"); break; } U.ReadFully(inStream, lenByte, 0, 4); int len = U.BytesToInt32(lenByte, 0); if (len == 0) { // Ping received. lastPingRcvTime = U.Now; continue; } if (len < 40) { Dbg.WriteLine("Invalid packet received [len=" + len + "]"); break; } U.ReadFully(inStream, head, 0, 40); long reqId = U.BytesToInt64(head, 0); Guid clientId = U.BytesToGuid(head, 8); Guid destNodeId = U.BytesToGuid(head, 24); byte[] msgBytes = new byte[len - 40]; U.ReadFully(inStream, msgBytes, 0, msgBytes.Length); GridClientResponse msg = marshaller.Unmarshal <GridClientResponse>(msgBytes); msg.RequestId = reqId; msg.ClientId = clientId; msg.DestNodeId = destNodeId; lastPacketRcvTime = U.Now; handleResponse(msg); } } catch (IOException e) { if (!closed) { Dbg.WriteLine("Failed to read data from remote host (will close connection)" + " [addr=" + ServerAddress + ", e=" + e.Message + "]"); } } catch (Exception e) { Dbg.WriteLine("Unexpected throwable in connection reader thread (will close connection)" + " [addr={0}, e={1}]", ServerAddress, e); } finally { U.Async(() => Close(false)); } }