1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace testform 8 { 9 using System; 10 using System.IO; 11 using System.Security.Cryptography.X509Certificates; 12 using System.Threading.Tasks; 13 using DotNetty.Codecs; 14 using DotNetty.Handlers.Logging; 15 using DotNetty.Handlers.Tls; 16 using DotNetty.Transport.Bootstrapping; 17 using DotNetty.Transport.Channels; 18 using DotNetty.Transport.Channels.Sockets; 19 using Examples.Common; 20 using System.Collections.Concurrent; 21 using System.Threading; 22 23 class RunServerto 24 { 25 //private static BlockingCollection<MesServer> bcms = new BlockingCollection<MesServer>(); 26 27 //public static string userId { get; internal set; } 28 //public static string SAY_WELCOME { get; internal set; } 29 //public static string BYE { get; internal set; } 30 31 //public static readonly string Content = "消息内容"; 32 //public static readonly string serverId = "Endv Cn Server";//当前托管线程的唯一标识符 33 //public static readonly int WAIT = 20000; 34 //internal static readonly string SAY_THANKS; 35 //internal static readonly string WHAT; 36 //internal static string TASK; 37 38 public static void Main(string [] args) 39 { 40 Parallel.Invoke( 41 () => 42 { 43 Form3 form3 = new Form3(); 44 form3.Show(); 45 //Console.WriteLine("任务1……"); 46 }, 47 () => 48 { 49 //RunServerAsync().Wait(); 50 Task thread2 = new Task(() => { RunServerAsync().Wait(); });//创建新线程 51 thread2.Start(); 52 //RunServerAsync(); 53 }); 54 55 56 //Console.ReadKey(); 57 //Form1 form1 = new Form1(); 58 ////消费者线程 59 //ParallelOptions po = new ParallelOptions(); 60 //po.MaxDegreeOfParallelism = -1; 61 //Parallel.Invoke(po, () => { 62 // //////////////////////////////////////// 63 // //int threadid = Environment.CurrentManagedThreadId; 64 // //MesServer server = new MesServer(); 65 // //while(true) 66 // //{ 67 // form1.textBox2.Text = "1234567form1"; 68 // form1.Show(); 69 70 // 尝试在指定的时间段内从 server 中移除一个项 71 //if(bcms.TryTake(out server, RunServerto.WAIT)) 72 //{ 73 // server.ThreadID = threadid; 74 // server.doAction(); 75 // //Console.WriteLine(" "); 76 // //Console.WriteLine(server.ToString(false)); 77 78 //} 79 //else 80 //{ 81 // if(server == null) 82 // { 83 // server = new MesServer(); 84 // } 85 // server.ThreadID = threadid; 86 // server.content = Content; 87 // //Console.WriteLine(server.ToString(false)); 88 //} 89 //} 90 //}, 91 ////////////////////////////////////////// 92 //() => { 93 94 //int prdID = Environment.CurrentManagedThreadId; 95 //MesServer server = new MesServer(); 96 //server.ProductorID = prdID; 97 //server.content = Content; 98 ////Console.WriteLine(productor.ToString(true)); 99 //while(true) 100 //{ 101 // Form3 form3 = new Form3(); 102 // form3.Show(); 103 // form3.label1.Text = ""; 104 // //Console.Write("Productor Behavior (i.e. say,hello) : "); 105 // //string msgContent = Console.ReadLine(); 106 107 // form3.textBox2.Text="打开窗体3"; 108 // string msgContent = form3.textBox2.Text ; 109 // server = new MesServer(); 110 // server.ProductorID = prdID; 111 // //:“索引超出了数组界限。” 112 113 // //server.key = msgContent.Split(',') [0]; 114 // //server.content = msgContent.Split(',') [1]; 115 // bcms.Add(server); 116 117 118 //Thread thread1 = new Thread(() => { threadPro(); });//创建新线程 119 //thread1.Start(); 120 //Thread thread2= new Thread(() => { MesServer.RunServerAsync().Wait(); });//创建新线程 121 //thread2.Start(); 122 //MesServer.RunServerAsync().Wait(); 123 //} 124 125 //}); 126 127 } 128 public static async Task RunServerAsync() 129 { 130 //在控制台命令中启用日志 131 //ExampleHelper.SetConsoleLogger(); 132 Form1 form1 = new Form1(); 133 form1.Show(); 134 //多线程事件循环组,创建一个新实例,老板组 135 var bossGroup = new MultithreadEventLoopGroup(1); 136 //多线程事件循环组,创建一个新实例,工作组 137 var workerGroup = new MultithreadEventLoopGroup(); 138 139 //字符串编码 140 var STRING_ENCODER = new StringEncoder(); 141 //字符串解码 142 var STRING_DECODER = new StringDecoder(); 143 //安全聊天服务器处理程序 144 var SERVER_HANDLER = new SecureChatServerHandler(); 145 146 X509Certificate2 tlsCertificate = null; 147 if(ServerSettings.IsSsl) 148 { 149 // 创建 X.509 证书 150 tlsCertificate = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password"); 151 } 152 try 153 { 154 //服务器启动 155 var bootstrap = new ServerBootstrap(); 156 bootstrap 157 .Group(bossGroup, workerGroup) 158 //服务器套接字通道 159 .Channel<TcpServerSocketChannel>() 160 //所以返回日志 161 .Option(ChannelOption.SoBacklog, 100) 162 //Handler用于服务请求 “信息”日志级别。 163 .Handler(new LoggingHandler(LogLevel.INFO)) 164 //设置{@链接channelhandler }这是用来服务请求{@链接通道}的。 165 .ChildHandler( 166 new ActionChannelInitializer<ISocketChannel>(channel => 167 { 168 IChannelPipeline pipeline = channel.Pipeline; 169 if(tlsCertificate != null) 170 { 171 //添加协议到最后 172 pipeline.AddLast(TlsHandler.Server(tlsCertificate)); 173 } 174 //添加基于行分隔符的帧解码器到最后 175 pipeline.AddLast(new DelimiterBasedFrameDecoder(8192, Delimiters.LineDelimiter())); 176 pipeline.AddLast(STRING_ENCODER, STRING_DECODER, SERVER_HANDLER); 177 })); 178 // 创建异步通道并绑定异步端口 179 IChannel bootstrapChannel = await bootstrap.BindAsync(ServerSettings.Port); 180 form1.textBox2.Text = "IChannel线程已启动"; 181 //Console.ReadLine(); 182 //关闭异步 183 await bootstrapChannel.CloseAsync(); 184 } 185 finally 186 { 187 // 等待提供的所有 System.Threading.Tasks.Task 对象完成执行过程 188 Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync()); 189 } 190 } 191 192 193 //static void threadPro() 194 //{ 195 // //this. form3.Show(); 196 //} 197 } 198 199 class MesServer 200 { 201 202 //public int ThreadID { get; internal set; }//当前托管线程的唯一标识符 203 ////public string userId { get; set; } 204 //public string key { get; set; } 205 //public string content { get; set; } 206 //public int ProductorID { get; internal set; } 207 208 //public void doAction() 209 //{ 210 // //比较 211 // if(string.Compare(key, RunServerto.userId) == 0) 212 // { Form1 form2 = new Form1(); 213 // content = string.Compare(content, RunServerto.SAY_THANKS) == 0 ? RunServerto.SAY_WELCOME : RunServerto.WHAT; 214 215 // form2.Show(); 216 // } 217 218 // if(string.Compare(key, RunServerto.TASK) == 0) 219 // { 220 // Task taskA = Task.Factory.StartNew(() => { 221 // //Main2().Show(); 222 // //Console.WriteLine("task A begin "); 223 // Task ChildOfFatehrA = Task.Factory.StartNew(() => { 224 // //Console.WriteLine("Sub task A begin "); 225 // //Thread.Sleep(1000); 226 // //Console.WriteLine("Sub task A end "); 227 // //Main2().Show(); 228 // Form4 form4 = new Form4(); 229 // form4.textBox2.Text = "打开窗体4"; 230 // //string msgContent = form3.textBox2.Text; 231 232 233 // }); 234 // ChildOfFatehrA.Wait(); 235 // Console.WriteLine("task A end "); 236 237 // }); 238 // taskA.ContinueWith(taskB => { 239 // //Console.WriteLine("task B begin "); 240 // //Thread.Sleep(5000); 241 // //Console.WriteLine("task B end "); 242 // }); 243 // taskA.Wait(); 244 // } 245 246 247 //} 248 249 250 251 } 252 }