Ejemplo n.º 1
0
        /// <summary>
        /// Start a new server that listens for connections from anyone.
        /// </summary>
        /// <param name="name">The unique name of the server.</param>
        /// <param name="resolver">For resolving custom message types received from the client.</param>
        /// <param name="ipAddress">Determines which local network interface to use.</param>
        /// <param name="udpGroupAddress">Optional request to setup UDP listener, pass null if you don't want that.</param>
        /// <param name="udpGroupPort">Optional port required if you provide udpGroupAddress.</param>
        /// <returns>Returns the new server object.</returns>
        public static SmartSocketServer StartServer(string name, SmartSocketTypeResolver resolver, string ipAddress,
                                                    string udpGroupAddress = "226.10.10.2", int udpGroupPort = 37992)
        {
            if (string.IsNullOrEmpty(ipAddress))
            {
                ipAddress = "127.0.0.1:0";
            }

            SmartSocketServer server = new SmartSocketServer(name, resolver, ipAddress, udpGroupAddress, udpGroupPort);

            server.StartListening();
            return(server);
        }
Ejemplo n.º 2
0
        internal SmartSocketClient(SmartSocketServer server, Socket client, SmartSocketTypeResolver resolver)
        {
            this.Client    = client;
            this.Stream    = new NetworkStream(client);
            this.Server    = server;
            this.Resolver  = resolver;
            client.NoDelay = true;

            DataContractSerializerSettings settings = new DataContractSerializerSettings();

            settings.DataContractResolver     = this.Resolver;
            settings.PreserveObjectReferences = true;
            this.Serializer = new DataContractSerializer(typeof(MessageWrapper), settings);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create another socket that will allow the server to send messages to the client any time.
        /// It is expected you will start a ReceiveAsync loop on this server object to process
        /// those messages.
        /// </summary>
        /// <param name="connectedHandler">An event handler to invoke when the server opens the back channel.</param>
        /// <returns>New server object that will get one ClientConnected event when the remote server connects.</returns>
        public async Task <SmartSocketServer> OpenBackChannel(EventHandler <SmartSocketClient> connectedHandler)
        {
            IPEndPoint ipe = (IPEndPoint)this.Socket.LocalEndPoint;
            // start a new server that does not use UDP.
            var server = SmartSocketServer.StartServer(this.Name, this.Resolver, ipe.Address.ToString(), null, 0);

            server.ClientConnected += connectedHandler;
            int port = server.EndPoint.Port;
            // tell the server we've opened another channel and pass the "port" number
            var response = await this.SendReceiveAsync(new SocketMessage(OpenBackChannelMessageId, this.Name + ":" + port));

            if (response.Id == ErrorMessageId)
            {
                throw new InvalidOperationException(response.Message);
            }

            return(server);
        }