Esempio n. 1
0
        public void Start()
        {
            if (this.isDisposed)
            {
                throw new ObjectDisposedException(nameof(RpcUdpServer));
            }

            if (this.acceptingThread != null)
            {
                return;
            }

            try
            {
                this.server.Bind(new IPEndPoint(this.ipAddress, this.port));
                this.server.Listen(int.MaxValue);
            }
            catch (SocketException e)
            {
                throw new RpcException($"Could not start TCP listener. Socket error: {e.SocketErrorCode}.");
            }

            if (this.port == 0)
            {
                this.port = ((IPEndPoint)this.server.LocalEndPoint).Port;
                lock (this.connections)
                {
                    var clientSettings = new PortMapperClientSettings {
                        Port = this.portMapperPort
                    };
                    foreach (int version in this.versions)
                    {
                        PortMapperUtilities.UnsetAndSetPort(
                            Protocol.Tcp,
                            this.port,
                            this.program,
                            version,
                            clientSettings);
                    }
                }
            }

            this.logger?.Info(
                $"{Utilities.ConvertToString(Protocol.Tcp)} Server listening on {this.server.LocalEndPoint}...");

            this.acceptingThread = new Thread(this.Accepting)
            {
                Name = $"RpcNet TCP Accept {this.port}"
            };
            this.acceptingThread.Start();
        }
Esempio n. 2
0
        public RpcUdpServer(
            IPAddress ipAddress,
            int program,
            int[] versions,
            Action <ReceivedRpcCall> receivedCallDispatcher,
            ServerSettings serverSettings = default)
        {
            this.port   = serverSettings?.Port ?? 0;
            this.server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            this.server.Bind(new IPEndPoint(ipAddress, this.port));

            this.reader = new UdpReader(this.server);
            this.writer = new UdpWriter(this.server);

            this.receivedCall = new ReceivedRpcCall(
                program,
                versions,
                this.reader,
                this.writer,
                receivedCallDispatcher);

            this.logger = serverSettings?.Logger;

            if (this.port == 0)
            {
                this.port = ((IPEndPoint)this.server.LocalEndPoint).Port;
                var clientSettings = new PortMapperClientSettings
                {
                    Port = serverSettings?.PortMapperPort ?? PortMapperConstants.PortMapperPort
                };
                foreach (int version in versions)
                {
                    PortMapperUtilities.UnsetAndSetPort(Protocol.Udp, this.port, program, version, clientSettings);
                }
            }

            this.logger?.Info(
                $"{Utilities.ConvertToString(Protocol.Udp)} Server listening on {this.server.LocalEndPoint}...");
        }