Esempio n. 1
0
        /// <summary>
        /// Disconnects from the server.
        /// This should reset this client to default state.
        /// </summary>
        public void Disconnect()
        {
            Debug.WriteLine("disconnecting");
            // Sending disconnect message to the server, if state isn't null
            if (_socketState != null)
            {
                AbstractNetworking.Send(_socketState, DISCONNECT);
            }
            // stopping timers
            pingTimer?.Stop();
            serverTimer?.Stop();

            DisconnectSpreadsheetCallback?.Invoke();
        }
Esempio n. 2
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);
        }