public void UpdateThreads() { for (int i = 0; i < runningThreads.Count; i++) { DataThread thread = runningThreads[i]; if (thread.jobHandle.IsCompleted) { thread.jobHandle.Complete(); callBack(thread.job); runningThreads.RemoveAt(i); if (toDO.Count > 0) { runThread(thread); } else { idleThreads.Enqueue(thread); } } } for (int i = 0; i < idleThreads.Count; i++) { if (toDO.Count > 0) { DataThread t = idleThreads.Dequeue(); runThread(t); } } //At the end of voxel managers update call JobHandle.ScheduleBatchedJobs(); to start execution on worker threads }
void DisposeAll(DataThread thread) { thread.job.data.Dispose(); thread.job.lengths.Dispose(); thread.job.vertices.Dispose(); thread.job.tris.Dispose(); thread.job.col.Dispose(); }
DataThread ScheduleThread(DataThread g, JobToDo p) { g.job.x = p.x; g.job.y = p.y; g.job.z = p.z; g.jobHandle = g.job.Schedule(); return(g); }
DataThread ScheduleThread(DataThread g, JobToDo c) { g.job.x = c.x; g.job.y = c.y; g.job.z = c.z; g.job.data.CopyFrom(c.data); g.jobHandle = g.job.Schedule(); return(g); }
public void stopDataThread() { if (_dataThread == null) { return; } _dataThread.Stop(); _dataThread = null; }
private void RunFinish(IAsyncResult aIAsyncResult) { try { DataThread aDataThread = (DataThread)aIAsyncResult.AsyncState; this.Dispatcher.BeginInvoke(new RunF(RunFinish), aDataThread.Run.EndInvoke(aIAsyncResult), aDataThread.ClusterAlgorithm); } catch (Exception _ex) { GeneralTools.Tools.WriteToLog(_ex); } }
private void startDataThread(IDataGetter dg) { if (_dataThread != null) { this.stopDataThread(); } _dataThread = new DataThread(); _dataThread.OnItems += new DTItemsHandler(_dataThread_onItems); _dataThread.OnFinish += new RSBEventHandler(_dataThread_OnFinish); _dataThread.InitMaxProgress += new DTProgressHandler(initProgress); //_dataThread.Progress +=new DTProgressHandler(progress); _dataThread.Run(dg); }
public bool Open(int port) { try { _client = new UdpClient(port); _port = (_client.Client.LocalEndPoint as IPEndPoint).Port; }catch (Exception e) { Debug.Log("RTPDataChannel Warning: " + e.Message); return(false); } _thread = new DataThread(this); _thread.Start(); return(true); }
public void InitThreads() { for (int i = 0; i < maxRunningThreads; i++) { DataThread thread = new DataThread { job = InitDataJob() }; if (toDO.Count > 0) { runThread(thread); } else { idleThreads.Enqueue(thread); } } }
//プロキシ処理 public override bool Pipe(ILife iLife) { DataThread dataThread = null; var paramStr = ""; //サーバ側との接続処理 if(!Proxy.Connect(iLife,_oneObj.Request.HostName,_oneObj.Request.Port,_oneObj.Request.RequestStr,_oneObj.Request.Protocol)) { Proxy.Logger.Set(LogKind.Debug,null,999,"□Break proxy.Connect()==false"); return false; } //wait 220 welcome if(!WaitLine("220",ref paramStr,iLife)) return false; Proxy.Sock(CS.Server).AsciiSend(string.Format("USER {0}",_user)); if(!WaitLine("331",ref paramStr,iLife)) return false; Proxy.Sock(CS.Server).AsciiSend(string.Format("PASS {0}",_pass)); if (!WaitLine("230", ref paramStr, iLife)) return false; //Ver5.6.6 if (_path == "/") { Proxy.Sock(CS.Server).AsciiSend("PWD"); if (!WaitLine("257", ref paramStr, iLife)) return false; var tmp = paramStr.Split(' '); if (tmp.Length >= 1) { _path = tmp[0].Trim(new[] { '"' }); if (_path[_path.Length - 1] != '/') { _path = _path + "/"; } } } //リクエスト if(_path != "") { Proxy.Sock(CS.Server).AsciiSend(string.Format("CWD {0}",_path)); if (!WaitLine("250", ref paramStr,iLife)) goto end; } Proxy.Sock(CS.Server).AsciiSend(_file == "" ? "TYPE A" : "TYPE I"); if (!WaitLine("200", ref paramStr,iLife)) goto end; //PORTコマンド送信 var bindAddr = Proxy.Sock(CS.Server).LocalIp; // 利用可能なデータポートの選定 while (iLife.IsLife()){ DataPort++; if (DataPort >= 9999) { DataPort = 2000; } if (SockServer.IsAvailable(_kernel,bindAddr, DataPort)){ break; } } int listenPort = DataPort; //データスレッドの生成 dataThread = new DataThread(_kernel,bindAddr,listenPort); // サーバ側に送るPORTコマンドを生成する string str = string.Format("PORT {0},{1},{2},{3},{4},{5}", bindAddr.IpV4[0], bindAddr.IpV4[1], bindAddr.IpV4[2], bindAddr.IpV4[3], listenPort / 256, listenPort % 256); Proxy.Sock(CS.Server).AsciiSend(str); if (!WaitLine("200", ref paramStr, iLife)) goto end; if(_file == "") { Proxy.Sock(CS.Server).AsciiSend("LIST"); if (!WaitLine("150", ref paramStr, iLife)) goto end; } else { Proxy.Sock(CS.Server).AsciiSend("RETR " + _file); if (!WaitLine("150", ref paramStr, iLife)) goto end; } //Ver5.0.2 while(iLife.IsLife()) { if(!dataThread.IsRecv) break; } if (!WaitLine("226", ref paramStr,iLife)) goto end; byte[] doc; if(_file == "") { //受信結果をデータスレッドから取得する List<string> lines = Inet.GetLines(dataThread.ToString()); //FTPサーバから取得したLISTの情報をHTMLにコンバートする doc = ConvFtpList(lines,_path); } else { doc = dataThread.ToBytes(); } //クライアントへリプライ及びヘッダを送信する var header = new Header(); header.Replace("Server", Util.SwapStr("$v", _kernel.Ver.Version(),(string)_conf.Get("serverHeader"))); header.Replace("MIME-Version","1.0"); if(_file == "") { header.Replace("Date",Util.UtcTime2Str(DateTime.UtcNow)); header.Replace("Content-Type","text/html"); } else { header.Replace("Content-Type","none/none"); } header.Replace("Content-Length",doc.Length.ToString()); Proxy.Sock(CS.Client).AsciiSend("HTTP/1.0 200 OK");//リプライ送信 Proxy.Sock(CS.Client).SendUseEncode(header.GetBytes());//ヘッダ送信 Proxy.Sock(CS.Client).SendNoEncode(doc);//ボディ送信 end: if(dataThread != null) dataThread.Dispose(); return false; }
public void Stop() { if(_client!=null) _client.Close (); if(_thread!=null) _thread.Abort (); _thread = null; _client = null; }
public void Start(int port=0) { try { _client = new UdpClient (port); _port=(_client.Client.LocalEndPoint as IPEndPoint).Port; }catch(Exception e) { } _thread = new DataThread (this); _thread.Start (); }
//*************************************************************** // パイプ(FTP) //*************************************************************** int PipeFtp(Dictionary <CS, TcpObj> sock, Request request, int dataPort) { DataThread dataThread = null; //ユーザ名及びパスワード string user = "******"; string pass = this.OpBase.ValString("anonymousAddress"); //Ver5.0.0-a23 URLで指定されたユーザ名およびパスワードを使用する if (request.User != null) { user = request.User; } if (request.Pass != null) { pass = request.Pass; } //wait 220 welcome if (!WaitLine(sock, "220")) { goto end; } sock[CS.SERVER].AsciiSend(string.Format("USER {0}", user), OPERATE_CRLF.YES); if (!WaitLine(sock, "331")) { goto end; } sock[CS.SERVER].AsciiSend(string.Format("PASS {0}", pass), OPERATE_CRLF.YES); if (!WaitLine(sock, "230")) { goto end; } //URIをパスとファイル名に分解する string path = request.Uri; string file = ""; int index = request.Uri.LastIndexOf('/'); if (index < request.Uri.Length - 1) { path = request.Uri.Substring(0, index); file = request.Uri.Substring(index + 1); } //リクエスト if (path != "") { sock[CS.SERVER].AsciiSend(string.Format("CWD {0}", path), OPERATE_CRLF.YES); if (!WaitLine(sock, "250")) { goto end; } } if (file == "") { sock[CS.SERVER].AsciiSend("TYPE A", OPERATE_CRLF.YES); } else { sock[CS.SERVER].AsciiSend("TYPE I", OPERATE_CRLF.YES); } if (!WaitLine(sock, "200")) { goto end; } //PORTコマンド送信 Ip bindAddr = new Ip(sock[CS.SERVER].LocalEndPoint.Address.ToString()); // 利用可能なデータポートの選定 int listenMax = 1; SSL ssl = null; TcpObj listenObj = null; while (life) { listenObj = new TcpObj(kanel, this.Logger, bindAddr, dataPort++, listenMax, ssl); if (listenObj.State != SOCKET_OBJ_STATE.ERROR) { break; } } if (listenObj == null) { goto end; } //データスレッドの生成 dataThread = new DataThread(this.Logger, listenObj); // サーバ側に送るPORTコマンドを生成する string str = string.Format("PORT {0},{1},{2},{3},{4},{5}", bindAddr.IpV4[0], bindAddr.IpV4[1], bindAddr.IpV4[2], bindAddr.IpV4[3], (listenObj.LocalEndPoint.Port) / 256, (listenObj.LocalEndPoint.Port) % 256); sock[CS.SERVER].AsciiSend(str, OPERATE_CRLF.YES); if (!WaitLine(sock, "200")) { goto end; } if (file == "") { sock[CS.SERVER].AsciiSend("LIST", OPERATE_CRLF.YES); if (!WaitLine(sock, "150")) { goto end; } } else { sock[CS.SERVER].AsciiSend("RETR " + file, OPERATE_CRLF.YES); if (!WaitLine(sock, "150")) { goto end; } } if (!WaitLine(sock, "226")) { goto end; } byte[] doc = new byte[0]; if (file == "") { //受信結果をデータスレッドから取得する List <string> lines = Inet.GetLines(dataThread.ToString()); //FTPサーバから取得したLISTの情報をHTMLにコンバートする doc = ConvFtpList(lines, path); } else { doc = dataThread.ToBytes(); } //クライアントへリプライ及びヘッダを送信する Header header = new Header(); header.Replace("Server", Util.SwapStr("$v", kanel.Ver.Version(), this.OpBase.ValString("serverHeader"))); header.Replace("MIME-Version", "1.0"); if (file == "") { header.Replace("Date", Util.UtcTime2Str(DateTime.UtcNow)); header.Replace("Content-Type", "text/html"); } else { header.Replace("Content-Type", "none/none"); } header.Replace("Content-Length", doc.Length.ToString()); sock[CS.CLIENT].AsciiSend("HTTP/1.0 200 OK", OPERATE_CRLF.YES); //リプライ送信 sock[CS.CLIENT].Send(header.GetBytes()); //ヘッダ送信 sock[CS.CLIENT].Send(doc); //ボディ送信 end: if (dataThread != null) { dataThread.Dispose(); } return(dataPort); }
void DisposeAll(DataThread item) { item.job.data.Dispose(); }
//*************************************************************** // パイプ(FTP) //*************************************************************** int PipeFtp(Dictionary<CS, TcpObj> sock, Request request,int dataPort) { DataThread dataThread = null; //ユーザ名及びパスワード string user = "******"; string pass = this.OpBase.ValString("anonymousAddress"); //Ver5.0.0-a23 URLで指定されたユーザ名およびパスワードを使用する if(request.User != null) user = request.User; if(request.Pass != null) pass = request.Pass; //wait 220 welcome if(!WaitLine(sock,"220")) goto end; sock[CS.SERVER].AsciiSend(string.Format("USER {0}",user),OPERATE_CRLF.YES); if(!WaitLine(sock,"331")) goto end; sock[CS.SERVER].AsciiSend(string.Format("PASS {0}",pass),OPERATE_CRLF.YES); if(!WaitLine(sock,"230")) goto end; //URIをパスとファイル名に分解する string path = request.Uri; string file = ""; int index = request.Uri.LastIndexOf('/'); if (index < request.Uri.Length - 1) { path = request.Uri.Substring(0, index); file = request.Uri.Substring(index + 1); } //リクエスト if (path != "") { sock[CS.SERVER].AsciiSend(string.Format("CWD {0}",path),OPERATE_CRLF.YES); if (!WaitLine(sock, "250")) goto end; } if (file == "") { sock[CS.SERVER].AsciiSend("TYPE A",OPERATE_CRLF.YES); } else { sock[CS.SERVER].AsciiSend("TYPE I",OPERATE_CRLF.YES); } if(!WaitLine(sock,"200")) goto end; //PORTコマンド送信 Ip bindAddr = new Ip(sock[CS.SERVER].LocalEndPoint.Address.ToString()); // 利用可能なデータポートの選定 int listenMax = 1; SSL ssl = null; TcpObj listenObj=null; while(life){ listenObj = new TcpObj(kanel, this.Logger,bindAddr, dataPort++, listenMax, ssl); if (listenObj.State != SOCKET_OBJ_STATE.ERROR) break; } if(listenObj==null) goto end; //データスレッドの生成 dataThread = new DataThread(this.Logger, listenObj); // サーバ側に送るPORTコマンドを生成する string str = string.Format("PORT {0},{1},{2},{3},{4},{5}",bindAddr.IpV4[0],bindAddr.IpV4[1],bindAddr.IpV4[2],bindAddr.IpV4[3],(listenObj.LocalEndPoint.Port) / 256,(listenObj.LocalEndPoint.Port) % 256); sock[CS.SERVER].AsciiSend(str,OPERATE_CRLF.YES); if(!WaitLine(sock,"200")) goto end; if(file==""){ sock[CS.SERVER].AsciiSend("LIST",OPERATE_CRLF.YES); if(!WaitLine(sock,"150")) goto end; }else{ sock[CS.SERVER].AsciiSend("RETR " + file,OPERATE_CRLF.YES); if(!WaitLine(sock,"150")) goto end; } if(!WaitLine(sock,"226")) goto end; byte[] doc = new byte[0]; if (file == "") { //受信結果をデータスレッドから取得する List<string> lines = Inet.GetLines(dataThread.ToString()); //FTPサーバから取得したLISTの情報をHTMLにコンバートする doc = ConvFtpList(lines, path); } else { doc = dataThread.ToBytes(); } //クライアントへリプライ及びヘッダを送信する Header header = new Header(); header.Replace("Server",Util.SwapStr("$v",kanel.Ver.Version(),this.OpBase.ValString("serverHeader"))); header.Replace("MIME-Version","1.0"); if (file == "") { header.Replace("Date",Util.UtcTime2Str(DateTime.UtcNow)); header.Replace("Content-Type","text/html"); } else { header.Replace("Content-Type","none/none"); } header.Replace("Content-Length",doc.Length.ToString()); sock[CS.CLIENT].AsciiSend("HTTP/1.0 200 OK",OPERATE_CRLF.YES);//リプライ送信 sock[CS.CLIENT].Send(header.GetBytes());//ヘッダ送信 sock[CS.CLIENT].Send(doc);//ボディ送信 end: if (dataThread != null) dataThread.Dispose(); return dataPort; }
// 用于测试线程传递数据 public static void ThreadDataSend(object parameter) { // 类型转换 DataThread obj = parameter as DataThread; Console.WriteLine("Running in a thread,receiving a parameter {0}", obj.message); }
static void Main(string[] args) { /* TemplateDe.TNullableDe.Nullable<int> x; x = 4; // x += 3; if (x.HasValue) { int y = x.Value; Console.WriteLine("if value of x is true; y = " + y.ToString()); } // 测试简单工厂模式 Magnate magnate = new Magnate(); */ char[][] grid = { {'1' },{'1' } }; var threadOne = new Thread(ThreadMain); threadOne.Start(); Console.WriteLine("ThreadMain is completed!"); // 线程传递数据 DataThread oData = new DataThread { message = "Hello", count = 1 }; var threadReceiveData = new Thread(ThreadDataSend); threadReceiveData.Start(oData); Console.WriteLine("data received!"); MyThread mythread = new MyThread("newthread", 12.2); var threadClass = new Thread(mythread.ThreadMain); threadClass.Start(); // 判断是否是前台线程还是后台线程 var bgThread = new Thread(ThreadMain) { Name = "My New Thread", IsBackground = true }; bgThread.Start(); Console.WriteLine("Hello, Main Thread completed!"); int nWorkThreadMax = 0; int nCompletePortThreads = 0; // 获取最大线程数 ThreadPool.GetMaxThreads(out nWorkThreadMax, out nCompletePortThreads); Console.WriteLine("Max work Thread sum = {0}" + "IO complete sum = {1}", nWorkThreadMax,nCompletePortThreads); for (int i = 0; i <5;i++) { ThreadPool.QueueUserWorkItem(JobForAThread); } Thread.Sleep(2000); // 判断线程中的竞争条件 var shareStateObj = new StateObject(); // 10个线程共享一个数据变量 for (int i = 0; i < 10; i++) { new Task(new SampleTask().RaceCondition, shareStateObj).Start(); } Thread.Sleep(1000); Console.ReadKey(true); }
void runThread(DataThread thread) { runningThreads.Add(ScheduleThread(thread, toDO.Dequeue())); }
void DisposeAll(DataThread item) { item.job.array.Dispose(); }
//プロキシ処理 override public bool Pipe(ILife iLife) { DataThread dataThread = null; var paramStr = ""; //サーバ側との接続処理 if (!Proxy.Connect(iLife, _oneObj.Request.HostName, _oneObj.Request.Port, _oneObj.Request.RequestStr, _oneObj.Request.Protocol)) { Proxy.Logger.Set(LogKind.Debug, null, 999, "□Break proxy.Connect()==false"); return(false); } //wait 220 welcome if (!WaitLine("220", ref paramStr, iLife)) { return(false); } Proxy.Sock(CS.Server).AsciiSend(string.Format("USER {0}", _user)); if (!WaitLine("331", ref paramStr, iLife)) { return(false); } Proxy.Sock(CS.Server).AsciiSend(string.Format("PASS {0}", _pass)); if (!WaitLine("230", ref paramStr, iLife)) { return(false); } //Ver5.6.6 if (_path == "/") { Proxy.Sock(CS.Server).AsciiSend("PWD"); if (!WaitLine("257", ref paramStr, iLife)) { return(false); } var tmp = paramStr.Split(' '); if (tmp.Length >= 1) { _path = tmp[0].Trim(new[] { '"' }); if (_path[_path.Length - 1] != '/') { _path = _path + "/"; } } } //リクエスト if (_path != "") { Proxy.Sock(CS.Server).AsciiSend(string.Format("CWD {0}", _path)); if (!WaitLine("250", ref paramStr, iLife)) { goto end; } } Proxy.Sock(CS.Server).AsciiSend(_file == "" ? "TYPE A" : "TYPE I"); if (!WaitLine("200", ref paramStr, iLife)) { goto end; } //PORTコマンド送信 var bindAddr = Proxy.Sock(CS.Server).LocalIp; // 利用可能なデータポートの選定 while (iLife.IsLife()) { DataPort++; if (DataPort >= 9999) { DataPort = 2000; } if (SockServer.IsAvailable(_kernel, bindAddr, DataPort)) { break; } } int listenPort = DataPort; //データスレッドの生成 dataThread = new DataThread(_kernel, bindAddr, listenPort); // サーバ側に送るPORTコマンドを生成する string str = string.Format("PORT {0},{1},{2},{3},{4},{5}", bindAddr.IpV4[0], bindAddr.IpV4[1], bindAddr.IpV4[2], bindAddr.IpV4[3], listenPort / 256, listenPort % 256); Proxy.Sock(CS.Server).AsciiSend(str); if (!WaitLine("200", ref paramStr, iLife)) { goto end; } if (_file == "") { Proxy.Sock(CS.Server).AsciiSend("LIST"); if (!WaitLine("150", ref paramStr, iLife)) { goto end; } } else { Proxy.Sock(CS.Server).AsciiSend("RETR " + _file); if (!WaitLine("150", ref paramStr, iLife)) { goto end; } } //Ver5.0.2 while (iLife.IsLife()) { if (!dataThread.IsRecv) { break; } } if (!WaitLine("226", ref paramStr, iLife)) { goto end; } byte[] doc; if (_file == "") { //受信結果をデータスレッドから取得する List <string> lines = Inet.GetLines(dataThread.ToString()); //FTPサーバから取得したLISTの情報をHTMLにコンバートする doc = ConvFtpList(lines, _path); } else { doc = dataThread.ToBytes(); } //クライアントへリプライ及びヘッダを送信する var header = new Header(); header.Replace("Server", Util.SwapStr("$v", _kernel.Ver.Version(), (string)_conf.Get("serverHeader"))); header.Replace("MIME-Version", "1.0"); if (_file == "") { header.Replace("Date", Util.UtcTime2Str(DateTime.UtcNow)); header.Replace("Content-Type", "text/html"); } else { header.Replace("Content-Type", "none/none"); } header.Replace("Content-Length", doc.Length.ToString()); Proxy.Sock(CS.Client).AsciiSend("HTTP/1.0 200 OK"); //リプライ送信 Proxy.Sock(CS.Client).SendUseEncode(header.GetBytes()); //ヘッダ送信 Proxy.Sock(CS.Client).SendNoEncode(doc); //ボディ送信 end: if (dataThread != null) { dataThread.Dispose(); } return(false); }