SuperSocket 1.4系列文档(17) 在Windows Azure中运行SuperSocket

Windows Azure是微软的云计算平台!Windows Azure通过微软的数据中心为开发人员提供以按需的计算能力和存储能力去托管、扩展和管理互联网上的应用程序。

运行于Windows Azure上的应用程序具有很高的可靠性和可伸缩性。

基于SuperSocket的服务器程序可以轻易的运行于Windows Azure平台之上。

和普通Socket服务器程序不同,首先需要在Role的属性中设置Socket程序对外提供服务的Endpoint:

M[90I@W~C78O1JN{4D@S@$9[4]

然后在WorkerRole代码文件中动态的获取外部Endpoint所对应的内部Endpoint:

var instanceEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[serverConfig.Name + "Endpoint"];

完整的代码如下:

public override bool OnStart()
{
    LogUtil.Setup();
    // Set the maximum number of concurrent connections 
    ServicePointManager.DefaultConnectionLimit = 100;
 
    // For information on handling configuration changes
    // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
 
    var serverConfig = ConfigurationManager.GetSection("socketServer") as SocketServiceConfig;
 
    if (!SocketServerManager.Initialize(serverConfig, ResolveServerConfig))
    {
        Trace.WriteLine("Failed to initialize SuperSocket!", "Error");
        return false;
    }
 
    if (!SocketServerManager.Start())
    {
        Trace.WriteLine("Failed to start SuperSocket!", "Error");
        return false;
    }
 
    return base.OnStart();
}
 
private IServerConfig ResolveServerConfig(IServerConfig serverConfig)
{
    var config = new ServerConfig();
    serverConfig.CopyPropertiesTo(config);
 
    var instanceEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[serverConfig.Name + "Endpoint"];
    if (instanceEndpoint == null)
    {
        Trace.WriteLine(string.Format("Failed to find Input Endpoint configuration {0}!", serverConfig.Name + "Endpoint"), "Error");
        return serverConfig;
    }
 
    var ipEndpoint = instanceEndpoint.IPEndpoint;
    config.Ip = ipEndpoint.Address.ToString();
    config.Port = ipEndpoint.Port;
    return config;
}

就这样,你的SuperSocket服务器程序就可以正确的运行在Windows Azure平台之上。

完整的示例代码,请参考源代码中WindowsAzure文件夹下的项目。

posted @ 2011-05-16 21:56  江大渔  阅读(1752)  评论(0编辑  收藏  举报