static void Main(string[] args) { //使用HTTP通道得到远程对象 HttpChannel chan2 = new HttpChannel(); System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chan2, false); HelloServer obj1 = (HelloServer)Activator.GetObject( typeof(TestProject.HelloServer), "http://localhost:8086/SayHello");//创建类的实例 if (obj1 == null) { System.Console.WriteLine( "Could not locate HTTP server"); } Console.WriteLine( "Client HTTP HelloUserMethod {0}", obj1.HelloUserMethod(new User("张生", true))); //将类作为参数(将User作为参数必须是serializable) TcpChannel tcpChannel = new TcpChannel(); System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(tcpChannel, false); HelloServer obj2 = (HelloServer)Activator.GetObject( typeof(TestProject.HelloServer), "tcp://localhost:8086/SayHello");//创建类的实例 }
public static void Main() { // Create a 'HttpChannel' object and register with channel services. ChannelServices.RegisterChannel(new HttpChannel()); Console.WriteLine(" Start calling from Client One......."); WellKnownClientTypeEntry myWellKnownClientTypeEntry = new WellKnownClientTypeEntry(typeof(HelloServer), "http://localhost:8086/SayHello"); myWellKnownClientTypeEntry.ApplicationUrl = "http://localhost:8086/SayHello"; RemotingConfiguration.RegisterWellKnownClientType(myWellKnownClientTypeEntry); // Get the proxy object for the remote object. HelloServer myHelloServerObject = new HelloServer(); // Retrieve an array of object types registered on the // client end as well-known types. WellKnownClientTypeEntry [] myWellKnownClientTypeEntryCollection = RemotingConfiguration.GetRegisteredWellKnownClientTypes(); Console.WriteLine("The Application Url to activate the Remote Object :" + myWellKnownClientTypeEntryCollection[0].ApplicationUrl); Console.WriteLine("The 'WellKnownClientTypeEntry' object :" + myWellKnownClientTypeEntryCollection[0].ToString()); // Make remote method calls. for (int i = 0; i < 5; i++) { Console.WriteLine(myHelloServerObject.HelloMethod(" Client One")); } }
private void OnHelloServer(ushort id, HelloServer helloServer) { Logger.Get().Info(this, $"Received HelloServer data from ID {id}"); // Start by sending the new client the current Server Settings _netServer.GetUpdateManagerForClient(id).UpdateGameSettings(_gameSettings); // Create new player data object var playerData = new ServerPlayerData( helloServer.Username, helloServer.SceneName, helloServer.Position, helloServer.Scale, helloServer.AnimationClipId ); // Store data in mapping _playerData[id] = playerData; foreach (var idPlayerDataPair in _playerData.GetCopy()) { if (idPlayerDataPair.Key == id) { continue; } _netServer.GetUpdateManagerForClient(idPlayerDataPair.Key).AddPlayerConnectData( id, helloServer.Username ); } OnClientEnterScene(id, playerData); }
public void MarshalByRefObjectSampleTest() { TcpChannel chan1 = new TcpChannel(18085); HttpChannel chan2 = new HttpChannel(18086); ChannelServices.RegisterChannel(chan1, false); ChannelServices.RegisterChannel(chan2, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer), "SayHello", WellKnownObjectMode.Singleton); //创建类的实例 //使用HTTP通道得到远程对象 HttpChannel chan3 = new HttpChannel(); ChannelServices.RegisterChannel(chan3); HelloServer obj1 = (HelloServer)Activator.GetObject( typeof(HelloServer), "http://localhost:8086/SayHello");//创建类的实例 if (obj1 == null) { System.Console.WriteLine( "Could not locate HTTP server"); } Console.WriteLine( "Client1 TCP HelloUserMethod {0}", obj1.HelloUserMethod(new User("张生", true))); //将类作为参数 }
public void InvalidLogin(Connection conn, LoginInfo login) { if (HelloServer.Login(_entity, _privKey)) { HelloServer.Register(_ic, _properties); } }
public static void Main() { // Register TCP Channel. ChannelServices.RegisterChannel(new TcpChannel()); //<snippet2> // Create activated client type entry. ActivatedClientTypeEntry myActivatedClientTypeEntry = new ActivatedClientTypeEntry(typeof(HelloServer), "tcp://localhost:8082"); //</snippet2> // Register type on client to activate it on the server. RemotingConfiguration.RegisterActivatedClientType( myActivatedClientTypeEntry); // Activate a client activated object type. HelloServer myHelloServer = new HelloServer("ParameterString"); //<snippet3> // Print the object type. Console.WriteLine( "Object type of client activated object: " + myActivatedClientTypeEntry.ObjectType.ToString()); //</snippet3> //<snippet4> // Print the application URL. Console.WriteLine( "Application url where the type is activated: " + myActivatedClientTypeEntry.ApplicationUrl); //</snippet4> //<snippet5> // Print the string representation of the type entry. Console.WriteLine( "Type name, assembly name and application URL " + "of the remote object: " + myActivatedClientTypeEntry.ToString()); //</snippet5> // Print a blank line. Console.WriteLine(); // Check that server was located. if (myHelloServer == null) { Console.WriteLine("Could not locate server"); } else { Console.WriteLine("Calling remote object"); Console.WriteLine(myHelloServer.HelloMethod("Bill")); } }
public async Task SendReceive() { var cs = new CancellationTokenSource(TimeSpan.FromSeconds(30)); var tcp = new Tcp(); using (var server = new HelloServer()) using (var stream = await tcp.ConnectAsync(server.Address, cs.Token)) { var bytes = new byte[5]; await stream.ReadAsync(bytes, 0, bytes.Length); Assert.AreEqual("hello", Encoding.UTF8.GetString(bytes)); } }
public ServerUpdatePacket(Packet packet) : base(packet) { DataPacketIds = new HashSet <ServerPacketId>(); HelloServer = new HelloServer(); PlayerUpdate = new PlayerUpdate(); EntityUpdates = new PacketDataCollection <EntityUpdate>(); PlayerEnterScene = new ServerPlayerEnterScene(); PlayerTeamUpdate = new ServerPlayerTeamUpdate(); PlayerSkinUpdate = new ServerPlayerSkinUpdate(); PlayerEmoteUpdate = new ServerPlayerEmoteUpdate(); }
static void Main(string[] args) { const int Port = 50051; Server server = new Server { Services = { HelloServer.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Console.WriteLine("Greeter server listening on port " + Port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); server.ShutdownAsync().Wait(); }
public static void Main() { ChannelServices.RegisterChannel(new HttpChannel()); Console.WriteLine(" Start calling from Client One......."); WellKnownClientTypeEntry myWellKnownClientTypeEntry = new WellKnownClientTypeEntry(typeof(HelloServer), "http://localhost:8086/SayHello"); myWellKnownClientTypeEntry.ApplicationUrl = "http://localhost:8086/SayHello"; RemotingConfiguration.RegisterWellKnownClientType(myWellKnownClientTypeEntry); HelloServer myHelloServerObject = new HelloServer(); for (int i = 0; i < 5; i++) { Console.WriteLine(myHelloServerObject.HelloMethod(" Client One")); } }
/// <summary> /// Callback method for when HelloServer data is received from a client. /// </summary> /// <param name="id">The ID of the client.</param> /// <param name="helloServer">The HelloServer packet data.</param> private void OnHelloServer(ushort id, HelloServer helloServer) { Logger.Get().Info(this, $"Received HelloServer data from ID {id}"); // Start by sending the new client the current Server Settings _netServer.GetUpdateManagerForClient(id)?.UpdateGameSettings(GameSettings); if (!_playerData.TryGetValue(id, out var playerData)) { Logger.Get().Warn(this, $"Could not find player data for ID: {id}"); return; } playerData.CurrentScene = helloServer.SceneName; playerData.Position = helloServer.Position; playerData.Scale = helloServer.Scale; playerData.AnimationId = helloServer.AnimationClipId; var clientInfo = new List <(ushort, string)>(); foreach (var idPlayerDataPair in _playerData.GetCopy()) { if (idPlayerDataPair.Key == id) { continue; } clientInfo.Add((idPlayerDataPair.Key, idPlayerDataPair.Value.Username)); _netServer.GetUpdateManagerForClient(idPlayerDataPair.Key)?.AddPlayerConnectData( id, helloServer.Username ); } _netServer.GetUpdateManagerForClient(id).SetHelloClientData(clientInfo); try { PlayerConnectEvent?.Invoke(playerData); } catch (Exception e) { Logger.Get().Warn(this, $"Exception thrown while invoking PlayerConnect event, {e.GetType()}, {e.Message}, {e.StackTrace}"); } OnClientEnterScene(playerData); }
public static void Main() { // open channel and get handle to object ChannelServices.RegisterChannel(new TcpChannel()); //Registering the well known object on the client side for use by the operator new. RemotingConfiguration.RegisterWellKnownClientType(typeof(HelloServer), "tcp://localhost:8084/SayHello"); HelloServer obj = new HelloServer(); // call remote method Console.WriteLine(); Console.WriteLine("Before Call"); Console.WriteLine(obj.HelloMethod("Caveman")); Console.WriteLine(obj.HelloMethod("Spaceman")); Console.WriteLine(obj.HelloMethod("Client Man")); Console.WriteLine("After Call"); Console.WriteLine(); }
static async Task MainAsync(string[] args) { var isServer = args[0].Equals("server"); if (isServer) { var server = new HelloServer(); server.Listen(); Console.WriteLine("Listening..."); Console.ReadKey(); await server.ShutdownAsync(); } else { var client = new HelloClient(); client.Connect(); Console.WriteLine(await client.SendMessageAsync()); await client.ShutdownAsync(); } }
public HttpServerApp() { // Marshal an object over HTTP at http://localhost:8090/HttpHelloServer. const string myObjectUri = "HttpHelloServer"; const int myPort = 8090; HttpChannel myHttpChannel = new HttpChannel(myPort); ChannelServices.RegisterChannel(myHttpChannel); MarshalByRefObject myMbro = (MarshalByRefObject)Activator.CreateInstance(typeof(HelloServer)); RemotingServices.SetObjectUriForMarshal(myMbro, myObjectUri); ObjRef myObjRef = RemotingServices.Marshal(myMbro); Console.WriteLine("Press Enter to stop the demo."); Console.ReadLine(); HelloServer umObj = (HelloServer)RemotingServices.Unmarshal(myObjRef); RemotingServices.Disconnect(myMbro); }
public static void Main() { ChannelServices.RegisterChannel(new TcpChannel()); ActivatedClientTypeEntry myActivatedClientTypeEntry = new ActivatedClientTypeEntry(typeof(HelloServer), "tcp://localhost:8082"); // Register 'HelloServer' Type on the client end so that it can be // activated on the server. RemotingConfiguration.RegisterActivatedClientType( myActivatedClientTypeEntry); // Obtain a proxy object for the remote object. HelloServer myHelloServer = new HelloServer("ParameterString"); if (myHelloServer == null) { System.Console.WriteLine("Could not locate server"); } else { Console.WriteLine("Calling remote object"); Console.WriteLine(myHelloServer.HelloMethod("Bill")); } }
public override void ReadPacket() { ReadHeaders(Packet); // Read the byte flag representing which packets // are included in this update var dataPacketIdFlag = Packet.ReadUShort(); // Keep track of value of current bit var currentTypeValue = 1; for (var i = 0; i < Enum.GetNames(typeof(ServerPacketId)).Length; i++) { // If this bit was set in our flag, we add the type to the list if ((dataPacketIdFlag & currentTypeValue) != 0) { DataPacketIds.Add((ServerPacketId)i); } // Increase the value of current bit currentTypeValue *= 2; } /* * foreach (var item in DataPacketIds) * { * Logger.Info(this,$"reading packet with {Enum.GetName(typeof(ServerPacketId), item)}"); * } */ if (DataPacketIds.Contains(ServerPacketId.HelloServer)) { HelloServer.ReadData(Packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerUpdate)) { PlayerUpdate.ReadData(Packet); } if (DataPacketIds.Contains(ServerPacketId.EntityUpdate)) { EntityUpdates.ReadData(Packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerEnterScene)) { PlayerEnterScene.ReadData(Packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerTeamUpdate)) { PlayerTeamUpdate.ReadData(Packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerSkinUpdate)) { PlayerSkinUpdate.ReadData(Packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerEmoteUpdate)) { PlayerEmoteUpdate.ReadData(Packet); } }
public override Packet CreatePacket() { var packet = new Packet(); WriteHeaders(packet); // Construct the byte flag representing which packets are included // in this update ushort dataPacketIdFlag = 0; // Keep track of value of current bit ushort currentTypeValue = 1; /* * foreach (var item in DataPacketIds) * { * Logger.Info(this,$"creating packet with {Enum.GetName(typeof(ServerPacketId), item)}"); * } */ for (var i = 0; i < Enum.GetNames(typeof(ServerPacketId)).Length; i++) { // Cast the current index of the loop to a ServerPacketId and check if it is // contained in the update type list, if so, we add the current bit to the flag if (DataPacketIds.Contains((ServerPacketId)i)) { dataPacketIdFlag |= currentTypeValue; } currentTypeValue *= 2; } packet.Write(dataPacketIdFlag); // TODO: this is a mess, we have an interface that exposes a write and read method // to packets, but we don't really use the abstraction since we still need to // write and read in a specific order to ensure consistency // The same holds then for determining whether this packet contains reliable data // and finding a way to elegantly copy reliable data to a new packet if (DataPacketIds.Contains(ServerPacketId.HelloServer)) { HelloServer.WriteData(packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerUpdate)) { PlayerUpdate.WriteData(packet); } if (DataPacketIds.Contains(ServerPacketId.EntityUpdate)) { EntityUpdates.WriteData(packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerEnterScene)) { PlayerEnterScene.WriteData(packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerTeamUpdate)) { PlayerTeamUpdate.WriteData(packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerSkinUpdate)) { PlayerSkinUpdate.WriteData(packet); } if (DataPacketIds.Contains(ServerPacketId.PlayerEmoteUpdate)) { PlayerEmoteUpdate.WriteData(packet); } // Check whether there is reliable data written in this packet // and set the boolean value accordingly _containsReliableData = DataPacketIds.Contains(ServerPacketId.HelloServer) || DataPacketIds.Contains(ServerPacketId.PlayerEnterScene) || DataPacketIds.Contains(ServerPacketId.PlayerLeaveScene) || DataPacketIds.Contains(ServerPacketId.PlayerDeath) || DataPacketIds.Contains(ServerPacketId.PlayerTeamUpdate) || DataPacketIds.Contains(ServerPacketId.PlayerSkinUpdate); packet.WriteLength(); return(packet); }
public static void Main() { // Register TCP Channel. ChannelServices.RegisterChannel(new TcpChannel()); // Create activated client type entry. ActivatedClientTypeEntry myActivatedClientTypeEntry = new ActivatedClientTypeEntry(typeof(HelloServer), "tcp://localhost:8082"); // Register type on client to activate it on the server. RemotingConfiguration.RegisterActivatedClientType( myActivatedClientTypeEntry); // Activate a client activated object type. HelloServer myHelloServer = new HelloServer("ParameterString"); // Print the object type. Console.WriteLine( "Object type of client activated object: " + myActivatedClientTypeEntry.ObjectType.ToString()); myHelloServer.HelloMethod("Bill"); for (int i = 1; i < 10; i++) { myHelloServer.Hello("You name"); } myHelloServer.HelloWorld(); int sum = myHelloServer.Sum(4, 5); Console.WriteLine(sum); Console.ReadLine(); //// Print the application URL. //Console.WriteLine( // "Application url where the type is activated: " + // myActivatedClientTypeEntry.ApplicationUrl); //// Print the string representation of the type entry. //Console.WriteLine( // "Type name, assembly name and application URL " + // "of the remote object: " + // myActivatedClientTypeEntry.ToString()); //// Print a blank line. //Console.WriteLine(); //// Check that server was located. //if (myHelloServer == null) //{ // Console.WriteLine("Could not locate server"); //} //else //{ // Console.WriteLine("Calling remote object"); // Console.WriteLine(myHelloServer.HelloMethod("Bill")); //} }
public TcpClientApp() { const string myHelloServerUrl = "tcp://localhost:8085/TcpHelloServer"; HelloServer obj = (HelloServer)RemotingServices.Connect( typeof(HelloServer), myHelloServerUrl ); //</snippet11> //<snippet18> // GetRealProxy, GetObjectUri, GetEnvoyChainForProxy RealProxy proxy = RemotingServices.GetRealProxy(obj); Console.WriteLine("Real proxy type: {0}", proxy.GetProxiedType().ToString()); Console.WriteLine("Object URI: {0}", RemotingServices.GetObjectUri(obj).ToString()); IMessageSink msgSink = RemotingServices.GetEnvoyChainForProxy(obj).NextSink; //</snippet18> //<snippet12> //IsTransparentProxy, IsObjectOutOfAppDomain, IsObjectOutOfContext Console.WriteLine("Proxy transparent? {0}", RemotingServices.IsTransparentProxy(obj) ? "Yes" : "No" ); Console.WriteLine("Object outside app domain? {0}", RemotingServices.IsObjectOutOfAppDomain(obj) ? "Yes" : "No" ); Console.WriteLine("Object out of context? {0}", RemotingServices.IsObjectOutOfContext(obj) ? "Yes" : "No" ); //</snippet12> //<snippet21> // GetLifetimeService ILease lease = (ILease)RemotingServices.GetLifetimeService(obj); Console.WriteLine("Object lease time remaining: {0}.", lease.CurrentLeaseTime.ToString() ); //</snippet21> //<snippet16> string twoWayMethodArg = "John"; Console.WriteLine("Invoking SayHelloToServerAndWait(\"{0}\").", twoWayMethodArg); Console.WriteLine("Server returned: {0}", obj.SayHelloToServerAndWait(twoWayMethodArg)); //</snippet16> //<snippet17> string oneWayMethodArg = "Mary"; Console.WriteLine("Invoking SayHelloToServer(\"{0}\").", oneWayMethodArg); obj.SayHelloToServer(oneWayMethodArg); //</snippet17> //<snippet23> }