Skip to content

Commit f59f20c

Browse files
[sync] 导入0.1版本
1 parent 0c18574 commit f59f20c

8 files changed

Lines changed: 392 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vs/
2+
AMG_Cloud/bin/
3+
AMG_Cloud/obj/

AMG_Cloud.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29911.84
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AMG_Cloud", "AMG_Cloud\AMG_Cloud.csproj", "{8E44D322-AB7C-462D-899A-8B86446CEBF6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{8E44D322-AB7C-462D-899A-8B86446CEBF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8E44D322-AB7C-462D-899A-8B86446CEBF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8E44D322-AB7C-462D-899A-8B86446CEBF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8E44D322-AB7C-462D-899A-8B86446CEBF6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {18562F90-0CCD-4A96-B086-933512B1AD9B}
24+
EndGlobalSection
25+
EndGlobal

AMG_Cloud/AMG_Cloud.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
9+
<PlatformTarget>x64</PlatformTarget>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<Reference Include="NetworkSocket">
18+
<HintPath>NetworkSocket.dll</HintPath>
19+
</Reference>
20+
</ItemGroup>
21+
22+
</Project>

AMG_Cloud/NetworkSocket.dll

219 KB
Binary file not shown.

AMG_Cloud/ObjectCopier.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.Serialization;
4+
using System.Runtime.Serialization.Formatters.Binary;
5+
6+
7+
namespace AMG_Cloud
8+
{
9+
public static class ObjectCopier
10+
{
11+
public static T Clone<T>(T source)
12+
{
13+
if (!typeof(T).IsSerializable)
14+
{
15+
throw new ArgumentException("The type must be serializable.", "source");
16+
}
17+
18+
if (Object.ReferenceEquals(source, null))
19+
{
20+
return default(T);
21+
}
22+
23+
IFormatter formatter = new BinaryFormatter();
24+
Stream stream = new MemoryStream();
25+
using (stream)
26+
{
27+
formatter.Serialize(stream, source);
28+
stream.Seek(0, SeekOrigin.Begin);
29+
return (T)formatter.Deserialize(stream);
30+
}
31+
}
32+
}
33+
}

AMG_Cloud/Program.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace AMG_Cloud
6+
{
7+
public static class Globle
8+
{
9+
public static string APPName = "AMG_Cloud_Server";
10+
public static string APPVersion = "Alpha 0.1";
11+
public static int ModelNum = 1;
12+
public static int IPNum = 0;
13+
public static Dictionary<string, string> ModelToIP;
14+
public static Dictionary<string, string> IPMessage;
15+
public static Dictionary<string, string> RemoteIPMessage;
16+
17+
public static Dictionary<string, Dictionary<string, string>> ServeIPMessage;
18+
//<主机名,<处理后ip,传输信息>>
19+
public static Dictionary<string, int> ServeIPMessageTime;
20+
//主机名, 更新时间
21+
22+
}
23+
24+
class Program
25+
{
26+
27+
private static SocketManager myP2PServer;
28+
29+
static void Main(string[] args)
30+
{
31+
Console.WriteLine(Globle.APPName + " 运行版本:" + Globle.APPVersion);
32+
Globle.APPVersion = "21212";
33+
Globle.ServeIPMessage = new Dictionary<string, Dictionary<string, string>>();
34+
Globle.ServeIPMessageTime = new Dictionary<string, int>();
35+
36+
myP2PServer = new SocketManager();
37+
myP2PServer.P2PServerStart();
38+
39+
var ms = 1000 / 60;
40+
System.Timers.Timer pTimer = new System.Timers.Timer(ms);
41+
pTimer.Elapsed += pTimer_Elapsed;
42+
pTimer.AutoReset = true;
43+
pTimer.Enabled = true;
44+
45+
Console.Read();
46+
myP2PServer.P2PServerStop();
47+
}
48+
49+
private static void pTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
50+
{
51+
try
52+
{
53+
if (myP2PServer.P2PServerStatus == true)
54+
{
55+
myP2PServer.SendBinaryToClients();
56+
}
57+
}
58+
catch { }
59+
}
60+
61+
}
62+
}

AMG_Cloud/SocketManager.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using NetworkSocket.WebSocket;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading;
9+
10+
namespace AMG_Cloud
11+
{
12+
class SocketManager
13+
{
14+
private NetworkSocket.TcpListener P2Plistener;
15+
public bool P2PServerStatus = false;
16+
public void P2PServerStart()
17+
{
18+
try
19+
{
20+
P2Plistener = new NetworkSocket.TcpListener();
21+
//P2Plistener.Use<HttpMiddleware>();
22+
P2Plistener.Use<WebSocketServerMiddleware>();
23+
P2Plistener.UsePlug<CustomWSServerPlug>();
24+
P2Plistener.Start(1314);
25+
this.P2PServerStatus = true;
26+
Console.WriteLine("[WSS]本地服务器已经启动");
27+
}
28+
catch (Exception ex)
29+
{
30+
Console.WriteLine("[WSS]本地服务器发生错误 " + ex.Message + " : " + ex.StackTrace);
31+
}
32+
}
33+
34+
public void P2PServerStop()
35+
{
36+
this.P2PServerStatus = false;
37+
P2Plistener.Dispose();
38+
Console.WriteLine("[WSS]本地服务器已经关闭");
39+
}
40+
41+
public void SendBinaryToClients()
42+
{
43+
//Console.WriteLine(JsonConvert.SerializeObject(Globle.ServeIPMessage));
44+
var webSocketSessions = P2Plistener.SessionManager.FilterWrappers<WebSocketSession>();
45+
foreach (var item in webSocketSessions)
46+
{
47+
Thread thread = new Thread(new ParameterizedThreadStart(SendToClient));//创建线程
48+
49+
thread.Start(item);
50+
}
51+
/*
52+
53+
*/
54+
}
55+
56+
public void SendToClient(object ssession)
57+
{
58+
try
59+
{
60+
var item = (WebSocketSession)ssession;
61+
//var uip = ((System.Net.IPEndPoint)item.RemoteEndPoint).Address.ToString();
62+
var uuip = item.RemoteEndPoint.ToString();
63+
var rIPMessage = new Dictionary<string, string>();
64+
var aServeIPMessage = ObjectCopier.Clone(Globle.ServeIPMessage);
65+
//Console.WriteLine(JsonConvert.SerializeObject(aServeIPMessage));
66+
aServeIPMessage.Remove(uuip);
67+
//Console.WriteLine(JsonConvert.SerializeObject(aServeIPMessage));
68+
foreach (KeyValuePair<string, Dictionary<string, string>> kvp in aServeIPMessage)
69+
{
70+
foreach (KeyValuePair<string, string> kkvp in kvp.Value)
71+
{
72+
if (rIPMessage.ContainsKey(kkvp.Key))
73+
{
74+
rIPMessage[kkvp.Key] = kkvp.Value;
75+
}
76+
else
77+
{
78+
rIPMessage.Add(kkvp.Key, kkvp.Value);
79+
}
80+
}
81+
}
82+
var aa = new { keyboardAttached = new ArrayList(), ipMessage = rIPMessage };
83+
byte[] byteArray = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(aa));
84+
//Console.WriteLine(JsonConvert.SerializeObject(aa));
85+
item.SendBinary(byteArray);
86+
}
87+
catch (Exception ex)
88+
{
89+
Console.WriteLine("[WSS]发生错误:" + ex.Message + " | " + ex.StackTrace);
90+
}
91+
}
92+
93+
}
94+
}
95+
96+
/*
97+
* try
98+
{
99+
var item = (WebSocketSession)ssession;
100+
var uip = ((System.Net.IPEndPoint)item.RemoteEndPoint).Address.ToString();
101+
var uuip = item.RemoteEndPoint.ToString();
102+
var rIPMessage = new Dictionary<string, string>();
103+
var aServeIPMessage = ObjectCopier.Clone(Globle.ServeIPMessage);
104+
//var aServeIPMessage = Globle.ServeIPMessage;
105+
aServeIPMessage.Remove(uip);
106+
foreach (KeyValuePair<string, Dictionary<string, string>> kvp in aServeIPMessage)
107+
{
108+
foreach (KeyValuePair<string, string> kkvp in kvp.Value)
109+
{
110+
if (rIPMessage.ContainsKey(kkvp.Key))
111+
{
112+
rIPMessage[kkvp.Key] = kkvp.Value;
113+
}
114+
else
115+
{
116+
rIPMessage.Add(kkvp.Key, kkvp.Value);
117+
}
118+
}
119+
}
120+
var aa = new { keyboardAttached = new ArrayList(), ipMessage = rIPMessage };
121+
byte[] byteArray = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(aa));
122+
item.SendBinary(byteArray);
123+
}
124+
catch(Exception ex)
125+
{
126+
Console.WriteLine("[WSS]发生错误:" + ex.Message + " | " + ex.StackTrace);
127+
}
128+
* */
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using NetworkSocket;
2+
using NetworkSocket.Plugs;
3+
using NetworkSocket.WebSocket;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
6+
using System;
7+
using System.Collections;
8+
using System.Collections.Generic;
9+
using System.Text;
10+
11+
namespace AMG_Cloud
12+
{
13+
public class WebSocketServerMiddleware : WebSocketMiddlewareBase
14+
{
15+
protected sealed override void OnBinary(IContenxt context, FrameRequest frame)
16+
{
17+
try
18+
{
19+
var text = Encoding.UTF8.GetString(frame.Content);
20+
var jsonResult = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(text);
21+
var ipMessage = jsonResult["ipMessage"];
22+
//var uip = ((System.Net.IPEndPoint)context.Session.RemoteEndPoint).Address.ToString();
23+
var uuip = context.Session.RemoteEndPoint.ToString();
24+
var hostname = ((System.Net.IPEndPoint)context.Session.RemoteEndPoint).Address.ToString();
25+
if (jsonResult["hostName"] != null)
26+
{
27+
hostname = jsonResult["hostName"].ToString();
28+
}
29+
//预处理IP
30+
var IPMessage = new Dictionary<string, string>();
31+
foreach (JProperty jp in ipMessage)
32+
{
33+
var ip = hostname + " : " + jp.Name;
34+
var request = jp.Value.ToString();
35+
if (IPMessage.ContainsKey(ip))
36+
{
37+
IPMessage[ip] = request;
38+
}
39+
else
40+
{
41+
IPMessage.Add(ip, request);
42+
}
43+
}
44+
//Console.WriteLine(uip);
45+
//处理IP主机
46+
if (Globle.ServeIPMessage.ContainsKey(uuip))
47+
{
48+
Globle.ServeIPMessage[uuip] = IPMessage;
49+
}
50+
else
51+
{
52+
Globle.ServeIPMessage.Add(uuip, IPMessage);
53+
}
54+
55+
56+
//var session = (WebSocketSession)context.Session.Wrapper;
57+
//session.SendBinary(byteArray);
58+
//item.SendBinary(byteArray);
59+
}
60+
catch (Exception ex)
61+
{
62+
var log = "[WSS]服务端发生错误 " + ex.Message + ":" + ex.StackTrace;
63+
Console.WriteLine(log);
64+
//Globle.AddDataLog(log);
65+
}
66+
}
67+
}
68+
69+
public class CustomWSServerPlug : PlugBase
70+
{
71+
protected sealed override void OnConnected(object sender, IContenxt context)
72+
{
73+
var log = string.Format("[WSS]时间:{0} 用户:{1} 连接", DateTime.Now.ToString("mm:ss"), context.Session.ToString());
74+
//Debug.Log(log);
75+
Console.WriteLine(log);
76+
}
77+
78+
protected sealed override void OnDisconnected(object sender, IContenxt context)
79+
{
80+
var log = string.Format("[WSS]时间:{0} 用户:{1} 断开连接", DateTime.Now.ToString("mm:ss"), context.Session.RemoteEndPoint.ToString());
81+
//var uip = ((System.Net.IPEndPoint)context.Session.RemoteEndPoint).Address.ToString();
82+
var uuip = context.Session.RemoteEndPoint.ToString();
83+
Globle.ServeIPMessage.Remove(uuip);
84+
Console.WriteLine(log);
85+
//Debug.Log(log);
86+
/*var RemoteIPMessage = Globle.RemoteIPMessage;
87+
foreach (KeyValuePair<string, string> kvp in RemoteIPMessage)
88+
{
89+
if (kvp.Key.IndexOf(context.Session.RemoteEndPoint.ToString()) > 0)
90+
{
91+
Globle.RemoteIPMessage.Remove(kvp.Key);
92+
}
93+
}
94+
Globle.AddDataLog(log);
95+
Globle.globleIPChanged = true;*/
96+
//要处理
97+
}
98+
99+
protected sealed override void OnException(object sender, Exception exception)
100+
{
101+
var log = string.Format("[WSS]时间:{0} 发生错误:{1} {2}", DateTime.Now.ToString("mm:ss"), exception.Message, exception.StackTrace);
102+
//Debug.Log(log);
103+
//Globle.AddDataLog(log);
104+
//Globle.IPMessage.Remove(context.Session.RemoteEndPoint.ToString());
105+
Console.WriteLine(log);
106+
}
107+
108+
/*protected sealed override void OnRequested(object sender, IContenxt context)
109+
{
110+
111+
var request = context.StreamReader.ReadString(Encoding.ASCII).Replace("\n", "");
112+
var log = string.Format("[WSS]时间:{0} 用户:{1} 信息:{2}", DateTime.Now.ToString("mm:ss"), context.Session.RemoteEndPoint.ToString(), request);
113+
//Debug.Log(log);
114+
Console.WriteLine(log);
115+
}*/
116+
}
117+
118+
119+
}

0 commit comments

Comments
 (0)