public void TestRemve() { RequestMap map = new RequestMap(); map.Add("key1", "value1"); Assert.AreEqual(1, map.Count); Assert.IsTrue(map.ContainsKey("key1")); Assert.AreEqual("value1", map ["key1"]); //test negative Assert.IsFalse(map.ContainsKey("key2")); map.Remove("key1"); Assert.AreEqual(0, map.Count); Assert.IsFalse(map.ContainsKey("key1")); }
private static async Task Client_OnReactionAdded(Cacheable <IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction reaction) { var message = await cache.GetOrDownloadAsync(); if (!reaction.User.Value.IsBot && RequestMap.ContainsKey(message.Id)) { SongData songData = RequestMap[message.Id]; if (reaction.Emote.Name == "👍") { BotTools.LogLine($"{songData.Requestor}'s request was approved by {reaction.User.Value.Username}"); await DJVettingChannelWebhook.SendMessageAsync($"*{songData.Requestor}'s request was approved by {reaction.User.Value.Username}*"); RequestMap.Remove(message.Id); try { Music.GetPlaylist("request").AddSong(songData); var placement = Music.GetPlaylist("request").Songs.Count; var placementWord = BotTools.GetPlacementWord(placement); TwitchBot.SendMessage($"@{songData.Requestor}, Your request is {placementWord} in the queue! razCool"); Music.QueuedSong = Music.DownloadedSong = null; Music.SavePlaylists(); await message.RemoveAllReactionsAsync(); } catch (ArgumentException) { BotTools.LogLine($"{songData.Requestor}'s request rejected: Already in queue"); TwitchBot.SendMessage($"Sorry @{songData.Requestor}, that request is already queued!"); } catch (FormatException) { BotTools.LogLine($"{songData.Requestor}'s request rejected: Bad url"); TwitchBot.SendMessage($"Sorry @{songData.Requestor}, I can't access any video with that url!"); } } else if (reaction.Emote.Name == "👎") { BotTools.LogLine($"{RequestMap[message.Id].Requestor}'s request was declined by {reaction.User.Value.Username}"); await DJVettingChannelWebhook.SendMessageAsync($"*{RequestMap[message.Id].Requestor}'s request was declined by {reaction.User.Value.Username}*"); TwitchBot.SendMessage($"@{songData.Requestor}, Your request was declined! razHands"); RequestMap.Remove(message.Id); await message.RemoveAllReactionsAsync(); } } }
public static void DownLoad(object param) { ThreadParam tParam = param as ThreadParam; ManualResetEvent close = tParam.close; AutoResetEvent Wait = tParam.waitfor; try { while (bContinue) { Wait.WaitOne(); HttpRequest req = null; string strURI = null; while (bContinue) { req = null; strURI = null; lock (RequestMap) { if (RequestMap.Count == 0) { break; } bool bFindOrder = false; foreach (KeyValuePair <string, HttpRequest> each in RequestMap) { if (req == null) { req = each.Value; } if (strURI == null) { strURI = each.Key; } if (each.Value != null) { if (each.Value.order != RequestStatus.Pause) { req = each.Value; strURI = each.Key; bFindOrder = true; break; } else { continue; } } } if (strURI != null) { //may be is retry lock (DownloadMap) { if (!DownloadMap.ContainsKey(strURI)) { DownloadMap.Add(strURI, req); } } RequestMap.Remove(strURI); } } Socket sClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); //http1.1 download start string strHost = ""; string strPort = ""; string strRelativePath = ""; if (!ParseURI(strURI, ref strHost, ref strPort, ref strRelativePath)) { req.error = "404"; req.callback(ref req); break; } else { EndPoint server = new IPEndPoint(IPAddress.Parse(strHost), System.Convert.ToInt32(strPort)); try { connectDone.Reset(); sClient.BeginConnect(server, new AsyncCallback(ConnectCallback), sClient); connectDone.WaitOne(2000, false); if (!sClient.Connected) { Log.LogInfo("connect time out"); //repush to download again lock (RequestMap) { if (!RequestMap.ContainsKey(strURI)) { req.error = null; req.order = 0; RequestMap.Add(strURI, req); } } continue; } } catch { req.error = "connect failed"; req.callback(ref req); //repush to download again lock (RequestMap) { if (!RequestMap.ContainsKey(strURI)) { req.error = null; req.order = 0; RequestMap.Add(strURI, req); } } continue; } if (!sClient.Connected) { req.error = "connect failed"; req.callback(ref req); //repush to download again lock (RequestMap) { if (!RequestMap.ContainsKey(strURI)) { req.error = null; req.order = 0; RequestMap.Add(strURI, req); } } continue; } string strSend = string.Format(strHttpVer, strRelativePath, strHost, strPort, req.cbstart); Log.LogInfo("send packet:" + strSend); byte[] bySend = System.Text.Encoding.UTF8.GetBytes(string.Format(strHttpVer, strRelativePath, strHost, strPort, req.cbstart)); sClient.Send(bySend); int nByteRecved = 0; int nNewLine = 0; //recv http head MemoryStream ms = new MemoryStream(); byte[] byteRecved = new byte[1]; while (true) { if (!bContinue) { break; } try { nByteRecved = sClient.Receive(byteRecved, 1, 0); } catch (Exception exp) { break; } if (nByteRecved <= 0) { break; } ms.Write(byteRecved, 0, 1); if (byteRecved[0] == '\n') { nNewLine++; if (System.Text.Encoding.UTF8.GetString(ms.GetBuffer()).Contains("\r\n\r\n")) { break; } } } if (!sClient.Connected || !bContinue) { req.error = "recv interrupt"; req.callback(ref req); lock (RequestMap) { if (!RequestMap.ContainsKey(strURI)) { req.error = null; req.order = 0; RequestMap.Add(strURI, req); } } continue; } nByteRecved = 0; string strHead = System.Text.Encoding.UTF8.GetString(ms.GetBuffer()); Log.LogInfo("http recv:" + strHead); string strHeadLower = strHead.ToLower(); //check http1.1 return code int nReturnCode = 0; long nContentLength = 0; int nRangeStart = 0; int nRangeStop = 0; string[] strResponse = new string[nNewLine]; char[] split = { '\n' }; strResponse = strHeadLower.Split(split); for (int i = 0; i < strResponse.Length; ++i) { if (strResponse[i].Contains("http/")) { string strStatus = strResponse[i]; nReturnCode = System.Convert.ToInt32(strStatus.Substring(9, 3)); Log.LogInfo("http result:" + nReturnCode.ToString()); } else if (strResponse[i].Contains("content-length:")) { string strLength = strResponse[i]; string[] strSplit = strLength.Split(new char[] { ' ' }, 2); nContentLength = System.Convert.ToInt64(strSplit[1]); if (req.cbstart == 0) { req.cbsize = nContentLength; req.callback(ref req); } } else if (strResponse[i].Contains("tranfer-encoding:chunked")) { Log.LogInfo("error !!! can not read chunked data"); } if (nReturnCode != 0 && nContentLength != 0) { break; } } ms.Close(); ms = null; if (nReturnCode != 206 && nReturnCode != 200) { req.error = nReturnCode.ToString(); req.callback(ref req); lock (RequestMap) { if (!RequestMap.ContainsKey(strURI)) { req.error = null; req.order = 0; RequestMap.Add(strURI, req); } } sClient.Close(); continue; } FileStream fs = File.Open(req.strLocalPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); if (req.cbstart != 0) { fs.Seek(req.cbstart, SeekOrigin.Begin); } //calc kbps //total recved / total time => kbps long nTotalRecved = req.cbstart; int nLoop = 0; byte[] memory = new byte[10 * 1024]; while (true) { long nTickBegin = DateTime.Now.Ticks; if (!bContinue) { break; } try { nByteRecved = sClient.Receive(memory, 10 * 1024, SocketFlags.None); } catch (Exception exp) { break; } if (nByteRecved <= 0) { break; } nLoop++; //Loader.LogErr("recv bytes:" + nByteRecved.ToString()); fs.Write(memory, 0, nByteRecved); fs.Flush(); nTotalRecved += nByteRecved; req.cboffset = nTotalRecved; req.loadBytes += nByteRecved; if (nTotalRecved == nContentLength) { break; } if (nLoop >= 10) { req.callback(ref req); nLoop = 0; } } sClient.Close(); //Loader.LogErr("file transfer result:" + nByteRecved.ToString()); req.cboffset = fs.Seek(0, SeekOrigin.Current); fs.Flush(); fs.Close(); req.callback(ref req); req.bDone = true; } } } if (close != null) { Log.LogInfo("thread quit signal open"); close.Set(); close = null; } } catch (Exception exp) { Debug.LogError(exp.Message + "|" + exp.StackTrace + " download thread crashed"); } finally { if (close != null) { close.Set(); close = null; } } }