コード例 #1
0
        public NetIncomingMessage Receive(out IPEndPoint sender)
        {
            NetIncomingMessage m;

            while ((m = server.ReadMessage()) != null)
            {
                switch (m.MessageType)
                {
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                    Console.WriteLine(m.ReadString());
                    break;

                case NetIncomingMessageType.Data:
                    sender = m.SenderEndpoint;
                    return(compress?NetCompressor.Decompress(m):m);

                case NetIncomingMessageType.DiscoveryRequest:
                {
                    ISerializable info = Root.Instance.CurrentFlow.Query();
                    if (info != null)
                    {
                        NetOutgoingMessage m2 = server.CreateMessage();
                        m2.Write(info.ToString());
                        server.SendDiscoveryResponse(m2, m.SenderEndpoint);
                    }
                    break;
                }

                case NetIncomingMessageType.StatusChanged:
                {
                    NetConnectionStatus newStatus = (NetConnectionStatus)m.ReadByte();
                    string statusMessage          = m.ReadString();
                    Console.WriteLine(statusMessage);
                    switch (newStatus)
                    {
                    case NetConnectionStatus.Connected:
                        OnConnect(m.SenderConnection);
                        break;

                    case NetConnectionStatus.Disconnected:
                        OnDisconnect(m.SenderConnection);
                        break;
                    }
                }
                break;

                case NetIncomingMessageType.ConnectionApproval:
                {
                    int root_version = m.ReadInt32();
                    if (root_version != Root.Instance.Version)
                    {
                        Cheetah.Console.WriteLine("Client rejected: Wrong root version.");
                        m.SenderConnection.Deny("Wrong Root Version. Need " + Root.Instance.Version + ", got " + root_version);
                        break;
                    }
                    int mod_version = m.ReadInt32();
                    if (mod_version != Root.Instance.Mod.Version)
                    {
                        Cheetah.Console.WriteLine("Client rejected: Wrong mod version.");
                        m.SenderConnection.Deny("Wrong Mod Version. Need " + Root.Instance.Mod.Version + ", got " + mod_version);
                        break;
                    }
                    string password = m.ReadString();
                    if (password != this.Password && !string.IsNullOrEmpty(Password))
                    {
                        Cheetah.Console.WriteLine("Client rejected: Wrong password.");
                        m.SenderConnection.Deny("Wrong Password.");
                        break;
                    }

                    m.SenderConnection.Approve();
                }
                break;

                default:
                    Console.WriteLine("Unhandled type: " + m.MessageType);
                    break;
                }
                server.Recycle(m);
            }
            sender = null;
            return(null);
        }
コード例 #2
0
 public void Save(ISerializable data, FileMode mode = FileMode.Create)
 {
     stream = new FileStream(FileName(data.ToString()), mode);
     formatter.Serialize(stream, data);
     stream.Close();
 }