⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clienthost.cs

📁 经典游戏程序设计:visual c++ 上的杀人游戏源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Net;
using KillingClientUI;
using System.Xml;


namespace KillingClientUI
{
    public class Service
    {
        public ServiceHost host = null;

        //works as a server host
        public bool LaunchServer()
        {
            try
            {
                // Returns a list of ipaddress configuration
                IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());

                // Select the first entry. I hope it's this maschines IP
                IPAddress _ipAddress = ips.AddressList[0];

                // Create the url that is needed to specify
                // where the service should be started
                string urlService = "net.tcp://" +
                    _ipAddress.ToString() + ":8002/KillPersonServerApp.MessageDistribute";

                XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
                quotas.MaxStringContentLength = 6553500;

                // Instruct the ServiceHost that the typezNBB
                // that is used is a ServiceLibrary.service1sd
                ServiceHost host = new ServiceHost(typeof(MessageDistribute));
                host.Opening += new EventHandler(host_Opening);
                host.Opened += new EventHandler(host_Opened);
                host.Closing += new EventHandler(host_Closing);
                host.Closed += new EventHandler(host_Closed);

                // The binding is where we can choose what
                // transport layer we want to use. HTTP, TCP ect.
                NetTcpBinding tcpBinding = new NetTcpBinding();
                tcpBinding.TransactionFlow = false;
                tcpBinding.Security.Transport.ProtectionLevel =
                   System.Net.Security.ProtectionLevel.EncryptAndSign;
                tcpBinding.Security.Transport.ClientCredentialType =
                   TcpClientCredentialType.Windows;
                tcpBinding.Security.Mode = SecurityMode.None;
                tcpBinding.ReaderQuotas = quotas;
                tcpBinding.SendTimeout = TimeSpan.FromMinutes(10);
                tcpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                tcpBinding.OpenTimeout = TimeSpan.FromMinutes(10);
                tcpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                tcpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
                tcpBinding.ListenBacklog = 1000;
                tcpBinding.MaxConnections = 200;
                host.CloseTimeout = TimeSpan.FromDays(1);
                host.OpenTimeout = TimeSpan.FromHours(1);
                // <- Very crucial

                // Add a endpoint
                host.AddServiceEndpoint(typeof(
                   IMessageDistribute), tcpBinding, urlService);

                // //A channel to describe the service.
                // //Used with the proxy scvutil.exe tool
                ServiceMetadataBehavior metadataBehavior;
                metadataBehavior =
                  host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (metadataBehavior == null)
                {
                    // This is how I create the proxy object
                    // that is generated via the svcutil.exe tool
                    metadataBehavior = new ServiceMetadataBehavior();
                    metadataBehavior.HttpGetUrl = new Uri("http://" +
                          _ipAddress.ToString() + ":8004/KillPersonServerApp.MessageDistribute");
                    metadataBehavior.HttpGetEnabled = true;
                    metadataBehavior.ToString();
                    host.Description.Behaviors.Add(metadataBehavior);
                    string urlMeta = metadataBehavior.HttpGetUrl.ToString();
                }

                host.Open();
                Console.WriteLine("Please don't close this window, it is the game server!");
                Console.Read();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }

            return true;

        }

        //connect to server, when server host works as a client, this will be used.
        public static MessageDistribution ConnectToServer(string strEndPointAddr)
        {
            XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
            quotas.MaxStringContentLength = 6553500;

            NetTcpBinding tcpBinding = new NetTcpBinding();
            tcpBinding.TransactionFlow = false;
            tcpBinding.Security.Transport.ProtectionLevel =
              System.Net.Security.ProtectionLevel.EncryptAndSign;
            tcpBinding.Security.Transport.ClientCredentialType =
              TcpClientCredentialType.Windows;
            tcpBinding.Security.Mode = SecurityMode.None;
            tcpBinding.ReaderQuotas = quotas;
            tcpBinding.MaxReceivedMessageSize = 6553500;
            tcpBinding.SendTimeout = TimeSpan.FromMinutes(10);
            tcpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            tcpBinding.OpenTimeout = TimeSpan.FromMinutes(10);
            tcpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            tcpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
            tcpBinding.ListenBacklog = 1000;
            tcpBinding.MaxConnections = 200;

            EndpointAddress endpointAddress =
                new EndpointAddress(strEndPointAddr);
            
            MessageDistribution proxy =
            ChannelFactory<MessageDistribution>.CreateChannel(tcpBinding, endpointAddress);
            return proxy;
        }

        void host_Opening(object sender, EventArgs e)
        {
            //TODO:
        }
        void host_Closed(object sender, EventArgs e)
        {
            //Append("Service closed");
        }

        void host_Closing(object sender, EventArgs e)
        {
            //Append("Service closing ... stand by");
        }

        void host_Opened(object sender, EventArgs e)
        {
            //Append("Service opened.");
            //Append("Service URL:\t" + urlService);
            //Append("Meta URL:\t" + urlMeta + " (Not that relevant)");
            //Append("Waiting for clients...");
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -