예제 #1
0
        public bool Start(IPAddress bindAddress, IPAddress multicastAddress4, IPAddress multicastAddress6,
                          uint protocolVersion, uint requiredVersion)
        {
            if (Running)
            {
                return(false);
            }
            BindAddress       = bindAddress;
            MulticastAddress4 = multicastAddress4;
            MulticastAddress6 = multicastAddress6;
            ProtocolVersion   = protocolVersion;
            RequiredVersion   = requiredVersion;

            /// ToDo: Actually filter unspecified addresses like 0.0.0.0 or ::.
            if (bindAddress.AddressFamily == AddressFamily.InterNetwork ||
                bindAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                var serviceSocket = new TcpListenerSocket();
                serviceSocket.Connected    += OnClientConnected;
                serviceSocket.Disconnected += OnClientDisconnected;
                serviceSocket.Listen(new IPEndPoint(bindAddress, ServicePort));
                _serviceSockets.Add(serviceSocket);

                var multicastSocket = new UdpSocket();
                multicastSocket.MessageReceived += OnMulticastReceive;
                multicastSocket.Open(new IPEndPoint(bindAddress, MulticastPort),
                                     (bindAddress.AddressFamily == AddressFamily.InterNetworkV6)
                    ? multicastAddress6
                    : multicastAddress4);
                _multicastSockets.Add(multicastSocket);
            }
            else
            {
                foreach (var address in NetworkHost.Instance.LocalAddresses())
                {
                    var serviceSocket = new TcpListenerSocket();
                    serviceSocket.Connected    += OnClientConnected;
                    serviceSocket.Disconnected += OnClientDisconnected;
                    serviceSocket.Listen(new IPEndPoint(address, ServicePort));
                    _serviceSockets.Add(serviceSocket);

                    var multicastSocket = new UdpSocket();
                    multicastSocket.MessageReceived += OnMulticastReceive;
                    multicastSocket.Open(new IPEndPoint(address, MulticastPort),
                                         (bindAddress.AddressFamily == AddressFamily.InterNetworkV6)
                      ? multicastAddress6
                      : multicastAddress4);
                    _multicastSockets.Add(multicastSocket);
                }
            }
            Console.WriteLine(String.Format("Started ServiceHost {0:X}", HostGuid));
            Running = true;
            return(Running);
        }
예제 #2
0
 public ServiceBase(bool isProvider, uint serviceUid, ulong interfaceUids, ushort serviceTag)
 {
     IsProvider    = isProvider;
     ServiceGuid   = GuidGenerator.generate();
     ServiceUid    = serviceUid;
     InterfaceUids = interfaceUids;
     ServiceTag    = serviceTag;
     Sockets       = new HashSet <TcpClientSocket>();
     if (IsProvider)
     {
         ListenerSocket               = new TcpListenerSocket();
         ListenerSocket.Connected    += OnConnected;
         ListenerSocket.Disconnected += OnDisconnected;
         ListenerSocket.Listen(new IPEndPoint(
                                   ServiceHost.Instance.BindAddress, 0));
     }
     Caller = new ThreadLocal <Connection>(
         () =>
     {
         throw new NotImplementedException();
     }, false);
 }