-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.cs
More file actions
97 lines (77 loc) · 2.53 KB
/
Server.cs
File metadata and controls
97 lines (77 loc) · 2.53 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SpigotGUI
{
class Server
{
private Process process;
private string path, arguments;
private Form1 form;
public Server(string path, string args, Form1 form)
{
this.path = path;
this.arguments = args;
this.form = form;
}
public void start()
{
char quote = '"';
ProcessStartInfo ServerStartInfo = new ProcessStartInfo(@"java.exe",
@"-Xms1024m -Xmx2048m -jar " + quote + path + quote + "");
process = new Process();
process.StartInfo = ServerStartInfo;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
// Set event handler
process.OutputDataReceived += onReceiveData;
// Start the process.
process.Start();
// Start the asynchronous read
process.BeginOutputReadLine();
}
public void stop()
{
sendCommand("stop");
}
public void sendCommand(string cmd)
{
StreamWriter writer = process.StandardInput;
writer.WriteLine(cmd);
}
delegate void SetTextCallback(string text);
private void setText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (form.consoleField.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(setText);
form.consoleField.Invoke(d, new object[] { text });
}
else
{
form.consoleField.Text = text;
}
}
private string console = "";
private void addText(string text)
{
console = console + text + "\n";
setText(console);
form.onAddConsole(text);
}
public void onReceiveData(object sender, DataReceivedEventArgs e)
{
addText(e.Data);
}
}
}