提交 8feb5ac1 编写于 作者: T tanghai

整理了代码

上级 3ae9e288
{ "_t" : "StartConfig", "_id" : NumberLong("98547768819754"), "components" : [{ "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10002, "Host2" : "127.0.0.1" }, { "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "HttpConfig", "Url" : "http://*/", "AppId" : 0, "AppKey" : "", "ManagerSystemUrl" : "" }, { "_t" : "DBConfig", "ConnectionString" : null, "DBName" : null }], "AppId" : 1, "AppType" : "AllServer", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("98547768819754"), "components" : [{ "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10002, "Host2" : "127.0.0.1" }, { "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "HttpConfig", "Url" : "http://*:8080/", "AppId" : 0, "AppKey" : "", "ManagerSystemUrl" : "" }, { "_t" : "DBConfig", "ConnectionString" : null, "DBName" : null }], "AppId" : 1, "AppType" : "AllServer", "ServerIP" : "*" }
using ETModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Net;
using ETModel;
namespace ETHotfix
{
public class AccountInfo
{
public string name;
public string pwd;
}
public class AccountInfo
{
public string name;
public string pwd;
}
[HttpHandler(AppType.Gate, "/")]
public class HttpTest: AHttpHandler
{
[Get] // url-> /Login?name=11&age=1111
public string Login(string name, int age, HttpListenerRequest req, HttpListenerResponse resp)
{
Log.Info(name);
Log.Info($"{age}");
return "ok";
}
[HttpHandler(AppType.Gate, "/")]
public class HttpTest : AHttpHandler
{
[Get] // url-> /Login?name=11&age=1111
public string Login(string name, int age, HttpListenerRequest req, HttpListenerResponse resp)
{
Log.Info(name);
Log.Info($"{age}");
return "ok";
}
[Get("t")] // url-> /t
public int Test()
{
return 1;
}
[Get("t")] // url-> /t
public int Test()
{
return 1;
}
[Post] // url-> /Test1
public int Test1(HttpListenerRequest req)
{
return 1;
}
[Post] // url-> /Test1
public int Test1(HttpListenerRequest req)
{
return 1;
}
[Get] // url-> /Test2
public int Test2(HttpListenerResponse resp)
{
return 1;
}
[Get] // url-> /Test2
public int Test2(HttpListenerResponse resp)
{
return 1;
}
[Post]
public object Test3(HttpListenerResponse resp, HttpListenerRequest req, string postBody, AccountInfo accountInfo)
{
Log.Info(postBody);
Log.Info(JsonHelper.ToJson(accountInfo));
return new { name = "1111" };
}
}
}
\ No newline at end of file
[Post]
public object Test3(HttpListenerResponse resp, HttpListenerRequest req, string postBody, AccountInfo accountInfo)
{
Log.Info(postBody);
Log.Info(JsonHelper.ToJson(accountInfo));
return new { name = "1111" };
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.IO;
namespace ETModel
{
[ObjectSystem]
public class HttpComponentComponentAwakeSystem : AwakeSystem<HttpComponent>
{
public override void Awake(HttpComponent self)
{
self.Awake();
}
}
[ObjectSystem]
public class HttpComponentComponentLoadSystem : LoadSystem<HttpComponent>
{
public override void Load(HttpComponent self)
{
self.Load();
}
}
/// <summary>
/// http请求分发器
/// </summary>
public class HttpComponent : Component
{
public AppType appType;
public HttpListener listener;
public HttpConfig HttpConfig;
public Dictionary<string, IHttpHandler> dispatcher;
// 处理方法
private Dictionary<MethodInfo, IHttpHandler> handlersMapping;
// Get处理
private Dictionary<string, MethodInfo> getHandlers;
private Dictionary<string, MethodInfo> postHandlers;
public void Awake()
{
StartConfig startConfig = Game.Scene.GetComponent<StartConfigComponent>().StartConfig;
this.appType = startConfig.AppType;
this.HttpConfig = startConfig.GetComponent<HttpConfig>();
this.Load();
try
{
this.listener = new HttpListener();
if (this.HttpConfig.Url == null)
{
this.HttpConfig.Url = "";
}
foreach (string s in this.HttpConfig.Url.Split(';'))
{
if (s.Trim() == "")
{
continue;
}
this.listener.Prefixes.Add(s);
}
this.listener.Start();
this.Accept();
}
catch (HttpListenerException e)
{
throw new Exception($"http server error: {e.ErrorCode}", e);
}
}
public void Load()
{
this.dispatcher = new Dictionary<string, IHttpHandler>();
this.handlersMapping = new Dictionary<MethodInfo, IHttpHandler>();
this.getHandlers = new Dictionary<string, MethodInfo>();
this.postHandlers = new Dictionary<string, MethodInfo>();
Type[] types = DllHelper.GetMonoTypes();
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(HttpHandlerAttribute), false);
if (attrs.Length == 0)
{
continue;
}
HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute)attrs[0];
if (!httpHandlerAttribute.AppType.Is(this.appType))
{
continue;
}
object obj = Activator.CreateInstance(type);
IHttpHandler ihttpHandler = obj as IHttpHandler;
if (ihttpHandler == null)
{
throw new Exception($"HttpHandler handler not inherit IHttpHandler class: {obj.GetType().FullName}");
}
this.dispatcher.Add(httpHandlerAttribute.Path, ihttpHandler);
LoadMethod(type, httpHandlerAttribute, ihttpHandler);
}
}
public void LoadMethod(Type type, HttpHandlerAttribute httpHandlerAttribute, IHttpHandler httpHandler)
{
// 扫描这个类里面的方法
MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance);
foreach (var method in methodInfos)
{
object[] getAttrs = method.GetCustomAttributes(typeof(GetAttribute), false);
if (getAttrs.Length != 0)
{
GetAttribute get = (GetAttribute)getAttrs[0];
string path = method.Name;
if (!string.IsNullOrEmpty(get.Path))
path = get.Path;
getHandlers.Add(httpHandlerAttribute.Path + path, method);
Log.Debug($"add handler[{httpHandler.ToString()}.{method.Name}] path {httpHandlerAttribute.Path + path}");
}
object[] postAttrs = method.GetCustomAttributes(typeof(PostAttribute), false);
if (postAttrs.Length != 0)
{
// Post处理方法
PostAttribute post = (PostAttribute)postAttrs[0];
string path = method.Name;
if (!string.IsNullOrEmpty(post.Path))
path = post.Path;
postHandlers.Add(httpHandlerAttribute.Path + path, method);
Log.Debug($"add handler[{httpHandler.ToString()}.{method.Name}] path {httpHandlerAttribute.Path + path}");
}
if (getAttrs.Length == 0 && postAttrs.Length == 0) continue;
handlersMapping.Add(method, httpHandler);
}
}
public async void Accept()
{
while (true)
{
if (this.IsDisposed)
{
return;
}
HttpListenerContext context = await this.listener.GetContextAsync();
InvokeHandler(context);
context.Response.Close();
}
}
/// <summary>
/// 调用处理方法
/// </summary>
/// <param name="context"></param>
private void InvokeHandler(HttpListenerContext context)
{
context.Response.StatusCode = 404;
MethodInfo methodInfo = null;
IHttpHandler httpHandler = null;
string postbody = "";
if (context.Request.HttpMethod == "GET")
{
this.getHandlers.TryGetValue(context.Request.Url.AbsolutePath, out methodInfo);
if (methodInfo != null)
this.handlersMapping.TryGetValue(methodInfo, out httpHandler);
}
else if (context.Request.HttpMethod == "POST")
{
this.postHandlers.TryGetValue(context.Request.Url.AbsolutePath, out methodInfo);
if (methodInfo != null)
{
this.handlersMapping.TryGetValue(methodInfo, out httpHandler);
using (StreamReader sr = new StreamReader(context.Request.InputStream))
{
postbody = sr.ReadToEnd();
}
}
}
else
{
context.Response.StatusCode = 405;
}
if (httpHandler != null)
{
object[] args = InjectParameters(context, methodInfo, postbody);
// 自动把返回值,以json方式响应。
object resp = methodInfo?.Invoke(httpHandler, args);
if (resp != null)
{
using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
{
if (resp.GetType() == typeof(string))
{
sw.Write(resp.ToString());
}
else
{
sw.Write(JsonHelper.ToJson(resp));
}
}
}
}
}
/// <summary>
/// 注入参数
/// </summary>
/// <param name="context"></param>
/// <param name="methodInfo"></param>
/// <param name="postbody"></param>
/// <returns></returns>
private static object[] InjectParameters(HttpListenerContext context, MethodInfo methodInfo, string postbody)
{
context.Response.StatusCode = 200;
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
object[] args = new object[parameterInfos.Length];
for (int i = 0; i < parameterInfos.Length; i++)
{
ParameterInfo item = parameterInfos[i];
if (item.ParameterType == typeof(HttpListenerRequest))
{
args[i] = context.Request;
}
else if (item.ParameterType == typeof(HttpListenerResponse))
{
args[i] = context.Response;
}
else
{
try
{
if (context.Request.HttpMethod == "POST") //TODO 扩展一些,Http Entity 自动转换 的功能
{
if (item.Name == "postBody") // 约定参数名称为postBody,只传string类型。本来是byte[],有需求可以改。
{
args[i] = postbody;
}
else if (item.ParameterType.IsClass && item.ParameterType != typeof(string) && !string.IsNullOrEmpty(postbody))
{
object entity = JsonHelper.FromJson(item.ParameterType, postbody);
args[i] = entity;
}
}
else if (context.Request.HttpMethod == "GET")
{
string query = context.Request.QueryString[item.Name];
if (query != null)
{
object value = Convert.ChangeType(query, item.ParameterType);
args[i] = value;
}
}
else
{
args[i] = null;
}
}
catch (Exception e)
{
Log.Debug(e.ToString());
args[i] = null;
}
}
}
return args;
}
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
this.listener.Stop();
this.listener.Close();
}
}
[ObjectSystem]
public class HttpComponentComponentAwakeSystem: AwakeSystem<HttpComponent>
{
public override void Awake(HttpComponent self)
{
self.Awake();
}
}
[ObjectSystem]
public class HttpComponentComponentLoadSystem: LoadSystem<HttpComponent>
{
public override void Load(HttpComponent self)
{
self.Load();
}
}
/// <summary>
/// http请求分发器
/// </summary>
public class HttpComponent: Component
{
public AppType appType;
public HttpListener listener;
public HttpConfig HttpConfig;
public Dictionary<string, IHttpHandler> dispatcher;
// 处理方法
private Dictionary<MethodInfo, IHttpHandler> handlersMapping;
// Get处理
private Dictionary<string, MethodInfo> getHandlers;
private Dictionary<string, MethodInfo> postHandlers;
public void Awake()
{
StartConfig startConfig = Game.Scene.GetComponent<StartConfigComponent>().StartConfig;
this.appType = startConfig.AppType;
this.HttpConfig = startConfig.GetComponent<HttpConfig>();
this.Load();
try
{
this.listener = new HttpListener();
if (this.HttpConfig.Url == null)
{
this.HttpConfig.Url = "";
}
foreach (string s in this.HttpConfig.Url.Split(';'))
{
if (s.Trim() == "")
{
continue;
}
this.listener.Prefixes.Add(s);
}
this.listener.Start();
this.Accept();
}
catch (HttpListenerException e)
{
throw new Exception($"http server error: {e.ErrorCode}", e);
}
}
public void Load()
{
this.dispatcher = new Dictionary<string, IHttpHandler>();
this.handlersMapping = new Dictionary<MethodInfo, IHttpHandler>();
this.getHandlers = new Dictionary<string, MethodInfo>();
this.postHandlers = new Dictionary<string, MethodInfo>();
Type[] types = DllHelper.GetMonoTypes();
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(HttpHandlerAttribute), false);
if (attrs.Length == 0)
{
continue;
}
HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute) attrs[0];
if (!httpHandlerAttribute.AppType.Is(this.appType))
{
continue;
}
object obj = Activator.CreateInstance(type);
IHttpHandler ihttpHandler = obj as IHttpHandler;
if (ihttpHandler == null)
{
throw new Exception($"HttpHandler handler not inherit IHttpHandler class: {obj.GetType().FullName}");
}
this.dispatcher.Add(httpHandlerAttribute.Path, ihttpHandler);
LoadMethod(type, httpHandlerAttribute, ihttpHandler);
}
}
public void LoadMethod(Type type, HttpHandlerAttribute httpHandlerAttribute, IHttpHandler httpHandler)
{
// 扫描这个类里面的方法
MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance);
foreach (MethodInfo method in methodInfos)
{
object[] getAttrs = method.GetCustomAttributes(typeof(GetAttribute), false);
if (getAttrs.Length != 0)
{
GetAttribute get = (GetAttribute) getAttrs[0];
string path = method.Name;
if (!string.IsNullOrEmpty(get.Path))
{
path = get.Path;
}
getHandlers.Add(httpHandlerAttribute.Path + path, method);
//Log.Debug($"add handler[{httpHandler}.{method.Name}] path {httpHandlerAttribute.Path + path}");
}
object[] postAttrs = method.GetCustomAttributes(typeof(PostAttribute), false);
if (postAttrs.Length != 0)
{
// Post处理方法
PostAttribute post = (PostAttribute) postAttrs[0];
string path = method.Name;
if (!string.IsNullOrEmpty(post.Path))
{
path = post.Path;
}
postHandlers.Add(httpHandlerAttribute.Path + path, method);
//Log.Debug($"add handler[{httpHandler}.{method.Name}] path {httpHandlerAttribute.Path + path}");
}
if (getAttrs.Length == 0 && postAttrs.Length == 0)
{
continue;
}
handlersMapping.Add(method, httpHandler);
}
}
public async void Accept()
{
while (true)
{
if (this.IsDisposed)
{
return;
}
HttpListenerContext context = await this.listener.GetContextAsync();
InvokeHandler(context);
context.Response.Close();
}
}
/// <summary>
/// 调用处理方法
/// </summary>
/// <param name="context"></param>
private void InvokeHandler(HttpListenerContext context)
{
context.Response.StatusCode = 404;
MethodInfo methodInfo = null;
IHttpHandler httpHandler = null;
string postbody = "";
switch (context.Request.HttpMethod)
{
case "GET":
this.getHandlers.TryGetValue(context.Request.Url.AbsolutePath, out methodInfo);
if (methodInfo != null)
{
this.handlersMapping.TryGetValue(methodInfo, out httpHandler);
}
break;
case "POST":
this.postHandlers.TryGetValue(context.Request.Url.AbsolutePath, out methodInfo);
if (methodInfo != null)
{
this.handlersMapping.TryGetValue(methodInfo, out httpHandler);
using (StreamReader sr = new StreamReader(context.Request.InputStream))
{
postbody = sr.ReadToEnd();
}
}
break;
default:
context.Response.StatusCode = 405;
break;
}
if (httpHandler != null)
{
object[] args = InjectParameters(context, methodInfo, postbody);
// 自动把返回值,以json方式响应。
object resp = methodInfo.Invoke(httpHandler, args);
if (resp != null)
{
using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
{
if (resp is string)
{
sw.Write(resp.ToString());
}
else
{
sw.Write(JsonHelper.ToJson(resp));
}
}
}
}
}
/// <summary>
/// 注入参数
/// </summary>
/// <param name="context"></param>
/// <param name="methodInfo"></param>
/// <param name="postbody"></param>
/// <returns></returns>
private static object[] InjectParameters(HttpListenerContext context, MethodInfo methodInfo, string postbody)
{
context.Response.StatusCode = 200;
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
object[] args = new object[parameterInfos.Length];
for (int i = 0; i < parameterInfos.Length; i++)
{
ParameterInfo item = parameterInfos[i];
if (item.ParameterType == typeof(HttpListenerRequest))
{
args[i] = context.Request;
}
else if (item.ParameterType == typeof(HttpListenerResponse))
{
args[i] = context.Response;
}
else
{
try
{
if (context.Request.HttpMethod == "POST") //TODO 扩展一些,Http Entity 自动转换 的功能
{
if (item.Name == "postBody") // 约定参数名称为postBody,只传string类型。本来是byte[],有需求可以改。
{
args[i] = postbody;
}
else if (item.ParameterType.IsClass && item.ParameterType != typeof(string) && !string.IsNullOrEmpty(postbody))
{
object entity = JsonHelper.FromJson(item.ParameterType, postbody);
args[i] = entity;
}
}
else if (context.Request.HttpMethod == "GET")
{
string query = context.Request.QueryString[item.Name];
if (query != null)
{
object value = Convert.ChangeType(query, item.ParameterType);
args[i] = value;
}
}
else
{
args[i] = null;
}
}
catch (Exception e)
{
Log.Debug(e.ToString());
args[i] = null;
}
}
}
return args;
}
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
this.listener.Stop();
this.listener.Close();
}
}
}
\ No newline at end of file
......@@ -2,46 +2,46 @@
namespace ETModel
{
public class HttpHandlerAttribute : Attribute
{
public AppType AppType { get; }
public string Path { get; }
public HttpHandlerAttribute(AppType appType, string path)
{
this.AppType = appType;
this.Path = path;
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class GetAttribute : Attribute
{
public string Path { get; }
public GetAttribute()
{
}
public GetAttribute(string path)
{
this.Path = path;
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class PostAttribute : Attribute
{
public string Path { get; }
public PostAttribute()
{
}
public PostAttribute(string path)
{
this.Path = path;
}
}
public class HttpHandlerAttribute: Attribute
{
public AppType AppType { get; }
public string Path { get; }
public HttpHandlerAttribute(AppType appType, string path)
{
this.AppType = appType;
this.Path = path;
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class GetAttribute: Attribute
{
public string Path { get; }
public GetAttribute()
{
}
public GetAttribute(string path)
{
this.Path = path;
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class PostAttribute: Attribute
{
public string Path { get; }
public PostAttribute()
{
}
public PostAttribute(string path)
{
this.Path = path;
}
}
}
\ No newline at end of file
......@@ -2,15 +2,15 @@
namespace ETModel
{
public interface IHttpHandler
{
void Handle(HttpListenerContext context);
}
public interface IHttpHandler
{
void Handle(HttpListenerContext context);
}
public abstract class AHttpHandler : IHttpHandler
{
public virtual void Handle(HttpListenerContext context)
{
}
}
public abstract class AHttpHandler: IHttpHandler
{
public virtual void Handle(HttpListenerContext context)
{
}
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@
namespace ETModel
{
[BsonIgnoreExtraElements]
public class HttpConfig : AConfigComponent
public class HttpConfig: AConfigComponent
{
public string Url { get; set; } = "";
public int AppId { get; set; }
......
......@@ -88,8 +88,8 @@ public class Kcp
public static T[] slice<T>(T[] p, int start, int stop)
{
var arr = new T[stop - start];
var index = 0;
for (var i = start; i < stop; i++)
int index = 0;
for (int i = start; i < stop; i++)
{
arr[index] = p[i];
index++;
......@@ -109,7 +109,7 @@ public class Kcp
public static T[] append<T>(T[] p, T c)
{
var arr = new T[p.Length + 1];
for (var i = 0; i < p.Length; i++)
for (int i = 0; i < p.Length; i++)
arr[i] = p[i];
arr[p.Length] = c;
return arr;
......@@ -118,9 +118,9 @@ public class Kcp
public static T[] append<T>(T[] p, T[] cs)
{
var arr = new T[p.Length + cs.Length];
for (var i = 0; i < p.Length; i++)
for (int i = 0; i < p.Length; i++)
arr[i] = p[i];
for (var i = 0; i < cs.Length; i++)
for (int i = 0; i < cs.Length; i++)
arr[p.Length + i] = cs[i];
return arr;
}
......@@ -169,7 +169,7 @@ public class Kcp
// encode a segment into buffer
internal int encode(byte[] ptr, int offset)
{
var offset_ = offset;
int offset_ = offset;
offset += ikcp_encode32u(ptr, offset, conv);
offset += ikcp_encode8u(ptr, offset, (byte)cmd);
......@@ -259,7 +259,7 @@ public class Kcp
if (0 == rcv_queue.Length)
return -1;
var seq = rcv_queue[0];
Segment seq = rcv_queue[0];
if (0 == seq.frg)
return seq.data.Length;
......@@ -269,7 +269,7 @@ public class Kcp
int length = 0;
foreach (var item in rcv_queue)
foreach (Segment item in rcv_queue)
{
length += item.data.Length;
if (0 == item.frg)
......@@ -285,21 +285,21 @@ public class Kcp
if (0 == rcv_queue.Length)
return -1;
var peekSize = PeekSize();
int peekSize = PeekSize();
if (0 > peekSize)
return -2;
if (peekSize > buffer.Length)
return -3;
var fast_recover = false;
bool fast_recover = false;
if (rcv_queue.Length >= rcv_wnd)
fast_recover = true;
// merge fragment.
var count = 0;
var n = 0;
foreach (var seg in rcv_queue)
int count = 0;
int n = 0;
foreach (Segment seg in rcv_queue)
{
Array.Copy(seg.data, 0, buffer, n, seg.data.Length);
n += seg.data.Length;
......@@ -313,7 +313,7 @@ public class Kcp
// move available data from rcv_buf -> rcv_queue
count = 0;
foreach (var seg in rcv_buf)
foreach (Segment seg in rcv_buf)
if (seg.sn == this.rcv_nxt && this.rcv_queue.Length < this.rcv_wnd)
{
this.rcv_queue = append(this.rcv_queue, seg);
......@@ -346,7 +346,7 @@ public class Kcp
return -1;
}
var count = 0;
int count = 0;
if (length < mss)
count = 1;
......@@ -359,17 +359,17 @@ public class Kcp
if (0 == count)
count = 1;
var offset = 0;
int offset = 0;
for (var i = 0; i < count; i++)
for (int i = 0; i < count; i++)
{
var size = 0;
int size = 0;
if (length - offset > mss)
size = (int)mss;
else
size = length - offset;
var seg = new Segment(size);
Segment seg = new Segment(size);
Array.Copy(bytes, offset + index, seg.data, 0, size);
offset += size;
seg.frg = (UInt32)(count - i - 1);
......@@ -399,7 +399,7 @@ public class Kcp
rx_srtt = 1;
}
var rto = (int)(rx_srtt + _imax_(1, 4 * rx_rttval));
int rto = (int)(rx_srtt + _imax_(1, 4 * rx_rttval));
rx_rto = _ibound_(rx_minrto, (UInt32)rto, IKCP_RTO_MAX);
}
......@@ -416,8 +416,8 @@ public class Kcp
if (_itimediff(sn, snd_una) < 0 || _itimediff(sn, snd_nxt) >= 0)
return;
var index = 0;
foreach (var seg in snd_buf)
int index = 0;
foreach (Segment seg in snd_buf)
{
if (sn == seg.sn)
{
......@@ -432,8 +432,8 @@ public class Kcp
private void parse_una(UInt32 una)
{
var count = 0;
foreach (var seg in snd_buf)
int count = 0;
foreach (Segment seg in snd_buf)
if (_itimediff(una, seg.sn) > 0)
count++;
else
......@@ -456,16 +456,16 @@ public class Kcp
private void parse_data(Segment newseg)
{
var sn = newseg.sn;
uint sn = newseg.sn;
if (_itimediff(sn, rcv_nxt + rcv_wnd) >= 0 || _itimediff(sn, rcv_nxt) < 0)
return;
var n = rcv_buf.Length - 1;
var after_idx = -1;
var repeat = false;
for (var i = n; i >= 0; i--)
int n = rcv_buf.Length - 1;
int after_idx = -1;
bool repeat = false;
for (int i = n; i >= 0; i--)
{
var seg = rcv_buf[i];
Segment seg = rcv_buf[i];
if (seg.sn == sn)
{
repeat = true;
......@@ -487,8 +487,8 @@ public class Kcp
append(new Segment[1] { newseg }, slice(this.rcv_buf, after_idx + 1, this.rcv_buf.Length)));
// move available data from rcv_buf -> rcv_queue
var count = 0;
foreach (var seg in rcv_buf)
int count = 0;
foreach (Segment seg in rcv_buf)
if (seg.sn == this.rcv_nxt && this.rcv_queue.Length < this.rcv_wnd)
{
this.rcv_queue = append(this.rcv_queue, seg);
......@@ -507,11 +507,11 @@ public class Kcp
// when you received a low level packet (eg. UDP packet), call it
public int Input(byte[] data)
{
var s_una = snd_una;
uint s_una = snd_una;
if (data.Length < IKCP_OVERHEAD)
return 0;
var offset = 0;
int offset = 0;
while (true)
{
......@@ -575,7 +575,7 @@ public class Kcp
ack_push(sn, ts);
if (_itimediff(sn, rcv_nxt) >= 0)
{
var seg = new Segment((int)length);
Segment seg = new Segment((int)length);
seg.conv = conv_;
seg.cmd = cmd;
seg.frg = frg;
......@@ -612,7 +612,7 @@ public class Kcp
if (_itimediff(snd_una, s_una) > 0)
if (this.cwnd < this.rmt_wnd)
{
var mss_ = this.mss;
uint mss_ = this.mss;
if (this.cwnd < this.ssthresh)
{
this.cwnd++;
......@@ -646,24 +646,24 @@ public class Kcp
// flush pending data
private void flush()
{
var current_ = current;
uint current_ = current;
var buffer_ = buffer;
var change = 0;
var lost = 0;
int change = 0;
int lost = 0;
if (0 == updated)
return;
var seg = new Segment(0);
Segment seg = new Segment(0);
seg.conv = conv;
seg.cmd = IKCP_CMD_ACK;
seg.wnd = (UInt32)wnd_unused();
seg.una = rcv_nxt;
// flush acknowledges
var count = acklist.Length / 2;
var offset = 0;
for (var i = 0; i < count; i++)
int count = acklist.Length / 2;
int offset = 0;
for (int i = 0; i < count; i++)
{
if (offset + IKCP_OVERHEAD > mtu)
{
......@@ -720,17 +720,17 @@ public class Kcp
probe = 0;
// calculate window size
var cwnd_ = _imin_(snd_wnd, rmt_wnd);
uint cwnd_ = _imin_(snd_wnd, rmt_wnd);
if (0 == nocwnd)
cwnd_ = _imin_(cwnd, cwnd_);
count = 0;
for (var k = 0; k < snd_queue.Length; k++)
for (int k = 0; k < snd_queue.Length; k++)
{
if (_itimediff(snd_nxt, snd_una + cwnd_) >= 0)
break;
var newseg = snd_queue[k];
Segment newseg = snd_queue[k];
newseg.conv = conv;
newseg.cmd = IKCP_CMD_PUSH;
newseg.wnd = seg.wnd;
......@@ -750,18 +750,18 @@ public class Kcp
this.snd_queue = slice(this.snd_queue, count, this.snd_queue.Length);
// calculate resent
var resent = (UInt32)fastresend;
uint resent = (UInt32)fastresend;
if (fastresend <= 0)
resent = 0xffffffff;
var rtomin = rx_rto >> 3;
uint rtomin = rx_rto >> 3;
if (nodelay != 0)
rtomin = 0;
// flush data segments
foreach (var segment in snd_buf)
foreach (Segment segment in snd_buf)
{
var needsend = false;
var debug = _itimediff(current_, segment.resendts);
bool needsend = false;
int debug = _itimediff(current_, segment.resendts);
if (0 == segment.xmit)
{
needsend = true;
......@@ -796,7 +796,7 @@ public class Kcp
segment.wnd = seg.wnd;
segment.una = rcv_nxt;
var need = IKCP_OVERHEAD + segment.data.Length;
int need = IKCP_OVERHEAD + segment.data.Length;
if (offset + need > mtu)
{
output(buffer, offset);
......@@ -827,7 +827,7 @@ public class Kcp
// update ssthresh
if (change != 0)
{
var inflight = snd_nxt - snd_una;
uint inflight = snd_nxt - snd_una;
ssthresh = inflight / 2;
if (ssthresh < IKCP_THRESH_MIN)
ssthresh = IKCP_THRESH_MIN;
......@@ -864,7 +864,7 @@ public class Kcp
ts_flush = current;
}
var slap = _itimediff(current, ts_flush);
int slap = _itimediff(current, ts_flush);
if (slap >= 10000 || slap < -10000)
{
......@@ -893,10 +893,10 @@ public class Kcp
if (0 == updated)
return current_;
var ts_flush_ = ts_flush;
var tm_flush_ = 0x7fffffff;
var tm_packet = 0x7fffffff;
var minimal = 0;
uint ts_flush_ = ts_flush;
int tm_flush_ = 0x7fffffff;
int tm_packet = 0x7fffffff;
int minimal = 0;
if (_itimediff(current_, ts_flush_) >= 10000 || _itimediff(current_, ts_flush_) < -10000)
ts_flush_ = current_;
......@@ -906,9 +906,9 @@ public class Kcp
tm_flush_ = _itimediff(ts_flush_, current_);
foreach (var seg in snd_buf)
foreach (Segment seg in snd_buf)
{
var diff = _itimediff(seg.resendts, current_);
int diff = _itimediff(seg.resendts, current_);
if (diff <= 0)
return current_;
if (diff < tm_packet)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册