Skip to content

Commit 45a016f

Browse files
author
magiclu550
committed
[proxy] add events
1 parent ffa11a1 commit 45a016f

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

jsmod2/Program.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,22 @@ public void listenerThread()
9999
}
100100

101101
//触发事件只发id
102+
//在jsmod2的监听器执行完前不能停止,get为true,在read处阻塞
102103
public void sendEventObject(Event e,int id,IdMapping mapping)
103104
{
104-
sendObject("{}",id,mapping);
105+
sendObject("{}",id,mapping,true);
105106
}
106107

107-
public void sendObject(string json1, int id,IdMapping mapping)
108+
public void sendObject(string json1, int id,IdMapping mapping,bool get)
108109
{
109-
sendObject(new TcpClient(),json1,id,mapping);
110+
sendObject(new TcpClient(),json1,id,mapping,get);
110111
}
111112

112-
public void sendObject(TcpClient tcp,string json1, int id,IdMapping mapping)
113+
public void sendObject(TcpClient tcp,string json1, int id,IdMapping mapping,bool get)
113114
{
114115
//如何定位物品,并设置,通过itemMapping找到id归属对象(player这个字段就是id)
115116
//然后通过id定位到物品,并设置
116-
send(getProtocol(json1,id,mapping),tcp);
117+
send(getProtocol(json1,id,mapping),tcp,get);
117118
}
118119

119120
//发送协议集合,用于传输物品数组,有多个不同的id
@@ -135,7 +136,8 @@ public void sendObjects(JsonSetting[] settings)
135136
{
136137
sendObjects(new TcpClient(),settings);
137138
}
138-
private void send(string jsons,TcpClient tcp)
139+
140+
private void send(string jsons, TcpClient tcp, bool get)
139141
{
140142
try
141143
{
@@ -144,16 +146,28 @@ private void send(string jsons,TcpClient tcp)
144146
byte[] bytes = utf8WithoutBom.GetBytes(Convert.ToBase64String(utf8WithoutBom.GetBytes(jsons)));
145147
if (!tcp.Connected)
146148
{
147-
tcp.Connect(new IPEndPoint(IPAddress.Parse(reader.get("jsmod2.ip")),port));
149+
tcp.Connect(new IPEndPoint(IPAddress.Parse(reader.get("jsmod2.ip")), port));
148150
}
149-
151+
150152
tcp.GetStream().Write(bytes, 0, bytes.Length);
153+
if (get)
154+
{
155+
tcp.GetStream().ReadByte(); //停止
156+
}
151157
}
152158
catch (Exception e)
153159
{
154160
Error(e.Message);
155161
}
156-
162+
finally
163+
{
164+
tcp.Close();
165+
}
166+
}
167+
private void send(string jsons,TcpClient tcp)
168+
{
169+
170+
send(jsons, tcp, false);
157171

158172
}
159173

0 commit comments

Comments
 (0)