internal static bool TryDeserialize(BinaryReader reader, out Host host) { host = null; HostEntry entry; if (!HostEntry.TryDeserialize(reader, out entry)) { return(false); } string identity; if (!MalockMessage.TryFromStringInReadStream(reader, out identity)) { return(false); } host = new Host(identity, entry); return(true); }
private void InternalRemoteGetHostEntryAsync(string key, int timeout, Action <NnsError, HostEntry> state, bool retrying) { if ((retrying && timeout <= 0) || (timeout <= 0 && timeout != -1)) { state(NnsError.kTimeout, null); } else if (!this.Available) { state(NnsError.kAborted, null); } else { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Action <NnsClient> onaborted = (self) => { if (!this.Available) { state(NnsError.kAborted, null); } else { var delaytick = Malock.NewTimer(); delaytick.Interval = Malock.SmoothingInvokeTime; delaytick.Tick += delegate { long elapsedMilliseconds = stopwatch.ElapsedMilliseconds; { stopwatch.Stop(); delaytick.Stop(); } if (!this.Available) { state(NnsError.kAborted, null); } else { if (timeout != -1) { timeout -= Convert.ToInt32(elapsedMilliseconds); } this.InternalRemoteGetHostEntryAsync(key, timeout, state, timeout == -1 ? false : true); } }; delaytick.Start(); } }; Action <int, MalockMessage, Stream> callback = (errno, response, stream) => { bool closedstopwatch = true; if (errno == MalockMessage.Mappable.ERROR_NOERROR) { if (response.Command != MSG.CLIENT_COMMAND_QUERYHOSTENTRYINFO) { state(NnsError.kError, null); } else { HostEntry entry; if (!HostEntry.TryDeserialize(stream, out entry)) { state(NnsError.kError, null); } else { state(NnsError.kSuccess, entry); } } } else if (errno == MalockMessage.Mappable.ERROR_ABORTED) { closedstopwatch = false; onaborted(this); } else if (errno == MalockMessage.Mappable.ERROR_TIMEOUT) { state(NnsError.kTimeout, null); } if (closedstopwatch) { stopwatch.Stop(); } }; MalockMessage message = this.NewMessage(key, MSG.CLIENT_COMMAND_QUERYHOSTENTRYINFO); Exception exception = null; if (!MalockMessage.TryInvokeAsync(this, message, timeout, callback, ref exception)) { onaborted(this); } } }