예제 #1
0
        /// <summary>
        /// Attempts to connect to the given hostname using the given nickname.
        /// </summary>
        /// <param name="hostname">The server address, excluding the port.</param>
        /// <param name="nickname">The nickname to use for the player connecting.</param>
        private void Connect(string hostname, string nickname)
        {
            // Connect to the server.
            ClientNetworking.ConnectToServer(
                hostname,
                11000,
                state =>
            {
                _socketState = state;

                // Listen for when data is received on the socket.
                _socketState.DataReceived += DataReceived;

                // Listen for when the socket disconnects.
                _socketState.Disconnected += () => { Disconnected?.Invoke(); };

                // Send the nickname of the user.
                AbstractNetworking.Send(state, nickname + '\n');

                // Wait for data.
                AbstractNetworking.GetData(state);
            },
                reason => _connectionFailedCallback(reason)
                );
        }
예제 #2
0
        /// <summary>
        /// This attempts to connect to the server at the ip provided.
        /// </summary>
        /// <param name="server">String ip of the server.</param>
        /// <param name="name">Name to send.</param>
        public void ConnectToServer(String server, String name = "Tarun")
        {
            // This is where we connect to the server for the first time. After the setup is done we
            // want our callback to be FirstContact.
            ClientNetworking.ConnectToServer(server,
                                             state =>
            {
                SuccessfulConnection?.Invoke("Connected to host: " + server);

                _socketState = state;

                // Listen for when data is received on the socket.
                _socketState.DataReceived += DataReceived;

                // Listen for when the socket disconnects.
                _socketState.Disconnected += () => { Disconnected?.Invoke(); };

                // Send the register message with the server.
                Debug.WriteLine(REGISTER, "sending register message");
                AbstractNetworking.Send(state, REGISTER);

                // Wait for data.
                AbstractNetworking.GetData(state);
            },
                                             reason => ErrorCallback?.Invoke(reason));
        }
예제 #3
0
        /// <summary>
        /// Called when data is received on the socket.
        /// </summary>
        /// <param name="data">The data that was received.</param>
        public void DataReceived(string data)
        {
            // We know the first packet has been handled once the world is not null.
            if (GameWorld == null)
            {
                ParseFirstPacket(data);
            }
            else
            {
                ParseJsonPacket(data);
            }

            // Get new data.
            AbstractNetworking.GetData(_socketState);
        }
예제 #4
0
        /// <summary>
        /// Called when data is received from the client.
        /// </summary>
        /// <param name="data">The data from the client.</param>
        private void OnDataReceived(string data)
        {
            // Check for nickname packet.
            if (!_nicknameReceived)
            {
                _nicknameReceived = true;

                // Trim newline from nickname and invoke event.
                var nickname = data.Replace("\n", "");
                NicknameReceived?.Invoke(nickname);

                // Send the first packet to the client.
                SendFirstPacket();

                // Listen for server events.
                _gameServerController.WorldUpdated += OnWorldUpdated;
            }
            else
            {
                // Thrust
                if (data.Contains("T"))
                {
                    ClientCommands[Ship.Command.Thrust] = true;
                }

                // Left or Right
                if (data.Contains("L"))
                {
                    ClientCommands[Ship.Command.Left] = true;
                }
                else if (data.Contains("R"))
                {
                    ClientCommands[Ship.Command.Right] = true;
                }

                // Fire
                if (data.Contains("F"))
                {
                    ClientCommands[Ship.Command.Fire] = true;
                }
            }

            AbstractNetworking.GetData(_state);
        }
예제 #5
0
 /// <summary>
 /// Asynchronously begins listening for client data.
 /// </summary>
 public void BeginListeningAsync()
 {
     AbstractNetworking.GetData(_state);
 }
예제 #6
0
        /// <summary>
        /// Called when data is received on the socket.
        /// </summary>
        /// <param name="data">The data that was received.</param>
        public void DataReceived(string data)
        {
            var commands = data.Split(Convert.ToChar(END_OF_TEXT));

            foreach (var message in commands)
            {
                if (message.Trim() == "")
                {
                    continue;
                }

                // Add back EOT after being split.
                var eotMessage = message + END_OF_TEXT;

                Debug.WriteLine(eotMessage, "data recieved from from server ");
                // If a disconnect message is received, Disconnect the client
                if (eotMessage.Equals(DISCONNECT))
                {
                    pingTimer.Stop();
                    serverTimer.Stop();
                    DisconnectSpreadsheetCallback?.Invoke();
                }

                // If a ping is received from the Server, send a ping_response back
                if (eotMessage.Equals(PING))
                {
                    AbstractNetworking.Send(_socketState, PING_RESPONSE);
                }

                // If a ping response is received from the Server, the Server ping response timer is reset
                if (eotMessage.Equals(PING_RESPONSE))
                {
                    // timer ensuring Server is still up resets, Server has another 60 seconds until
                    // another ping_response is necessary
                    serverTimer.Stop(); serverTimer.Start();
                }

                // We know the first packet has been handled once the world is not null.
                if (eotMessage.Equals(FILE_LOAD_ERROR) || eotMessage.StartsWith(CONNECTION_ACCEPTED_PREFIX))
                {
                    ParseFirstPacket(eotMessage);
                }
                else
                {
                    // full_state is only received upon initial loading of spreadsheet
                    // and the ping loop begins after the full_state message is received
                    if (eotMessage.StartsWith(FULL_STATE_PREFIX))
                    {
                        FullStateDocumentDocument(eotMessage);

                        // if serverTimer reaches 60s, disconnect from the Server
                        serverTimer = new Timer(60000)
                        {
                            Enabled   = true,
                            AutoReset = false
                        };
                        serverTimer.Elapsed += Disconnect;

                        // every 10 seconds (10000 milliseconds) another ping is sent to the Server
                        pingTimer = new Timer(10000)
                        {
                            Enabled = true
                        };
                        pingTimer.Elapsed += Ping;

                        // ping loop begins as both timers are started
                        pingTimer.Start();
                        serverTimer.Start();
                    }
                    else if (eotMessage.StartsWith(CHANGE_PREFIX))
                    {
                        ChangeDocument(eotMessage);
                    }
                    else if (eotMessage.StartsWith(FOCUS_PREFIX))
                    {
                        Focus_Cell(eotMessage, FOCUS_PREFIX);
                    }
                    else if (eotMessage.StartsWith(UNFOCUS_PREFIX))
                    {
                        Unfocus_Cell(eotMessage, UNFOCUS_PREFIX);
                    }
                }
            }

            // Get new data.
            AbstractNetworking.GetData(_socketState);
        }