This repository has been archived by the owner on Oct 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Thread.cs
142 lines (127 loc) · 4.44 KB
/
Thread.cs
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
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace DrClient
{
public partial class MainForm
{
volatile DrProtocol proc = new DrProtocol();
AddItemDelegate aid;
SetStateDelegate ssd;
UpdateIPDelegate uipd;
Thread pThread;
int trytimes = 0;
void ThreadProcess(object args)
{
this.Invoke(ssd, true, "");
try
{
proc = (DrProtocol)args;
proc.Initialize();
int ret = 0;
trytimes = 5;
while (true)
{
if (trytimes >= 0 && ret++ > trytimes)
{
Log("login", "! try over times, login fail!", false);
throw new DrException() { Source = "登录失败次数过多。" };
}
int p = proc.Challenge(ret);
if (p == -5) throw new DrException() { Source = proc.InnerSource };
if (p < 0) continue;
p = proc.Login();
if (p == -5) throw new DrException() { Source = proc.InnerSource };
if (p < 0) continue;
this.Invoke(uipd);
break;
}
while (true)
{
ret = 0;
int p;
while ((p = proc.Alive()) != 0)
{
if (p == -5) throw new DrException() { Source = proc.InnerSource };
if (trytimes >= 0 && ret++ > trytimes)
{
Log("alive", "alive(): fail;", false);
throw new DrException() { Source = "Keep-alive包发送超时多次。" };
}
Thread.Sleep(1000);
}
Thread.Sleep(20000);
}
}
catch (ThreadAbortException)
{
Log("logout", "logging out...", false);
this.Invoke(ssd, false, "");
}
catch (DrException e)
{
Log("drcom", "Socket closed. Please redail. ", false);
this.Invoke(ssd, false, e.Source);
}
// this.Invoke(ssd, false, " ");
}
private void Log(string app, object args, bool toHex)
{
if (!checkBox1.Checked) return;
string ept;
if (toHex)
{
char[] chars = "0123456789ABCDEF".ToCharArray();
byte[] bs = (byte[])args;
StringBuilder sb = new StringBuilder("");
int bit;
for (int i = 0; i < bs.Length; i++)
{
bit = (bs[i] & 0x0f0) >> 4;
sb.Append(chars[bit]);
bit = bs[i] & 0x0f;
sb.Append(chars[bit]);
}
ept = sb.ToString();
sb.Clear();
}
else
{
ept = (string)args;
}
try
{
this.Invoke(aid, string.Format("[{0}] {1}", app, ept));
}
catch (InvalidOperationException)
{
Process.GetCurrentProcess().Kill();
}
}
private delegate void AddItemDelegate(string str);
private void AddItem(string str)
{
listBox1.Items.Add(str);
}
private delegate void UpdateIPDelegate();
private void UpdateIP()
{
textBox2.Text = proc.Cert.ClientIP;
}
private delegate void SetStateDelegate(bool isLogin, string toolTip = "");
private void SetState(bool isLogin, string toolTip = "")
{
button1.Enabled = !isLogin;
button2.Enabled = isLogin;
textBox3.ReadOnly = isLogin;
textBox4.ReadOnly = isLogin;
comboBox1.Enabled = !isLogin;
checkBox2.Enabled = !isLogin;
notifyIcon1.Icon = isLogin ? Properties.Resources.Icon1 : Properties.Resources.Icon2;
if (!isLogin)
notifyIcon1.ShowBalloonTip(10000, "校园网", "已登出。" + toolTip, toolTip.Length == 0 ? ToolTipIcon.Info : ToolTipIcon.Error);
}
}
}