-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
323 lines (271 loc) · 11.7 KB
/
Program.cs
File metadata and controls
323 lines (271 loc) · 11.7 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using Aaf.Sinc.SharpConfig;
using Aaf.Sinc.Transport;
using Aaf.Sinc.Utils;
using NetFwTypeLib;
using System;
using System.Collections.Concurrent;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Aaf.Sinc
{
internal class Program
{
private const string TITLE = "ASinc";
private const string CopyRight = "Power by Ola Chan. Details at http://chenzheng.com";
private const string PARAMS_CONFIG_FILE = "Config\\params.ini";
private static NotifyIcon notificationIcon;
/// <summary>
/// 0 - SW_HIDE - Hides the window and activates another window.
/// </summary>
private static Int32 showWindow = 1;
/// <summary>
/// 任务队列
/// </summary>
private static ConcurrentQueue<Job> Jobs = new ConcurrentQueue<Job>();
private static string sourceDir = string.Empty;
private static bool isMaster = false;
private static IPAddress selfIP = null;
/// <summary>
/// 定义作为服务器端接受信息套接字
/// </summary>
public static Socket socketReceive = null;
/// <summary>
/// 定义接受信息的IP地址和端口号
/// </summary>
public static IPEndPoint ipReceive = null;
[STAThread]
private static void Main(string[] args)
{
CopyRight.Verbose();
new Thread(
delegate ()
{
notificationIcon = new NotifyIcon()
{
Icon = new Icon(@"app.ico")
};
notificationIcon.ContextMenuStrip = new ContextMenuStrip();
notificationIcon.ContextMenuStrip.Items.AddRange(new ToolStripItem[] { new ToolStripMenuItem() });
notificationIcon.ContextMenuStrip.Items[0].Text = "Exit";
notificationIcon.ContextMenuStrip.Items[0].Click += new EventHandler(smoothExit);
notificationIcon.Visible = true;
notificationIcon.MouseClick += notificationIcon_MouseClick;
Application.Run();
}).Start();
Console.Title = TITLE;
NativeMethods.DisableCloseButton(Console.Title);
// Some biolerplate to react to close window event, CTRL-C, kill, etc
NativeMethods.handler += new NativeMethods.AppEventHandler(NativeMethods.Handler);
NativeMethods.SetConsoleCtrlHandler(NativeMethods.handler, true);
"CTRL-C to stop service.".Warn();
//SetFirewall();
Init();
Console.ReadLine();
}
/// <summary>
/// 根据配置初始化
/// </summary>
private static void Init()
{
selfIP = Protocol.LocalIP;
var cfg = Configuration.LoadFromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PARAMS_CONFIG_FILE));
sourceDir = cfg["General"]["SourceDir"].StringValue;
if (string.IsNullOrEmpty(sourceDir) || sourceDir == ".") sourceDir = AppDomain.CurrentDomain.BaseDirectory;
if (!Directory.Exists(sourceDir)) Directory.CreateDirectory(sourceDir);
isMaster = cfg["General"]["IsMaster"].BoolValue;
if (isMaster)
{
"Current node is Master.".Verbose();
var fileWatcher = new FileWatcher(sourceDir, "*.*", true);
fileWatcher.OnChanged += new FileSystemEventHandler(OnChanged);
fileWatcher.OnCreated += new FileSystemEventHandler(OnCreated);
fileWatcher.OnRenamed += new RenamedEventHandler(OnRenamed);
fileWatcher.OnDeleted += new FileSystemEventHandler(OnDeleted);
fileWatcher.Start();
Task.Factory.StartNew(() =>
{
"FileWatcher thread start.".Verbose();
RunJob();
});
}
else
{
"Current node is Salve.".Verbose();
}
Task.Factory.StartNew(() =>
{
"UDP receive thread start.".Verbose();
new UdpThread().Start();
});
Task.Factory.StartNew(() =>
{
"UDP broadcast thread start.".Verbose();
new Broadcast().Send();
});
Task.Factory.StartNew(() =>
{
"TCP receive thread start.".Verbose();
Receive();
});
}
/// <summary>
/// 处理接受到的信息
/// </summary>
private static void Receive()
{
try
{
//初始化接受套接字: 寻址方案, 以字符流方式和Tcp通信
socketReceive = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//获取本机IP地址并设置接受信息的端口
ipReceive = new IPEndPoint(selfIP, Protocol.RECEIVE_MSG_PORT);
//将本机IP地址和接受端口绑定到接受套接字
socketReceive.Bind(ipReceive);
//监听端口, 并设置监听缓存大小为1024byte
socketReceive.Listen(Protocol.SOCKET_BUFFER_SIZE);
}
catch (Exception err)
{
err.Message.Error();
}
//定义接受信息时缓冲区
var buff = new byte[Protocol.SOCKET_BUFFER_SIZE];
//连续接受客户端发送过来的信息
while (true)
{
//定义一个chat套接字用来接受信息
var chat = socketReceive.Accept();
Task.Factory.StartNew(s =>
{
//接受其他主机发送的信息
new Session((Socket)s, sourceDir).Start();
}, chat);
}
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
ConsoleExtensions.Time();
string.Format("{0} was created", e.FullPath).Info();
Send(e.FullPath, Protocol.SEND_FILE_CMD, Protocol.GetPathType(e.FullPath));
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
ConsoleExtensions.Time();
string.Format("{0} was changed", e.FullPath).Info();
Send(e.FullPath, Protocol.SEND_FILE_CMD, Protocol.GetPathType(e.FullPath));
}
private static void OnDeleted(object source, FileSystemEventArgs e)
{
ConsoleExtensions.Time();
string.Format("{0} was deleted", e.FullPath).Info();
Send(e.FullPath, Protocol.DEL_FILE_CMD, Protocol.GetPathType(e.FullPath));
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
ConsoleExtensions.Time();
string.Format("{0} was renamed to {1}", e.OldFullPath, e.FullPath).Info();
Send(e.FullPath + "," + e.OldFullPath, Protocol.REN_FILE_CMD, Protocol.GetPathType(e.OldFullPath));
}
private static void Send(string path, string cmd = Protocol.SEND_FILE_CMD,
string type = Protocol.PATH_TYPE_FILE)
{
if (IgnoreHepler.IsMatch(path))
{
string.Format("{0} was matched by ignore rules. so passed.", path).Warn();
return;
}
Jobs.Enqueue(new Job { Path = path, Cmd = cmd, PathType = type });
}
private static void RunJob()
{
var job = new Job();
while (true)
{
var ips = NodeManager.IPs.Where(x => x != selfIP.ToString()).ToList();
var count = ips.Count;
if (Jobs.TryDequeue(out job))
{
//var tasks = new Task[count];
for (var i = 0; i < count; i++)
{
Task.Factory.StartNew(ip =>
{
string.Format("send data to {0}", ip).Verbose();
//初始化接受套接字: 寻址方案, 以字符流方式和Tcp通信
var socketSent = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//设置服务器IP地址和端口
var ipSent = new IPEndPoint(IPAddress.Parse((string)ip), Protocol.RECEIVE_MSG_PORT);
////与服务器进行连接
new LanSocket(socketSent, ipSent).Connect();
//var socketConnet = new LanSocket(socketSent, ipSent);
//var tConnection = new Thread(new ThreadStart(socketConnet.Connect));
//tConnection.Start();
Thread.Sleep(90);
"Lan socket connected.".Verbose();
//发送文件
new FileDispatcher(sourceDir,
job.Path, socketSent,
job.Cmd, job.PathType).Sent();
}, ips[i]).Wait();
//Task.WaitAll(task);
}
string.Format("job [{0}] complete.", job).Verbose();
}
else
Thread.Sleep(Protocol.BROADCAST_HEARTBEAT_INTERVAL);
}
}
private static void notificationIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
//reserve right click for context menu
showWindow = ++showWindow % 2;
NativeMethods.ShowWindow(NativeMethods.GetConsoleWindow(), showWindow);
}
}
private static void smoothExit(object sender, EventArgs e)
{
notificationIcon.Visible = false;
Application.Exit();
Environment.Exit(1);
}
private static void SetFirewall()
{
Type fwRule = Type.GetTypeFromProgID("HNetCfg.FWRule");
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
foreach (INetFwRule rule in fwPolicy2.Rules)
if (rule.Name == "ASinc") return;
// create a new rule
INetFwRule2 inboundTCPRule = (INetFwRule2)Activator.CreateInstance(fwRule);
inboundTCPRule.Enabled = true;
//Allow through firewall
inboundTCPRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
//Using protocol TCP
inboundTCPRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
inboundTCPRule.LocalPorts = "9528";
inboundTCPRule.Name = "ASinc";
inboundTCPRule.Profiles = fwPolicy2.CurrentProfileTypes;
// create a new rule
INetFwRule2 inboundUDPRule = (INetFwRule2)Activator.CreateInstance(fwRule);
inboundUDPRule.Enabled = true;
//Allow through firewall
inboundUDPRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
//Using protocol UDP
inboundUDPRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; ;
inboundUDPRule.LocalPorts = "9527";
inboundUDPRule.Name = "ASinc";
inboundUDPRule.Profiles = fwPolicy2.CurrentProfileTypes;
// add the rule
fwPolicy2.Rules.Add(inboundTCPRule);
fwPolicy2.Rules.Add(inboundUDPRule);
}
}
}