22
|
1 using System;
|
|
2 using System.Net;
|
|
3 using System.Net.Sockets;
|
33
|
4 using System.Threading;
|
62
|
5 using System.Threading.Tasks;
|
22
|
6 using Christie_net.codegear;
|
|
7
|
21
|
8 namespace Christie_net.daemon {
|
|
9 public class AcceptThread {
|
33
|
10 private TcpListener listener;
|
22
|
11 public int counter = 0;
|
|
12 public CodeGearManager cgm;
|
|
13
|
33
|
14 public AcceptThread(TcpListener listener, CodeGearManager cgm) {
|
|
15 this.listener = listener;
|
22
|
16 this.cgm = cgm;
|
|
17 }
|
|
18
|
|
19 public void Run() {
|
65
|
20 while (true) {
|
|
21 try {
|
|
22 TcpClient client = null;
|
|
23 client = listener.AcceptTcpClient();
|
|
24 client.NoDelay = true;
|
56
|
25
|
65
|
26 IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint;
|
|
27 IPAddress ipAddress = endPoint.Address;
|
|
28 IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress);
|
|
29 Console.WriteLine("Accept " + hostEntry.HostName + ":" + endPoint.Port);
|
56
|
30
|
65
|
31 Connection connection = new Connection(client.Client, cgm);
|
|
32 Console.WriteLine("connection:" + connection.GetInfoString());
|
|
33 string key = "accept" + counter;
|
33
|
34
|
65
|
35 IncomingTcpConnection incoming = new IncomingTcpConnection(connection);
|
|
36 Task.Factory.StartNew(() => incoming.Run());
|
56
|
37
|
65
|
38 cgm.SetAccept(key, incoming);
|
56
|
39
|
65
|
40 OutboundTcpConnection outbound = new OutboundTcpConnection(connection);
|
|
41 Task.Factory.StartNew(() => outbound.Run());
|
|
42 counter++;
|
|
43 } catch (Exception e) {
|
|
44 Console.WriteLine(e.StackTrace);
|
|
45 }
|
34
|
46 }
|
22
|
47 }
|
21
|
48 }
|
|
49 } |