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() {
|
56
|
20 try {
|
|
21 TcpClient client = null;
|
|
22 client = listener.AcceptTcpClient();
|
|
23 client.NoDelay = true;
|
|
24
|
|
25 IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint;
|
|
26 IPAddress ipAddress = endPoint.Address;
|
|
27 IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress);
|
|
28 Console.WriteLine("Accept " + hostEntry.HostName + ":" + endPoint.Port);
|
|
29
|
|
30 Connection connection = new Connection(client.Client, cgm);
|
|
31 Console.WriteLine("connection:" + connection.GetInfoString());
|
|
32 string key = "accept" + counter;
|
33
|
33
|
56
|
34 IncomingTcpConnection incoming = new IncomingTcpConnection(connection);
|
62
|
35 Task.Factory.StartNew(() => incoming.Run());
|
56
|
36
|
|
37 cgm.SetAccept(key, incoming);
|
|
38
|
|
39 OutboundTcpConnection outbound = new OutboundTcpConnection(connection);
|
62
|
40 Task.Factory.StartNew(() => outbound.Run());
|
56
|
41 counter++;
|
|
42 } catch (Exception e) {
|
|
43 Console.WriteLine(e.StackTrace);
|
34
|
44 }
|
22
|
45 }
|
21
|
46 }
|
|
47 } |