1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace FLY.OBJComponents.Client
{
/// <summary>
/// 管理全部 XxxxServiceClient 的连接关系
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class ObjServiceClientManager
{
NLog.ILogger logger = NLog.LogManager.GetCurrentClassLogger();
private static ObjServiceClientManager instance = null;
public static ObjServiceClientManager Instance
{
get {
if (instance == null)
{
instance = new ObjServiceClientManager();
instance.Load();
}
return instance;
}
}
[JsonProperty]
public List<ConnAddr> ConnAddrs = new List<ConnAddr>();
[JsonProperty]
public List<ObjServiceClientInfo> ClientInfos = new List<ObjServiceClientInfo>();
public UInt32 Connect_to_Another_OBJSys(string connName, string serviceName, FObj fObj)
{
//找到地址
var connaddr = ConnAddrs.Find(c => c.ConnName == connName);
if (connaddr == null)
{
//没有!!!!,失败!!!
logger.Error($"{connName}.{serviceName} 需要连接到服务器,但找不到 {connName}");
return 0;
}
//找服务Id
var clientInfo = ClientInfos.Find(c => c.ConnName == connName && c.ServiceName == serviceName);
if (clientInfo == null)
{
//没有!!!!,失败!!!
logger.Error($"{connName}.{serviceName} 需要连接到服务器,但找不到 服务Id");
return 0;
}
clientInfo.ClientId = fObj.ID;
connaddr.conn = FObjSys.Current.Connect_to_Another_OBJSys(connaddr.ep, clientInfo.ClientId);
return clientInfo.ServiceId;
}
public void ReConnect(string connName, string addr)
{
//找到地址
var connaddr = ConnAddrs.Find(c => c.ConnName == connName);
if (connaddr == null)
{
//没有!!!!,失败!!!
logger.Error($"ReConnect({connaddr},{addr}), 不能找到 ConnAddr");
return;
}
if (connaddr.conn != null)
{
var conn = connaddr.conn;
if (!conn.RemoteEP.Equals(Misc.StringConverter.ToIPEndPoint(addr)))
{
//断开之前的连接
FObjSys.Current.Disconnect_to_Another_OBJSys(conn.RemoteEP, GetAllClientIds(connaddr.ConnName));
}
}
connaddr.conn = FObjSys.Current.Connect_to_Another_OBJSys(connaddr.ep, GetAllClientIds(connaddr.ConnName));
}
UInt32[] GetAllClientIds(string connName)
{
return ClientInfos.FindAll(c => c.ConnName == connName && c.ClientId != 0).Select(c => c.ClientId).ToArray();
}
string filename = "serviceManager.json";
public void Load()
{
if (File.Exists(filename))
{
try
{
string json = File.ReadAllText(filename);
JsonConvert.PopulateObject(json, this);
if (this.ConnAddrs == null)
this.ConnAddrs = new List<ConnAddr>();
if (this.ClientInfos == null)
this.ClientInfos = new List<ObjServiceClientInfo>();
}
catch
{
}
}
}
public void Save()
{
try
{
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(filename, json);
}
catch
{
}
}
}
public class ObjServiceClientInfo
{
/// <summary>
/// 连接器名称
/// </summary>
public string ConnName;
/// <summary>
/// 服务名称, 客户端 在 unity的名称
/// </summary>
public string ServiceName;
/// <summary>
/// 服务Id
/// </summary>
public UInt32 ServiceId;
[JsonIgnore]
public UInt32 ClientId;
}
public class ConnAddr
{
/// <summary>
/// 连接器名称
/// </summary>
public string ConnName;
/// <summary>
/// 设备地址
/// </summary>
public string Addr;
public IPEndPoint ep
{
get {
return Misc.StringConverter.ToIPEndPoint(Addr);
}
}
[JsonIgnore]
public TCPCConn conn;
}
}