You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

193 lines
5.2 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using ProtoCSStruct;
using World;
namespace Sog
{
public enum LinkType
{
LinkType_Normal = 0,
LinkType_TimeOut = 1,
LinkType_Die = 2,
LinkType_NotExist = 3,
}
public class ServerLinkInfo
{
public int linkCount;
public LinkType linkType = LinkType.LinkType_Normal; //默认有效
}
//canRemove = true 的话直接移除
//其他情况下:1:isConnect = true 情况下,判断个1分钟超时后将canRemove设为true
//2:isConnect = false的情况下,根据connectTime时间,接收时间2-3分钟后发给game,通知game干掉playersession
public class GameConnectInfo
{
public long playerSessionID;
public uint gateServerID;
public string IPAddr = String.Empty;
public long connectTime;
public bool canRemove = false;
public long userID; //玩家uid
}
[Serializable]
public class PlayerLinkServerData : Singleton<PlayerLinkServerData>
{
public Dictionary<uint, ServerLinkInfo> serverLinkinfoDict;
//玩家连接信息,由game转发过来
public Dictionary<long, GameConnectInfo> connectInfo;
public PlayerLinkServerData()
{
serverLinkinfoDict = new Dictionary<uint, ServerLinkInfo>();
connectInfo = new Dictionary<long, GameConnectInfo>();
}
public void AddConnectInfo(ref SSGameConnectNotify notify, long connectTime)
{
if (connectInfo.ContainsKey(notify.PlayerSessionID))
{
return;
}
GameConnectInfo info = new GameConnectInfo();
info.playerSessionID = notify.PlayerSessionID;
info.gateServerID = notify.GateServerID;
info.IPAddr = notify.IPAddr.ToString();
info.connectTime = connectTime;
connectInfo.Add(notify.PlayerSessionID, info);
}
public GameConnectInfo GetGameConnectInfo(long playerSessionID)
{
if (connectInfo.ContainsKey(playerSessionID))
{
return connectInfo[playerSessionID];
}
return null;
}
public void RemoveGameConnectInfo(long playerSessionID)
{
if (connectInfo.ContainsKey(playerSessionID))
{
connectInfo.Remove(playerSessionID);
}
}
//有连接world的selrve就初始化下
public void InitLinkServer(uint serverID)
{
if (serverLinkinfoDict.ContainsKey(serverID) == false)
{
ServerLinkInfo linkInfo = new ServerLinkInfo();
serverLinkinfoDict.Add(serverID, linkInfo);
}
}
//server是否是有效状态
public bool ServerIsValid(uint serverID)
{
if (serverID == 0)
{
return false;
}
if (serverLinkinfoDict.ContainsKey(serverID))
{
return serverLinkinfoDict[serverID].linkType == LinkType.LinkType_Normal;
}
return false;
}
//获取linktype
public LinkType GetServerLinkType(uint serverID)
{
if (serverLinkinfoDict.ContainsKey(serverID))
{
return serverLinkinfoDict[serverID].linkType;
}
return LinkType.LinkType_NotExist;
}
//获取server已经连接的数目(包含还在缓存中的玩家)
public int GetServerIDLinkCount(uint serverID)
{
if (serverLinkinfoDict.ContainsKey(serverID))
{
return serverLinkinfoDict[serverID].linkCount;
}
return 0;
}
public void ResetServerIDLinkCount(uint serverID)
{
if (serverLinkinfoDict.ContainsKey(serverID))
{
serverLinkinfoDict[serverID].linkCount = 0;
}
}
//增加连接数
public void ChangeServerIDLinkCount(uint serverID, int Addcount = 1)
{
if (serverLinkinfoDict.ContainsKey(serverID) == false)
{
ServerLinkInfo linkInfo = new ServerLinkInfo();
serverLinkinfoDict.Add(serverID, linkInfo);
}
serverLinkinfoDict[serverID].linkCount += Addcount;
serverLinkinfoDict[serverID].linkCount = Math.Max(serverLinkinfoDict[serverID].linkCount, 0);
}
//设置数量
public void SetServerIDLinkCount(uint serverID, int count)
{
if (serverLinkinfoDict.ContainsKey(serverID) == false)
{
ServerLinkInfo linkInfo = new ServerLinkInfo();
serverLinkinfoDict.Add(serverID, linkInfo);
}
serverLinkinfoDict[serverID].linkCount = count;
}
//改变连接状态
public void ChangeServerIDLinkType(uint serverID, LinkType type)
{
if (serverLinkinfoDict.ContainsKey(serverID) == false)
{
ServerLinkInfo linkInfo = new ServerLinkInfo();
serverLinkinfoDict.Add(serverID, linkInfo);
}
serverLinkinfoDict[serverID].linkType = type;
}
//某个服务器已经挂掉了
public void ServerIdIsDieNotRunning(uint serverID)
{
if (serverLinkinfoDict.ContainsKey(serverID) == false)
{
TraceLog.Error("PlayerLinkServerData.ServerIdIsDieNotRunning serverID {0} is not valid", serverID);
return;
}
//不是登录状态???,要么通知错误了,如果服务器挂了,肯定是已经断开的状态
if (serverLinkinfoDict[serverID].linkType != LinkType.LinkType_TimeOut)
{
TraceLog.Error("PlayerLinkServerData.ServerIdIsDieNotRunning serverID {0} is not valid", serverID);
return;
}
serverLinkinfoDict[serverID].linkType = LinkType.LinkType_Die;
}
}
}