📄 server.cs
字号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Net;
using System.Xml;
using System.Threading;
namespace ServerLib
{
public class Service
{
public static List<ServiceHost> ListHosts = new List<ServiceHost>();
public static bool nMonitor = false;
//works as a server host
public static void LaunchServer()
{
Mutex firstMutex = new Mutex(false);
try
{
firstMutex.WaitOne();
nMonitor = true;
// 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() + ":8000/KillPersonServerApp.MessageDistribute";
// 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);
XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
quotas.MaxStringContentLength = 6553500;
// 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.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;
host.CloseTimeout = TimeSpan.FromDays(1);
host.OpenTimeout = TimeSpan.FromHours(1);
//closeTimeout="00:01:00"
// openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
// <- 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() + ":8001/KillPersonServerApp.MessageDistribute");
metadataBehavior.HttpGetEnabled = true;
metadataBehavior.ToString();
host.Description.Behaviors.Add(metadataBehavior);
string urlMeta = metadataBehavior.HttpGetUrl.ToString();
}
host.Open();
ListHosts.Add(host);
// Clear Database
KillingManager.Business.NewServer();
//Console.WriteLine("Please don't close this window, it is the game server!");
int allPlayerCount = 2;
//Get the total players count
//Console.WriteLine("Please input the player count in this game:");
while (!(int.TryParse(Console.ReadLine(), out allPlayerCount))) ;
MessageDistribute.AllPlayersCount = allPlayerCount;
//Console.WriteLine("Server launches!");
//Console.Read();
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
firstMutex.Close();
nMonitor = false;
return ;
}
private static string GetValidServerConnection()
{
return null;
}
private static ServiceHost CreateNewGameHost()
{
return null;
}
//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.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 + -