static void Main(string[] args) { Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); TcpListener tcpListener = new TcpListener(localAddr, port); tcpListener.Start(); while (true) { Console.WriteLine("Waiting for a connection..."); TcpClient tcpClient = tcpListener.AcceptTcpClient(); Console.WriteLine("Connected!"); var controller = new RpcController(); var server = new RpcServer(controller); server.RegisterService<ISampleService>(new SampleService()); var channel = new NetworkStreamRpcChannel(controller, tcpClient.GetStream()); channel.Start(); while (tcpClient.Connected) System.Threading.Thread.Sleep(1000); channel.CloseAndJoin(); Console.WriteLine("Connection closed.\n"); } }
private static void StartServer(Type interfaceType, Type implType, string pipeName) { //create the server var controller = new RpcController(); var server = new RpcServer(controller); //register the service with the server. We must specify the interface explicitly since we did not use attributes server.GetType() .GetMethod("RegisterService") .MakeGenericMethod(interfaceType) .Invoke(server, new[] {Activator.CreateInstance(implType)}); //build the connection using named pipes try { pipeServerStreamIn = CreateNamedPipe(pipeName + "ctos", PipeDirection.In); pipeServerStreamOut = CreateNamedPipe(pipeName + "stoc", PipeDirection.Out); streamsCreated = true; pipeServerStreamIn.WaitForConnection(); pipeServerStreamOut.WaitForConnection(); //create and start the channel which will receive requests var channel = new StreamRpcChannel(controller, pipeServerStreamIn, pipeServerStreamOut, useSharedMemory: true); channel.Start(); } catch (IOException e) { //swallow and exit Console.WriteLine("Something went wrong (pipes busy?), quitting: " + e); throw; } }
public StreamRpcChannel(RpcController controller, Stream readStream, Stream writeStream) : base(controller) { this.readStream = readStream; this.writeStream = writeStream; readThread = new Thread(ReadRun); writeThread = new Thread(WriteRun); }
static void Main(string[] args) { Int32 port = 13000; TcpClient tcpClient = new TcpClient("127.0.0.1", port); var controller = new RpcController(); var client = new RpcClient(controller); var channel = new NetworkStreamRpcChannel(controller, tcpClient.GetStream()); channel.Start(); ISampleService service = client.GetProxy<ISampleService>(); int counter = 0; while (true) { Console.WriteLine("Enter number to test:"); int x = Int32.Parse(Console.ReadLine()); bool isPrime; if (counter++ % 2 == 0) { Console.WriteLine(" Asking server if " + x + " is prime..."); isPrime = service.TestPrime(x); } else { Console.WriteLine(" Asking server if " + x + " is prime, using an async query..."); IAsyncResult asyncResult = service.BeginTestPrime(x, null, null); Console.WriteLine(" Doing some other stuff while the server is calculating..."); isPrime = service.EndTestPrime(asyncResult); } if (isPrime) Console.WriteLine(" Server says: Prime!"); else Console.WriteLine(" Server says: Not prime!"); } }
public RpcClient(RpcController controller) { this.controller = controller; controller.Client = this; }
public NetworkStreamRpcChannel(RpcController controller, NetworkStream stream) : base(controller, stream, stream) { }
public RpcServer(RpcController controller) { this.controller = controller; controller.Server = this; }