25
|
1 using System;
|
|
2 using System.IO;
|
|
3 using System.Net;
|
|
4 using System.Net.Sockets;
|
|
5 using System.Threading.Tasks;
|
|
6 using Christie_net.codegear;
|
|
7
|
20
|
8 namespace Christie_net.daemon {
|
|
9 public class ChristieDaemon {
|
|
10 private int localPort;
|
25
|
11 private AcceptThread acceptThread;
|
|
12 public CodeGearManager cgm;
|
|
13
|
|
14 public ChristieDaemon(int localPort, CodeGearManager cgm) {
|
|
15 this.localPort = localPort;
|
|
16 this.cgm = cgm;
|
|
17 }
|
|
18
|
|
19 public void Listen() {
|
|
20 try {
|
|
21 // listen on any address ipv4/ipv6
|
|
22 IPHostEntry host = Dns.GetHostEntry("::");
|
|
23 IPAddress ipAddress = host.AddressList[0];
|
|
24 IPEndPoint localEndPoint = new IPEndPoint(ipAddress, localPort);
|
|
25
|
|
26 Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
|
27 socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
28 Console.WriteLine("ChristieDaemon, listen: bind to " + localEndPoint);
|
|
29 socket.Bind(localEndPoint);
|
26
|
30 socket.Listen((int)SocketOptionName.MaxConnections);
|
25
|
31
|
|
32 acceptThread = new AcceptThread(socket, cgm);
|
26
|
33 Task.Factory.StartNew(
|
|
34 () => acceptThread.Run(),
|
|
35 TaskCreationOptions.LongRunning);
|
25
|
36 } catch (IOException e) {
|
|
37 Console.WriteLine(e.StackTrace);
|
|
38 }
|
|
39
|
|
40 }
|
|
41
|
20
|
42 }
|
|
43 } |