Skip to content

Commit 41eef1d

Browse files
committed
Smartx dotnet version 2.0.2b
1 parent efc9939 commit 41eef1d

File tree

87 files changed

+3356
-1614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+3356
-1614
lines changed

Base/Base/OneThreadSynchronizationContext.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public void Update()
3030
a();
3131
}
3232
}
33-
catch(Exception)
33+
catch(Exception e)
3434
{
35-
Log.Error("OneThreadSynchronizationContext.Update");
35+
Log.Error(e);
3636
}
3737
}
3838

@@ -44,9 +44,9 @@ public override void Post(SendOrPostCallback callback, object state)
4444
{
4545
callback(state);
4646
}
47-
catch (Exception)
47+
catch (Exception e)
4848
{
49-
Log.Error("OneThreadSynchronizationContext.Post callback");
49+
Log.Error(e);
5050
}
5151
return;
5252
}
@@ -56,9 +56,9 @@ public override void Post(SendOrPostCallback callback, object state)
5656
{
5757
callback(state);
5858
}
59-
catch (Exception)
59+
catch (Exception e)
6060
{
61-
Log.Error("OneThreadSynchronizationContext.Post Enqueue");
61+
Log.Error(e);
6262
}
6363
});
6464
}

Base/Helper/FileHelper.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ public static void CopyDirectory(string srcDir, string tgtDir)
7272
public static string GetFileData(string file)
7373
{
7474
StreamReader streamReader = File.OpenText(file);
75-
return streamReader.ReadToEnd();
76-
}
75+
var str = streamReader.ReadToEnd();
76+
streamReader.Close();
77+
streamReader.Dispose();
78+
return str;
79+
80+
}
7781

7882
}
7983
}

Base/Helper/TimeHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class TimeHelper
1111
/// <returns></returns>
1212
public static long ClientNow()
1313
{
14-
return (DateTime.Now.Ticks - epoch) / 10000;
14+
return (DateTime.UtcNow.Ticks - epoch) / 10000;
1515
}
1616

1717
public static long NowSeconds()
@@ -21,12 +21,12 @@ public static long NowSeconds()
2121

2222
public static long Now()
2323
{
24-
return (DateTime.Now.Ticks - epoch) / 10000;
24+
return (DateTime.UtcNow.Ticks - epoch) / 10000;
2525
}
2626

2727
public static long NowTicks()
2828
{
29-
return (DateTime.Now.Ticks - epoch);
29+
return (DateTime.UtcNow.Ticks - epoch);
3030
}
3131

3232
static long startTime = 0;

Base/Network/Channel/TCP/TService.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public sealed class TService : AService
1515
private Socket acceptor;
1616

1717
public RecyclableMemoryStreamManager MemoryStreamManager = new RecyclableMemoryStreamManager();
18-
19-
public HashSet<long> needStartSendChannel = new HashSet<long>();
20-
public override IPEndPoint GetEndPoint() { return (IPEndPoint)this.acceptor?.LocalEndPoint; }
18+
19+
public HashSet<long> needStartSendChannel_1 = new HashSet<long>();
20+
public HashSet<long> needStartSendChannel_2 = new HashSet<long>();
21+
public HashSet<long> needStartSendChannel = new HashSet<long>();
22+
public override IPEndPoint GetEndPoint() { return (IPEndPoint)this.acceptor?.LocalEndPoint; }
2123

2224
/// <summary>
2325
/// 即可做client也可做server
@@ -157,7 +159,13 @@ public override void Remove(long id)
157159

158160
public override void Update()
159161
{
160-
foreach (long id in this.needStartSendChannel)
162+
var sendChannel = this.needStartSendChannel;
163+
if (!this.needStartSendChannel.Equals(this.needStartSendChannel_1))
164+
this.needStartSendChannel = this.needStartSendChannel_1;
165+
else
166+
this.needStartSendChannel = this.needStartSendChannel_2;
167+
168+
foreach (long id in sendChannel)
161169
{
162170
TChannel channel;
163171
if (!this.idChannels.TryGetValue(id, out channel))
@@ -179,8 +187,8 @@ public override void Update()
179187
Log.Error(e);
180188
}
181189
}
182-
183-
this.needStartSendChannel.Clear();
190+
191+
sendChannel.Clear();
184192
}
185193
}
186194
}

Base/Network/Channel/WebSocket/HttpService.cs

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ public class HttpMessage : IMessage
1818

1919
public class HttpService : AService
2020
{
21+
public delegate byte[] Action<in T1, in T2>(T1 arg1, T2 arg2);
22+
2123
public override IPEndPoint GetEndPoint() { return NetworkHelper.ToIPEndPoint(prefix.ToLower().Replace("http://", "").Replace("/", "")); }
2224
public readonly string prefix;
2325
public readonly bool website;
26+
public static Action<byte[],HttpListenerRequest> ReplaceFunc = null;
2427

2528
public HttpService(string _prefix, bool website, Action<AChannel> acceptCallback)
2629
{
@@ -115,7 +118,7 @@ private void Result(HttpListenerContext context)
115118
response.ContentType = "text/plain;charset=UTF-8";//告诉客户端返回的ContentType类型为纯文本格式,编码为UTF-8
116119
response.AddHeader("Content-type", "text/plain");//添加响应头信息
117120
response.ContentEncoding = Encoding.UTF8;
118-
string returnObj = null;//定义返回客户端的信息
121+
byte[] returnObj = null;//定义返回客户端的信息
119122
if (request.HttpMethod == "POST" && request.InputStream != null)
120123
{
121124
//处理客户端发送的请求并返回处理信息
@@ -125,14 +128,8 @@ private void Result(HttpListenerContext context)
125128
{
126129
returnObj = OnGet(request, response);
127130
}
128-
var returnByteArr = Encoding.UTF8.GetBytes(returnObj);//设置客户端返回信息的编码
129-
using (var stream = response.OutputStream)
130-
{
131-
//把处理信息返回到客户端
132-
stream.Write(returnByteArr, 0, returnByteArr.Length);
133-
stream.Close();
134-
}
135-
response.Close();
131+
132+
OutputStream(returnObj, response);
136133
}
137134
catch (Exception )
138135
{
@@ -141,7 +138,35 @@ private void Result(HttpListenerContext context)
141138
}
142139
}
143140

144-
private string OnGet(HttpListenerRequest request, HttpListenerResponse response)
141+
// 异步回复,避免恶意卡死http服务
142+
private async void OutputStream(byte[] returnObj,HttpListenerResponse response)
143+
{
144+
try
145+
{
146+
if (returnObj != null)
147+
{
148+
//var returnByteArr = Encoding.UTF8.GetBytes(returnObj);//设置客户端返回信息的编码
149+
using (var stream = response.OutputStream)
150+
{
151+
await stream.WriteAsync(returnObj, 0, returnObj.Length).ConfigureAwait(false);
152+
stream.Close();
153+
}
154+
}
155+
}
156+
catch (Exception)
157+
{
158+
}
159+
160+
try
161+
{
162+
response.Close();
163+
}
164+
catch (Exception)
165+
{
166+
}
167+
}
168+
169+
private byte[] OnGet(HttpListenerRequest request, HttpListenerResponse response)
145170
{
146171
try
147172
{
@@ -166,7 +191,7 @@ private string OnGet(HttpListenerRequest request, HttpListenerResponse response)
166191
}
167192
}
168193

169-
return OnMessage(map, request, response);
194+
return Encoding.UTF8.GetBytes(OnMessage(map, request, response));
170195
}
171196
else
172197
// http服务
@@ -199,29 +224,33 @@ private string OnGet(HttpListenerRequest request, HttpListenerResponse response)
199224
// 安全检查
200225
var fileInfo = new FileInfo("./wwwroot" + RawUrl);
201226
if (fileInfo.DirectoryName.IndexOf(System.Environment.CurrentDirectory+"\\wwwroot")==-1)
202-
return "";
227+
return Encoding.UTF8.GetBytes("");
203228

204-
string RawUrlFileText = null;
229+
byte[] RawUrlFileText = null;
205230
if (File.Exists("./wwwroot" + RawUrl))
206-
RawUrlFileText = File.ReadAllText( "./wwwroot" + RawUrl);
231+
{
232+
RawUrlFileText = File.ReadAllBytes("./wwwroot" + RawUrl);
233+
}
207234
else
208-
if (File.Exists("./wwwroot" + RawUrl+".html"))
209-
RawUrlFileText = File.ReadAllText("./wwwroot" + RawUrl + ".html");
210-
if(RawUrl== "/js/helper.js" && RawUrlFileText != null)
235+
if (File.Exists("./wwwroot" + RawUrl + ".html"))
236+
{
237+
RawUrlFileText = File.ReadAllBytes("./wwwroot" + RawUrl + ".html");
238+
}
239+
if(RawUrl== "/js/helper.js" && RawUrlFileText != null && ReplaceFunc!=null )
211240
{
212-
RawUrlFileText = RawUrlFileText.Replace("\"http://www.SmartX.com:8004\"", $"\"http://{request.Url.Authority}\"");
241+
RawUrlFileText = ReplaceFunc(RawUrlFileText, request);
213242
}
214243
return RawUrlFileText;
215244
}
216-
return "";
245+
return Encoding.UTF8.GetBytes("");
217246
}
218247
catch (Exception ex)
219248
{
220-
return $"在接收数据时发生错误:{ex.ToString()}";
249+
return Encoding.UTF8.GetBytes($"在接收数据时发生错误:{ex.ToString()}");
221250
}
222251
}
223252

224-
private string OnPost(HttpListenerRequest request, HttpListenerResponse response)
253+
private byte[] OnPost(HttpListenerRequest request, HttpListenerResponse response)
225254
{
226255
try
227256
{
@@ -240,15 +269,15 @@ private string OnPost(HttpListenerRequest request, HttpListenerResponse response
240269
Dictionary<string, string> map = JsonHelper.FromJson<Dictionary<string, string>>(data);
241270
response.StatusDescription = "200";//获取或设置返回给客户端的 HTTP 状态代码的文本说明。
242271
response.StatusCode = 200;// 获取或设置返回给客户端的 HTTP 状态代码。
243-
return OnMessage(map, request, response);
272+
return Encoding.UTF8.GetBytes(OnMessage(map, request, response));
244273
}
245274
catch (Exception ex)
246275
{
247276
response.StatusDescription = "404";
248277
response.StatusCode = 404;
249278
//Console.ForegroundColor = ConsoleColor.Red;
250279
//Console.WriteLine($"在接收数据时发生错误:{ex.ToString()}");
251-
return $"在接收数据时发生错误:{ex.ToString()}";
280+
return Encoding.UTF8.GetBytes($"在接收数据时发生错误:{ex.ToString()}");
252281
}
253282
}
254283

@@ -261,7 +290,7 @@ private string OnMessage(Dictionary<string, string> map, HttpListenerRequest req
261290
message.request = request;
262291
message.response = response;
263292

264-
lock (Entity.Root)
293+
//lock (Entity.Root)
265294
{
266295
try
267296
{

Base/XLua/LuaDLL.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public partial class Lua
3636
#if (UNITY_IPHONE || UNITY_WEBGL || UNITY_SWITCH) && !UNITY_EDITOR
3737
const string LUADLL = "__Internal";
3838
#else
39-
const string LUADLL = "xlua";
39+
//const string LUADLL = "libxlua.so";
40+
const string LUADLL = "XLua.dll";
4041
#endif
4142

4243
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]

Bin/Base.dll

512 Bytes
Binary file not shown.

Bin/Base.pdb

12.2 KB
Binary file not shown.

Bin/Core.deps.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"compilationOptions": {},
77
"targets": {
88
".NETCoreApp,Version=v3.1": {
9-
"Core/1.5.2": {
9+
"Core/2.0.2": {
1010
"dependencies": {
1111
"Base": "1.0.0",
1212
"LevelDB.Standard": "2.1.6.1",
@@ -951,7 +951,7 @@
951951
}
952952
},
953953
"libraries": {
954-
"Core/1.5.2": {
954+
"Core/2.0.2": {
955955
"type": "project",
956956
"serviceable": false,
957957
"sha512": ""

Bin/Core.dll

30.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)