private static void ReceiveMessageAsyncWorker(WebSocket webSocket, WebSocketMessageType type, byte[] bytes, Action <byte[], WebSocketCloseStatus, string> callback) { var buffer = new byte[bufferSize]; webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None).ContinueWith(task => { WebSocketReceiveResult result; if (!MiscHelpers.Try(out result, () => task.Result) || (result == null)) { callback(null, WebSocketCloseStatus.ProtocolError, "Could not receive data from web socket"); } else if (result.MessageType == WebSocketMessageType.Close) { callback(null, result.CloseStatus ?? WebSocketCloseStatus.Empty, MiscHelpers.EnsureNonBlank(result.CloseStatusDescription, "Could not determine web socket close status")); } else if (result.MessageType != type) { callback(null, WebSocketCloseStatus.ProtocolError, "Received unrecognized data from web socket"); } else { bytes = bytes.Concat(buffer.Take(result.Count)).ToArray(); if (!result.EndOfMessage) { ReceiveMessageAsyncWorker(webSocket, type, bytes, callback); } else { callback(bytes, WebSocketCloseStatus.Empty, null); } } }); }
public string GetUniqueName(string inputName, string alternate) { lock (map) { var nonBlankName = MiscHelpers.EnsureNonBlank(inputName, alternate); map.TryGetValue(nonBlankName, out var count); map[nonBlankName] = ++count; return((count < 2) ? nonBlankName : string.Concat(nonBlankName, " [", count, "]")); } }
public string GetUniqueName(string inputName, string alternate) { lock (mapLock) { var nonBlankName = MiscHelpers.EnsureNonBlank(inputName, alternate); uint count; map.TryGetValue(nonBlankName, out count); map[nonBlankName] = ++count; return((count < 2) ? nonBlankName : MiscHelpers.FormatInvariant("{0} [{1}]", nonBlankName, count)); } }
public string GetUniqueName(string inputName, string alternate) { lock (map) { var nonBlankName = MiscHelpers.EnsureNonBlank(Path.GetFileNameWithoutExtension(inputName), alternate); var extension = Path.GetExtension(inputName); map.TryGetValue(nonBlankName, out var count); map[nonBlankName] = ++count; return((count < 2) ? string.Concat(nonBlankName, extension) : string.Concat(nonBlankName, " [", count, "]", extension)); } }
public string GetUniqueName(string inputName, string alternate) { lock (mapLock) { var nonBlankName = MiscHelpers.EnsureNonBlank(Path.GetFileNameWithoutExtension(inputName), alternate); uint count; map.TryGetValue(nonBlankName, out count); map[nonBlankName] = ++count; var uniqueName = (count < 2) ? nonBlankName : MiscHelpers.FormatInvariant("{0} [{1}]", nonBlankName, count); return(Path.HasExtension(inputName) ? Path.ChangeExtension(uniqueName, Path.GetExtension(inputName)) : uniqueName); } }