forked from FredericGuo/ADBConsole
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathADBParser.cs
171 lines (146 loc) · 5.09 KB
/
ADBParser.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
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 System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO; //for Streams
using System.Diagnostics; //for Process
using System.Threading; //to run commands concurrently
using System.Runtime.InteropServices;
namespace ADBConsole
{
class ADBAccess
{
Process processCmd;
Thread CMDThread;
bool m_bCMDWriting;
bool m_bWinWriting;
Queue m_CMDOutputQueue;
Queue m_CMDOutputQueueReader;
Queue m_CMDOutputQueueWriter;
Queue m_WinOutputQueueReader;
public Queue WinOutputQueueReader
{
set { m_WinOutputQueueReader = value; }
}
public string CMDOutputDequeue()
{
if (0 == m_CMDOutputQueueReader.Count)
{
throw new System.Exception("CMDOutputDequeue 0 size.");
}
return m_CMDOutputQueueReader.Dequeue().ToString();
}
public bool isCMDOutputQueueEmpty()
{
return 0 == m_CMDOutputQueueReader.Count;
}
public ADBAccess()
{
m_bCMDWriting = false;
m_bWinWriting = false;
processCmd = null;
m_CMDOutputQueue = new Queue();
m_CMDOutputQueueReader = Queue.Synchronized(m_CMDOutputQueue);
m_CMDOutputQueueWriter = Queue.Synchronized(m_CMDOutputQueue);
m_WinOutputQueueReader = null;
string errorStr;
CMDThread = new Thread(new ThreadStart(RunCMD));
}
public void StartCMD()
{
if (CMDThread.ThreadState == System.Threading.ThreadState.Unstarted)
{
CMDThread.Start();
}
}
private void RunCMD()
{
StringBuilder strInputCMD = new StringBuilder();
processCmd = new Process();
processCmd.StartInfo.FileName = "cmd.exe";
processCmd.StartInfo.CreateNoWindow = true;
processCmd.StartInfo.UseShellExecute = false;
processCmd.StartInfo.RedirectStandardOutput = true;
processCmd.StartInfo.RedirectStandardInput = true;
processCmd.StartInfo.RedirectStandardError = true;
processCmd.OutputDataReceived += new DataReceivedEventHandler(CmdOutputDataHandler);
processCmd.ErrorDataReceived += new DataReceivedEventHandler(CmdErrorDataHandler);
processCmd.Start();
processCmd.BeginOutputReadLine();
while (true)
{
try
{
if (m_bWinWriting) //don't read/write concurrently
continue;
if (0 < m_WinOutputQueueReader.Count)
{
strInputCMD.Append(m_WinOutputQueueReader.Dequeue());
strInputCMD.Append("\n");
processCmd.StandardInput.WriteLine(strInputCMD);
strInputCMD.Remove(0, strInputCMD.Length);
}
System.Threading.Thread.Sleep(400);
}
catch (Exception err)
{
Console.WriteLine("Stop CMD thread." + err.Message);
Cleanup();
break;
}
}
}
public void Exit()
{
if ( processCmd != null && !processCmd.HasExited)
{
try {
processCmd.Kill();
}
catch (Exception err)
{
Console.WriteLine("Cleanup exception: " + err.Message);
};
}
}
private void CmdOutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if(!String.IsNullOrEmpty(outLine.Data))
{
try
{
m_bCMDWriting = true;
m_CMDOutputQueueWriter.Enqueue(outLine.Data);
m_bCMDWriting = false;
}
catch (Exception err)
{
Console.WriteLine("CMD Thread Exception: " + err.Message);
}
}
}
private void CmdErrorDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
try
{
Console.WriteLine("CMD Error: " + outLine.Data);
}
catch (Exception err)
{
Console.WriteLine("CMD Error Handler Exception: " + err.Message);
}
}
}
private void Cleanup()
{
try { processCmd.Kill(); }
catch (Exception err)
{
Console.WriteLine("Cleanup exception: " + err.Message);
};
m_CMDOutputQueue.Clear();
}
}
}