Esempio n. 1
0
        /// <summary>
        /// Adds the <see cref="GladNetInputFormatter"/> and <see cref="GladNetOutputFormatter"/> to the known formatters.
        /// Also registers the gladnet media header to map to these formatters.
        /// </summary>
        /// <param name="builder">Builder to chain off.</param>
        /// <param name="serializerStrat">Serialization strategy</param>
        /// <param name="deserializerStrat">Deserialization strategy.</param>
        /// <param name="registry">Serialization registry.</param>
        /// <returns>The fluent <see cref="IServiceCollection"/> instance.</returns>
        public static IServiceCollection AddGladNet(this IServiceCollection builder, ISerializerStrategy serializerStrat, IDeserializerStrategy deserializerStrat, ISerializerRegistry registry)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (serializerStrat == null)
            {
                throw new ArgumentNullException(nameof(serializerStrat));
            }
            if (deserializerStrat == null)
            {
                throw new ArgumentNullException(nameof(deserializerStrat));
            }
            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }

            //need to register these types
            registry.Register(typeof(NetworkMessage));
            registry.Register(typeof(PacketPayload));
            registry.Register(typeof(RequestMessage));
            registry.Register(typeof(ResponseMessage));

            return(builder.AddMvcCore()
                   .AddGladNetFormatters(serializerStrat, deserializerStrat).Services);
        }
Esempio n. 2
0
        public InternalClientSessionFactory(AUIDServiceCollection <ClientSessionServiceContext> peerServiceCollection, ISerializerStrategy serializer, ILog peerLogger, IAUIDService <INetPeer> netPeerAuidService, IManagedClientSessionFactory managedSessionFactory)
        {
            if (peerServiceCollection == null)
            {
                throw new ArgumentNullException(nameof(peerServiceCollection));
            }
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }
            if (peerLogger == null)
            {
                throw new ArgumentNullException(nameof(peerLogger));
            }
            if (netPeerAuidService == null)
            {
                throw new ArgumentNullException(nameof(netPeerAuidService));
            }
            if (managedSessionFactory == null)
            {
                throw new ArgumentNullException(nameof(managedSessionFactory));
            }

            PeerServiceCollection = peerServiceCollection;
            Serializer            = serializer;
            PeerLogger            = peerLogger;
            NetPeerAUIDService    = netPeerAuidService;
            ManagedSessionFactory = managedSessionFactory;
        }
Esempio n. 3
0
        protected ApplicationBase(IDeserializerStrategy deserializer, ISerializerStrategy serializer, ILog logger, IManagedClientSessionFactory sessionManagedFactory)
        {
            if (deserializer == null)
            {
                throw new ArgumentNullException(nameof(deserializer));
            }
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            Logger = logger;

            //Init internal services/components
            //Create these first; thread needs them
            peerServiceCollection = new AUIDServiceCollection <ClientSessionServiceContext>(50);
            netPeerAUIDService    = new AUIDNetPeerServiceDecorator(peerServiceCollection);

            managedNetworkThread = new ManagedLidgrenNetworkThread(serializer, new LidgrenServerMessageContextFactory(deserializer), new PeerSendServiceSelectionStrategy(peerServiceCollection), e => Logger.Fatal($"{e.Message} StackTrace: {e.StackTrace}"));
            InternalClientSessionFactory sessionFactory = new InternalClientSessionFactory(peerServiceCollection, serializer, Logger, netPeerAUIDService, sessionManagedFactory);

            sessionlessHandler = new SessionlessMessageHandler(sessionFactory, Logger);
        }
Esempio n. 4
0
		//Message responses are dispatched right after requests with this strategy. This is much different
		//than what is normally found in GladNet2 implementations.
		/// <summary>
		/// Creates a new <see cref="IWebRequestEnqueueStrategy"/> that handles requests on the calling thread
		/// and blocks.
		/// </summary>
		/// <param name="baseURL">Base string for the end-poind webserver. (Ex. www.google.com/)</param>
		/// <param name="deserializationService">Deserialization strategy for responses.</param>
		/// <param name="responseReciever">Message receiver service for dispatching recieved resposne messages.</param>
		public RestSharpCurrentThreadEnqueueRequestHandlerStrategy(string baseURL, IDeserializerStrategy deserializationService, ISerializerStrategy serializerStrategy, INetworkMessageReceiver responseReciever, int connectionAUID
#if !ENDUSER
			, INetworkMessageRouteBackService routebackService) //unfortunaly the single-threaded blocking enqueue strat needs the routeback service to function.
#endif
		{
			if (String.IsNullOrEmpty(baseURL))
				throw new ArgumentException($"Parameter {baseURL} is not valid. Either null or empty. Base URL must be the base URL of the web server. (Ex. www.google.com)");

			if (deserializationService == null)
				throw new ArgumentNullException(nameof(baseURL), $"Parameter {deserializationService} must not be null. Deserialization for incoming responses is required.");
#if !ENDUSER
			if (routebackService == null)
				throw new ArgumentNullException(nameof(routebackService), $"{nameof(RestSharpCurrentThreadEnqueueRequestHandlerStrategy)} requires {(nameof(INetworkMessageRouteBackService))} as it handles responses too.");
#endif
			serializer = serializerStrategy;
			deserializer = deserializationService;
			responseMessageRecieverService = responseReciever;

			restsharpMiddlewares = new List<IRestSharpMiddleWare>(2);

			ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
			System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

			httpClient = new RestClient(baseURL) { Timeout = 15000 };

#if !ENDUSER
			messageRoutebackService = routebackService;
#endif
		}
		/// <summary>
		/// Creates a new <see cref="NetClient"/>-based network message router service for Lidgren.
		/// </summary>
		/// <param name="messageFactory">The network message factory.</param>
		/// <param name="peerObj">The <see cref="NetPeer"/> object.</param>
		/// <param name="serializerStrategy">Serialization strategy.</param>
		public LidgrenClientNetworkMessageRouterService(INetworkMessageFactory messageFactory, NetConnection connection, ISerializerStrategy serializerStrategy)
			: base(messageFactory, connection)
		{
			if (serializerStrategy == null)
				throw new ArgumentNullException(nameof(serializerStrategy), $"Cannot provide a null {nameof(ISerializerStrategy)} to {nameof(LidgrenClientNetworkMessageRouterService)}.");

			serializer = serializerStrategy;
		}
Esempio n. 6
0
        /// <summary>
        /// Creates a new Photon compatible <see cref="INetworkMessageRouterService"/>.
        /// </summary>
        /// <param name="peer"><see cref="PeerBase"/> to route messages to.</param>
        /// <param name="serializer">Serialization strategy to use for outgoing messages.</param>
        public PhotonServerINetworkMessageSenderClientAdapter(PeerBase peer, ISerializerStrategy serializer)
        {
            Throw <ArgumentNullException> .If.IsNull(peer)?.Now(nameof(peer));

            Throw <ArgumentNullException> .If.IsNull(serializer)?.Now(nameof(serializer));

            photonPeer         = peer;
            serializerStrategy = serializer;
        }
        /// <summary>
        /// Creates a new <see cref="NetClient"/>-based network message router service for Lidgren.
        /// </summary>
        /// <param name="messageFactory">The network message factory.</param>
        /// <param name="peerObj">The <see cref="NetPeer"/> object.</param>
        /// <param name="serializerStrategy">Serialization strategy.</param>
        public LidgrenServerNetworkMessageRouterService(INetworkMessageFactory messageFactory, NetConnection connection, ISerializerStrategy serializerStrategy)
            : base(messageFactory, connection)
        {
            if (serializerStrategy == null)
            {
                throw new ArgumentNullException(nameof(serializerStrategy));
            }

            serializer = serializerStrategy;
        }
Esempio n. 8
0
        public byte[] SerializeWithVisitor(ISerializerStrategy serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            //We can only serializer this type by pretending it's a real StatusMessage.
            //Probably what everyone expects anyway.
            return(serializer.Serialize(new StatusMessage(new StatusChangePayload(Status))));
        }
		public ManagedLidgrenNetworkThread(ISerializerStrategy serializerStrategy, ILidgrenMessageContextFactory lidgrenMessageContextFactory, ISendServiceSelectionStrategy sendStrategy, Action<Exception> onException = null)
		{
			if (serializerStrategy == null)
				throw new ArgumentNullException(nameof(serializerStrategy), $"Provided {nameof(ISerializerStrategy)} cannot be null.");

			if (lidgrenMessageContextFactory == null)
				throw new ArgumentNullException(nameof(lidgrenMessageContextFactory), $"Provided {nameof(ILidgrenMessageContextFactory)} cannot be null.");

			if (sendStrategy == null)
				throw new ArgumentNullException(nameof(sendStrategy), $"Provided {nameof(ISendServiceSelectionStrategy)} cannot be null.");

			OnException = onException;

			sendServiceStrategy = sendStrategy;
			messageContextFactory = lidgrenMessageContextFactory;
			serializer = serializerStrategy;

			managedNetworkThreads = new List<Thread>();
		}
Esempio n. 10
0
        public JWTAuthenticationMiddleware(IDeserializerStrategy deserializer, ISerializerStrategy serializer, ITokenRegistry tokenRegistry)
        {
            if (deserializer == null)
            {
                throw new ArgumentNullException(nameof(deserializer));
            }
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }
            if (tokenRegistry == null)
            {
                throw new ArgumentNullException(nameof(tokenRegistry));
            }

            deserializerStrategy = deserializer;
            serializerStrategy   = serializer;
            tokenRegistryService = tokenRegistry;
        }
 //This shouldn't be called
 public byte[] SerializeWithVisitor(ISerializerStrategy serializer)
 {
     throw new NotImplementedException($"{nameof(PhotonStatusMessageAdapter)} doesn't need to serialize the fake status message.");
 }
 /// <inheritdoc />
 public InternalUnityApplicationBase(IDeserializerStrategy deserializer, ISerializerStrategy serializer, ILog logger, IManagedClientSessionFactory sessionManagedFactory)
     : base(deserializer, serializer, logger, sessionManagedFactory)
 {
 }
		public byte[] SerializeWithVisitor(ISerializerStrategy serializer)
		{
			//We can only serializer this type by pretending it's a real StatusMessage.
			//Probably what everyone expects anyway.
			return serializer.Serialize(new StatusMessage(new StatusChangePayload(Status)));
		}
Esempio n. 14
0
        /// <summary>
        /// Adds the <see cref="GladNetInputFormatter"/> and <see cref="GladNetOutputFormatter"/> to the known formatters.
        /// Also registers the gladnet media header to map to these formatters.
        /// </summary>
        /// <param name="builder">Builder to chain off.</param>
        /// <param name="serializerStrat">Serialization strategy</param>
        /// <param name="deserializerStrat">Deserialization strategy.</param>
        /// <returns>The fluent <see cref="IMvcCoreBuilder"/> instance.</returns>
        public static IMvcBuilder AddGladNetFormatters([NotNull] this IMvcBuilder builder, [NotNull] ISerializerStrategy serializerStrat, [NotNull] IDeserializerStrategy deserializerStrat)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (serializerStrat == null)
            {
                throw new ArgumentNullException(nameof(serializerStrat));
            }
            if (deserializerStrat == null)
            {
                throw new ArgumentNullException(nameof(deserializerStrat));
            }

            //Add the formatters to the options.
            return(builder.AddMvcOptions(options =>
            {
                options.InputFormatters.Add(new GladNetInputFormatter(deserializerStrat));
                options.OutputFormatters.Add(new GladNetOutputFormatter(serializerStrat));
                options.FormatterMappings.SetMediaTypeMappingForFormat("GladNet", MediaTypeHeaderValue.Parse("application/gladnet"));
            }));
        }
 public Foo(ISerializerStrategy <PrescriberDto> serializer)
 {
     // ...
 }
		//This shouldn't be called
		public byte[] SerializeWithVisitor(ISerializerStrategy serializer)
		{
			throw new NotImplementedException($"{nameof(PhotonStatusMessageAdapter)} doesn't need to serialize the fake status message.");
		}
        /// <summary>
        /// Registers the Middlewares required for JWT authorization.
        /// </summary>
        /// <typeparam name="TMiddlewareRegistryType"></typeparam>
        /// <param name="middlewarRegistry"></param>
        /// <param name="serializer"></param>
        /// <param name="deserializer"></param>
        /// <param name="tokenService"></param>
        /// <returns>Registy to fluently chain on.</returns>
        public static TMiddlewareRegistryType RegisterAuthorizationMiddleware <TMiddlewareRegistryType>(this TMiddlewareRegistryType middlewarRegistry, ISerializerStrategy serializer, IDeserializerStrategy deserializer, ITokenService tokenService)
            where TMiddlewareRegistryType : IMiddlewareRegistry
        {
            //Register the middlewares
            middlewarRegistry.Register(new JWTAuthorizationHeaderMiddleware(tokenService));

            //return for fluent chaining
            return(middlewarRegistry);
        }
        /// <summary>
        /// Registers the Middlewares required for authentication.
        /// </summary>
        /// <typeparam name="TMiddlewareRegistryType"></typeparam>
        /// <param name="middlewarRegistry"></param>
        /// <param name="serializer"></param>
        /// <param name="deserializer"></param>
        /// <param name="tokenRegister"></param>
        /// <returns>Registy to fluently chain on.</returns>
        public static TMiddlewareRegistryType RegisterAuthenticationMiddleware <TMiddlewareRegistryType>(this TMiddlewareRegistryType middlewarRegistry, ISerializerStrategy serializer, IDeserializerStrategy deserializer, ITokenRegistry tokenRegister)
            where TMiddlewareRegistryType : IMiddlewareRegistry
        {
            //Register the middlewares
            middlewarRegistry.Register(new JWTAuthenticationMiddleware(deserializer, serializer, tokenRegister));

            //return for fluent chaining
            return(middlewarRegistry);
        }
        public GladNetOutputFormatter(ISerializerStrategy serializationStrategy)
        {
            serializerStrategy = serializationStrategy ?? throw new ArgumentNullException(nameof(serializationStrategy), $"Input formatter requires a serialization strategy.");

            SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/gladnet"));
        }