private string DownloadFile(string url, string fileId) { string path = System.IO.Path.Combine(dirPath, url.Substring(url.LastIndexOf('/') + 1)); string fileName = path.Substring(path.LastIndexOf('/') + 1); string endName = fileName.Substring(fileName.LastIndexOf('.')); path = path.Replace(fileName, fileId + endName); bool status = false; try { HttpSimpleMgr.DownloadHttp(url, path); status = true; } catch (NullReferenceException e) { LogAgent.LogError(e.ToString()); if (File.Exists(path)) { File.Delete(path); } } Interlocked.Exchange(ref isDownLoading, 0); if (status) { return(path); } else { return(string.Empty); } }
/*服务器通用*/ public void Init() { if (!InitUdpServer()) { LogAgent.LogError("Udp服务器初始化失败!\r\n"); } //if (InitTcpServer()) //{ // LogAgent.LogError("Tcp服务器初始化失败\r\n"); //} }
private string GetMsgFromHttpByPost(string url, Dictionary <string, string> param) { try { string content = HttpSimpleMgr.HttpGetStrContentByPost(url, param); return(content); } catch (NullReferenceException e) { LogAgent.LogError(e.ToString()); return(string.Empty); } }
public void Start(ISourceBlock sourceBlock) { if (m_Disposed) { throw new ObjectDisposedException(null, "DataflowNetwork already disposed."); } try { //Begin processing the soruce StartProcessingSource(sourceBlock); // Block and wait for all the dataflow-network constituents to complete their work. var waitForTasks = PendingTasks.ToArray(); if (waitForTasks.Length > 0) { Task.WaitAll(waitForTasks.ToArray(), Cts.Token); } LogAgent.LogInfo(DataflowNetworkConstituent.Network, Name, "Network finished successfully."); OutputStatistics(); LogAgent.Complete(); } catch (AggregateException ex) { LogAgent.LogError(DataflowNetworkConstituent.Network, Name, ex.Flatten(), "The network encountered an unhandled exception."); throw new DataflowNetworkFailedException("The datanetwork " + Name + " failed. See the inner exception for more details", ex); } catch (OperationCanceledException) { var exceptions = PendingTasks.Where(task => task.IsFaulted).Select(task => task.Exception); foreach (var ex in exceptions) { LogAgent.LogError(DataflowNetworkConstituent.Network, Name, ex.Flatten(), "The network encountered an unhandled exception."); } } catch (Exception ex) { LogAgent.LogError(DataflowNetworkConstituent.Network, Name, ex, "The network encountered an unhandled exception."); throw new DataflowNetworkFailedException("The datanetwork " + Name + " failed. See the inner exception for more details", ex); } finally { DisposePendingTasks(); Dispose(); } }
private bool CreateTcpClient(IPEndPoint locolIpEnd, IPEndPoint serverIpEnd) { this.tcpServer = new ServerWithTcp(); try { TcpClient client = new TcpClient(locolIpEnd); client.Connect(serverIpEnd); this.tcpServer.Init(client); return(true); } catch (Exception e) { LogAgent.LogError("服务器连接失败!\r\n" + e.ToString()); this.tcpServer.DestoryTcpServer(); return(false); } }
private void ReceiveMessage() { while (true) { Thread.Sleep(1); try { UdpResponseBase reponse = null; lock (mainLock) { switch (status) { case 1: break; case 2: continue; default: goto StopServer; } byte[] data = udp2ClientResponse.Receive(ref receiveClientIpend);//默认每次只接收一个包 data = CheckPack(data); if (data == null) { continue; } reponse = GetReponsePackage(data); } AddReqPackage(reponse); } catch (Exception e) { LogAgent.LogError("Udp消息接收失败\t" + e.ToString()); } } StopServer: Interlocked.Add(ref status, 1); LogAgent.Log("!关闭Udp循环线程"); }
private void AcceptTcpClient() { while (true) { switch (status) { case 1: break; case 2: continue; default: goto StopServer; } if (tcpListener.Pending()) { TcpClient tcpClient = tcpListener.AcceptTcpClient(); ClientWithTcp client = new ClientWithTcp(); string tcpId = ToolsMgr.GetUUID(); client.tcpId = tcpId; if (client.Init(tcpClient)) { client.StartClient(); client.timeTick = System.DateTime.Now.Ticks; lock (tmpClientDic) { tmpClientDic.Add(tcpId, client); } TcpRequestGetClientId pack = ProtocolMgr.GetPackageRequest <TcpRequestGetClientId>(-2); client.AddReqPackage(pack); } else { LogAgent.LogError("tcpClient客户端初始化失败 + \t" + ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString() + "\r\n"); client.DestoryTcpServer(); tcpClient.Close(); } } } StopServer: Interlocked.Add(ref status, 1); LogAgent.Log("!关闭Udp循环线程"); }
private void SendMessage() { List <UdpRequestBase> list = new List <UdpRequestBase>(); while (true) { Thread.Sleep(1); switch (status) { case 1: break; case 2: continue; default: goto StopServer; } List <UdpRequestBase> tmplist = GetSendData(); if (tmplist == null) { continue; } list.AddRange(tmplist); lock (mainLock) { switch (status) { case 1: break; case 2: DestorySendQueue(list); continue; default: DestorySendQueue(list); goto StopServer; } try { for (int i = 0; i < list.Count; i++) { UdpRequestBase req = list[i]; udp2AnyRequest.Send(req.getMemData(), (int)req.Count, boardIpEnd);//udpBug,后期修复 } } catch (Exception e) { LogAgent.LogError("Udp消息发送失败\t" + e.ToString()); } DestorySendQueue(list); } } StopServer: Interlocked.Add(ref status, 1); LogAgent.Log("!关闭Udp循环线程"); }
private void TcpMessage() { byte[] buffer = new byte[0x800]; List <TcpRequestBase> list = new List <TcpRequestBase>(); TcpResponseBase reponse = null; while (true) { Thread.Sleep(1); switch (status) { case 1: break; case 2: continue; default: goto StopServer; } List <TcpRequestBase> tmplist = GetSendData(); if (tmplist == null) { continue; } list.AddRange(tmplist); lock (mainLock) { switch (status) { case 1: break; case 2: DestorySendQueue(list); continue; default: DestorySendQueue(list); goto StopServer; } /*发送消息*/ for (int i = 0; i < list.Count; i++) { try { TcpRequestBase req = list[i]; if (this.client.Connected && this.stream.CanWrite) { stream.Write(req.getMemData(), 0, (int)req.Count);//udpBug,后期修复 } } catch (Exception e) { LogAgent.LogError("Tcp消息发送失败\t" + e.ToString()); } } DestorySendQueue(list); try { /*接收消息*/ if (this.client.Connected && this.client.Available > 0 && this.stream.CanRead) { int count = stream.Read(buffer, 0, buffer.Length);//默认每次只接收一个包 byte[] data = CheckPack(buffer, count); if (data == null) { continue; } reponse = GetReponsePackage(data); } } catch (Exception e) { LogAgent.LogError("Tcp消息接收失败\t" + e.ToString()); } } AddReqPackage(reponse); } StopServer: Interlocked.Add(ref status, 1); LogAgent.Log("!关闭Udp循环线程"); }
/// <summary> /// 查看公共视频是否完整 /// </summary> public void CheckPublicVedio() { string content = ""; TaskPoolHelper.Instance.StartNewTask(() => { Dictionary <string, string> tmpDic = new Dictionary <string, string>(); content = GetMsgFromHttpByPost(httpGetDefaultVedio, tmpDic); }); while (!publicVedioStatus) { Thread.Sleep(1); } if (string.IsNullOrEmpty(content)) { LogAgent.LogError("获取公共视频列表失败!"); return; } string listText =; string[] vedioId = listText.Split(...); addMainAction(() => { UICtrl.Instance.OpenDownloadPanel(); }); /*去检查公共视频是否下载完成*/ int count = 0; for (int i = 0; i < vedioId.Length; i++) { string path = CheckLocalFileExist(vedioId[i]); if (string.IsNullOrEmpty(path)) { TaskPoolHelper.Instance.StartNewTask(() => { string[] tmp = vedioId[i].Split(...); //0是url地址,1是id path = DownloadFile(tmp[0], tmp[1]); if (string.IsNullOrEmpty(path)) { if (File.Exists(path)) { File.Delete(path); } } else { lock (publicVedioList) publicVedioList.Add(path); } Interlocked.Add(ref count, 1); }); } else { Interlocked.Add(ref count, 1); publicVedioList.Add(path); } } /*等待公共视频全部下载完成*/ while (true) { if (count.Equals(vedioId.Length)) { break; } Thread.Sleep(1000); } UICtrl.Instance.mainPanel.AddList(publicVedioList); }