Example #1
0
 public static JsEncoder.TableValue ToTable(HostInformation Value)
 {
     JsEncoder.TableValue r = new JsEncoder.TableValue();
     r[1] = new JsEncoder.StringValue(Value.HostName);
     r[2] = new JsEncoder.IntValue(Value.Port);
     return(r);
 }
Example #2
0
        public void SendMessage(string MessageHeader, IEnumerable <JsEncoder.IAbstractValue> Contents, bool SendImmediately)
        {
            AssertNotDisposed();

            if (_ConnectionStatus == 1 || _ConnectionStatus == 2)
            {
                List <JsEncoder.IAbstractValue> ResponseArray = new List <JsEncoder.IAbstractValue>()
                {
                    new JsEncoder.StringValue(MessageHeader)
                };
                foreach (JsEncoder.IAbstractValue item in Contents)
                {
                    ResponseArray.Add(item);
                }
                JsEncoder.TableValue ResponseTable = JsEncoder.TableValue.ArrayToTable(ResponseArray.ToArray());

                if (SendImmediately)
                {
                    JsEncoder.IAbstractValue[] Param1 = new JsEncoder.IAbstractValue[1];
                    Param1[0] = ResponseTable;
                    Encoding_SendInput(Param1);
                }
                else
                {
                    _MessageOutbox.Add(ResponseTable);
                }
            }
        }
Example #3
0
        public static JsEncoder.TableValue ToTable(UserHostList Value)
        {
            JsEncoder.TableValue t  = new JsEncoder.TableValue();
            JsEncoder.TableValue ft = new JsEncoder.TableValue();
            JsEncoder.TableValue rt = new JsEncoder.TableValue();
            t[1] = ft;
            t[2] = rt;
            HostInformation[] fa = Value.FavoriteHosts.ToArray();
            HostInformation[] ra = Value.RecentHosts.ToArray();
            int fl = fa.Length;
            int rl = ra.Length;

            for (int i = 0; i < fl; i++)
            {
                JsEncoder.TableValue item = HostInformation.ToTable(fa[i]);
                ft[i + 1] = item;
            }
            for (int i = 0; i < rl; i++)
            {
                JsEncoder.TableValue item = HostInformation.ToTable(ra[i]);
                rt[i + 1] = item;
            }

            return(t);
        }
Example #4
0
        public void Save()
        {
            if (_IsLoaded)
            {
                DirectoryIO.CreateDirectory(DataSavePath);

                String HostListAsText = JsEncoder.EncoderStream.EncodeTable(UserHostList.ToTable(HostList));
                String ConfigAsText; // This won't be null after the region below

                #region ConfigFileSaving
                {
                    JsEncoder.TableValue ConfigTable = new JsEncoder.TableValue();
                    ConfigTable[new JsEncoder.StringValue("UserName")]    = new JsEncoder.StringValue(UserName);
                    ConfigTable[new JsEncoder.StringValue("WorkingPort")] = new JsEncoder.IntValue(WorkingPort);

                    ConfigAsText = JsEncoder.EncoderStream.EncodeTable(ConfigTable);
                }
                #endregion

                String VersionFileBackupPath  = String.Concat(VersionFilePath, ".bak");
                String ConfigFileBackupPath   = String.Concat(ConfigFilePath, ".bak");
                String HostListFileBackupPath = String.Concat(HostListFilePath, ".bak");

                if (FileIO.Exists(VersionFileBackupPath))
                {
                    FileIO.Delete(VersionFileBackupPath);
                }
                if (FileIO.Exists(ConfigFileBackupPath))
                {
                    FileIO.Delete(ConfigFileBackupPath);
                }
                if (FileIO.Exists(HostListFileBackupPath))
                {
                    FileIO.Delete(HostListFileBackupPath);
                }

                if (FileIO.Exists(VersionFilePath))
                {
                    FileIO.Move(VersionFilePath, VersionFileBackupPath);
                }
                if (FileIO.Exists(ConfigFilePath))
                {
                    FileIO.Move(ConfigFilePath, ConfigFileBackupPath);
                }
                if (FileIO.Exists(HostListFilePath))
                {
                    FileIO.Move(HostListFilePath, HostListFileBackupPath);
                }

                FileIO.WriteAllText(VersionFilePath, NetworkConfig.VersionString, System.Text.Encoding.Unicode);
                FileIO.WriteAllText(ConfigFilePath, ConfigAsText, System.Text.Encoding.Unicode);
                FileIO.WriteAllText(HostListFilePath, HostListAsText, System.Text.Encoding.Unicode);
            }
        }
Example #5
0
        public PeerMessage(JsEncoder.TableValue Table)
        {
            this.Table = Table;
            JsEncoder.StringValue headerV = (JsEncoder.StringValue)Table[1];
            Header = headerV.Value;

            // First two values
            Contents1V = Table[2];
            Contents2V = Table[3];

            // Convert them to Strings
            JsEncoder.StringValue?contents1SV = Contents1V as JsEncoder.StringValue?;
            JsEncoder.StringValue?contents2SV = Contents2V as JsEncoder.StringValue?;
            Contents1S = contents1SV?.Value;
            Contents2S = contents2SV?.Value;
        }
Example #6
0
        public static UserHostList FromTable(JsEncoder.TableValue Value)
        {
            JsEncoder.TableValue ft = (JsEncoder.TableValue)Value[1];
            JsEncoder.TableValue rt = (JsEncoder.TableValue)Value[2];
            UserHostList         r  = new UserHostList();

            for (int i = 1; ft.ContainsKey(i); i++)
            {
                JsEncoder.IAbstractValue item = ft[i];
                JsEncoder.TableValue     v    = (JsEncoder.TableValue)item;
                r.FavoriteHosts.Add(HostInformation.FromTable(v));
            }
            for (int i = 1; rt.ContainsKey(i); i++)
            {
                JsEncoder.IAbstractValue item = rt[i];
                JsEncoder.TableValue     v    = (JsEncoder.TableValue)item;
                r.RecentHosts.Add(HostInformation.FromTable(v));
            }

            return(r);
        }
Example #7
0
        // Other Things
        public void RunCycle(float DeltaTime)
        {
            AssertNotDisposed();

#if !EXPOSE_ERRORS
            try
#endif
            {
                // Connection-State Specific Operations
                switch (_ConnectionStatus)
                {
                case 1:     // Connected

                    // Send HeartBeats
                    _HeartBeatSendTimer += DeltaTime;
                    if (_HeartBeatSendTimer > NetworkConfig.HeartBeatInterval)
                    {
                        SendMessage("HEARTBEAT", "");
                        _HeartBeatSendTimer = 0;
                    }

                    // HeartBeat listening timer. "How long ago did I last hear you?"
                    _HeartBeatTimeout += DeltaTime;
                    if (_HeartBeatTimeout > NetworkConfig.HeartBeatSilenceTimeout)
                    {
                        _HeartBeatTimeout = 0;
                        DropConnection();
                        Log_Write_System("The HeartBeat timeout was exceeded! The connection was dropped.");
                    }
                    break;

                case 2:     // Connecting
                    if (AutoSendGreeting && !_GreetingSent)
                    {
                        SendGreeting();
                    }
                    else if (_GreetingSent & _GreetingReceived)
                    {
                        ChangeConnectionStatusValue(1);
                    }

                    _ConnectionTimeout += DeltaTime;
                    if (_ConnectionTimeout > NetworkConfig.ConnectionTimeout)
                    {
                        _ConnectionTimeout = 0;
                        DropConnection();
                        Log_Write_System("The connection timeout was exceeded! The connection was dropped.");
                    }

                    break;

                case 3:     // Disconnecting
                    _DisconnectTimer += DeltaTime;
                    if (_DisconnectTimer > NetworkConfig.DisconnectWait)
                    {
                        _DisconnectTimer = 0;
                        DropConnection();
                        Log_Write_System("Disconnected!");
                    }
                    break;

                default:
                    break;
                }

                // Mailing Procedure
                if (_Socket != null && _Socket.Connected)
                {
                    // ## BEGIN Receive

                    // Check on what was received.
                    JsEncoder.IAbstractValue[] output = Encoding_CollectOutput();
                    foreach (JsEncoder.IAbstractValue item in output)
                    {
                        // Debug
                        //LogSystemHumanMessage("RECEIVED: " + JsEncoder.EncoderStream.EncodeValue(item));

                        // Essential Information
                        JsEncoder.TableValue outputT = (JsEncoder.TableValue)item;
                        PeerMessage          m       = new PeerMessage(outputT);
                        string mh = m.Header;

                        if (mh == "GREETING")
                        {
                            if (_ConnectionStatus == 2)
                            {
                                bool   VersionMatches = (m.Contents1S == (string)NetworkConfig.VersionString);
                                string PeerID         = m.Contents2S;
                                _OtherPeerID      = PeerID;
                                _GreetingReceived = true;
                                if (VersionMatches)
                                {
                                    Log_Write_System("The other peer greeted you.");
                                }
                                else
                                {
                                    Log_Write_System("The other peer sent you a greeting with an unmatching version string. Incompatibilities may occur.");
                                }
                                if (_GreetingSent)
                                {
                                    ChangeConnectionStatusValue(1);
                                }
                            }
                        }
                        else if (mh == "DISCONNECTING")
                        {
                            if (_ConnectionStatus == 1)
                            {
                                BeginDisconnect(false);
                            }
                            break;
                        }
                        else if (mh == "HEARTBEAT")
                        {
                            _HeartBeatTimeout = 0;
                        }
                        else if (mh == "HUMANMESSAGE")
                        {
                            Log_Write(string.Format("({0}) {1}", OtherPeerDisplayName, m.Contents1S));
                        }

                        OutputMessage(new PeerMessage(outputT));
                    }

                    // ## END Receive

                    // ## BEGIN Send

                    // Debug
                    //foreach (JsEncoder.ValueBase item in _MessageQueue)
                    //    LogSystemHumanMessage("SENT: " + JsEncoder.EncoderStream.EncodeValue(item));

                    // Send the pending messages
                    // Don't send anything on 3 because we want to stop communications on that stage.
                    if (_ConnectionStatus == 1 || _ConnectionStatus == 2)
                    {
                        Encoding_SendInput(_MessageOutbox.ToArray());
                    }
                    _MessageOutbox.Clear();

                    // ## END Send
                }
            }
#if !EXPOSE_ERRORS
            catch (Exception e)
            {
                DropConnection();
                Log_Write_System(string.Concat("You lost connection due to an error. Error Message: ", e.Message));
            }
#endif
        }
Example #8
0
 public static HostInformation FromTable(JsEncoder.TableValue Value)
 {
     JsEncoder.StringValue HostNameV = (JsEncoder.StringValue)Value[1];
     JsEncoder.IntValue    PortV     = (JsEncoder.IntValue)Value[2];
     return(new HostInformation(HostNameV.Value, PortV.Value));
 }
Example #9
0
        public bool Load()
        {
            bool r = false;

            bool ve = FileIO.Exists(VersionFilePath);

            if (ve)
            {
                String VersionFileContents = FileIO.ReadAllText(VersionFilePath, System.Text.Encoding.Unicode);
                if (VersionFileContents == NetworkConfig.VersionString)
                {
                    r = true;
                }
                else
                {
                    String IncompatPath = String.Concat(DataSavePath, "_", VersionFileContents);
                    if (DirectoryIO.Exists(IncompatPath))
                    {
                        DirectoryIO.Delete(IncompatPath, true);
                    }
                    DirectoryIO.Move(DataSavePath, IncompatPath);
                    System.Windows.Forms.MessageBox.Show(String.Concat(
                                                             "The application tried to load Js ChatterBox files that were incompatible ",
                                                             "with this version. They have been moved to \"", IncompatPath, "\"."));
                }
            }

            if (r)
            {
                try
                {
                    #region ConfigFileLoading
                    {
                        String ConfigText = FileIO.ReadAllText(ConfigFilePath, System.Text.Encoding.Unicode);
                        JsEncoder.TableValue ConfigTable = (JsEncoder.TableValue)JsEncoder.DecoderStream.DecodeValue(ConfigText);

                        UserName    = ((JsEncoder.StringValue)ConfigTable[new JsEncoder.StringValue("UserName")]).Value;
                        WorkingPort = ((JsEncoder.IntValue)ConfigTable[new JsEncoder.StringValue("WorkingPort")]).Value;
                    }
                    #endregion

                    String HostListFileContents        = FileIO.ReadAllText(HostListFilePath, System.Text.Encoding.Unicode);
                    JsEncoder.TableValue HostListTable = (JsEncoder.TableValue)JsEncoder.DecoderStream.DecodeValue(HostListFileContents);
                    HostList = UserHostList.FromTable(HostListTable);
                }
                catch (Exception e)
                {
                    String BaseIncompatPath  = String.Concat(DataSavePath, "_Damaged<N>");
                    String FinalIncompatPath = null;
                    int    i = 0;
                    while (FinalIncompatPath == null)
                    {
                        i++;
                        FinalIncompatPath = BaseIncompatPath.Replace("<N>", i.ToString());
                        if (DirectoryIO.Exists(FinalIncompatPath))
                        {
                            FinalIncompatPath = null;
                        }
                    }
                    DirectoryIO.Move(DataSavePath, FinalIncompatPath);

                    r = false;
                    System.Windows.Forms.MessageBox.Show(String.Concat(
                                                             "An error occured while loading your save files. They have been moved to \"",
                                                             FinalIncompatPath, "\" and you will now get the defaults. ",
                                                             "The error message is \"", e.Message, "\"."));
                }
            }
            if (!r)
            {
                UserName    = "******";
                HostList    = new UserHostList();
                WorkingPort = NetworkConfig.DefaultServerPort;
            }
            _IsLoaded = true;
            return(r);
        }