Example #1
0
        public RpcUdpClient(
            IPAddress ipAddress,
            int program,
            int version,
            ClientSettings clientSettings = default)
        {
            int port = clientSettings?.Port ?? 0;

            if (port == 0)
            {
                var portMapperClientSettings = new PortMapperClientSettings
                {
                    Port = clientSettings?.PortMapperPort ?? PortMapperConstants.PortMapperPort
                };
                port = PortMapperUtilities.GetPort(Protocol.Udp, ipAddress, program, version, portMapperClientSettings);
            }

            var remoteIpEndPoint = new IPEndPoint(ipAddress, port);

            this.client         = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            this.ReceiveTimeout = clientSettings?.ReceiveTimeout ?? Utilities.DefaultClientReceiveTimeout;
            this.SendTimeout    = clientSettings?.SendTimeout ?? Utilities.DefaultClientSendTimeout;
            var reader = new UdpReader(this.client);
            var writer = new UdpWriter(this.client);

            this.call = new RpcCall(program, remoteIpEndPoint, reader, writer, null, clientSettings?.Logger);
        }
Example #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}...");
        }