예제 #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       = "127.0.0.1",
                                                    string udpGroupAddress = "226.10.10.2",
                                                    int udpGroupPort       = 37992)
        {
            SmartSocketServer server = new SmartSocketServer(name, resolver, ipAddress, udpGroupAddress, udpGroupPort);

            server.StartListening();
            return(server);
        }
예제 #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);
        }
예제 #3
0
        /// <summary>
        /// Create another socket that will allow the server to send messages to us 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 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.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);
        }