public bool Process(RequestData reqData) { OwinWapper owinWapper = new OwinWapper(); owinWapper.ReqData = reqData; owinWapper.OwinManager = this; if (_owinMainFunc == null) { return(false); } ISocket socket = owinWapper.ReqData.Socket; //是否是基于 http/1.1协议的 owinWapper.Keep = owinWapper.ReqData.IsKeepAlive(); owinWapper.OwinContext = new OwinContext("1.0", CancellationToken.None, owinWapper.ReqData.HeadDomainDic); owinWapper.OwinContext.Set("server.RemoteIpAddress", socket.GetRemoteIpAddress()); owinWapper.OwinContext.Set("server.RemotePort", socket.GetRemoteIpPort().ToString()); owinWapper.OwinContext.Set("server.LocalIpAddress", socket.LocalIpAddress()); owinWapper.OwinContext.Set("server.LocalPort", socket.LocalIpPort().ToString()); owinWapper.OwinContext.Set("server.IsLocal", socket.LocalIpAddress() == "127.0.0.1" || socket.GetRemoteIpAddress() == socket.LocalIpAddress()); owinWapper.OwinContext.Set("owin.CallCancelled", CancellationToken.None); owinWapper.OwinContext.Set("ssl.ClientCertificate", null); owinWapper.OwinContext.Set("server.Capabilities", Capabilities); owinWapper.OwinContext.Set("owin.RequestProtocol", owinWapper.ReqData.HttpVersion); owinWapper.OwinContext.Set("owin.RequestMethod", owinWapper.ReqData.HttpMethod); owinWapper.OwinContext.Set("owin.RequestQueryString", owinWapper.ReqData.RequestQueryString); owinWapper.OwinContext.Set("owin.RequestScheme", "http"); owinWapper.OwinContext.Set("owin.RequestPathBase", string.Empty); owinWapper.OwinContext.Set("owin.RequestPath", owinWapper.ReqData.SafeRequestUrl); OwinStream owinReqStream = new OwinStream(owinWapper.ReqData); OwinStream owinRepStream = new OwinStream(owinWapper.ReqData); int num = 0; string text = owinWapper.ReqData.HeadDomainDic.Keys.Contains("Content-Length") ? owinWapper.ReqData.HeadDomainDic["Content-Length"].FirstOrDefault <string>() : string.Empty; if (!string.IsNullOrEmpty(text)) { if (!int.TryParse(text, out num)) { num = 0; } num = ((num < 1) ? ((owinWapper.ReqData._preLoadedBody == null) ? 0 : owinWapper.ReqData._preLoadedBody.Length) : num); } OwinRequestStream owinRequestStream = new OwinRequestStream(owinReqStream, num); OwinResponseStream owinsResponseStream = new OwinResponseStream(owinWapper.OwinContext, owinRepStream, owinWapper.Keep); owinWapper.OwinContext.Set("server.OnSendingHeaders", new Action <Action <object>, object>(owinsResponseStream.AddToCallbacksList)); owinWapper.OwinContext.RequestBody = owinRequestStream; owinWapper.OwinContext.ResponseBody = owinsResponseStream; bool isWebSocket = false; if (owinWapper.ReqData.HeadDomainDic.Keys.Contains("Connection")) { //通过请求头中的Connection是不是等于Upgrade以及Upgrade是否等于WebSocket string connection = owinWapper.ReqData.HeadDomainDic["Connection"].FirstOrDefault <string>(); if (connection != null && (connection.IndexOf("Upgrade", StringComparison.OrdinalIgnoreCase) > -1 && owinWapper.ReqData.HeadDomainDic.Keys.Contains("Upgrade"))) { string upgrade = owinWapper.ReqData.HeadDomainDic["Upgrade"].FirstOrDefault <string>(); if (string.Equals(upgrade, "WebSocket", StringComparison.OrdinalIgnoreCase)) { isWebSocket = true; } } } OwinWebSocketServer owinWebSocketServer = null; if (isWebSocket) { var action = new Action(owinWapper.CallCompleteCallbackToCloseTcp); owinWebSocketServer = new OwinWebSocketServer(owinReqStream, owinRepStream, owinWapper.ReqData.HeadDomainDic, action); owinWapper.OwinContext.Set("websocket.Version", "1.0"); owinWapper.OwinContext.Set("websocket.Accept", owinWebSocketServer.Accept); } Task task = _owinMainFunc(owinWapper.OwinContext); if (task == null) { return(false); } if (owinWebSocketServer != null && owinWebSocketServer.IsCompleted()) { return(true); } if (task.IsCompleted || task.IsCanceled || task.IsFaulted) { WorkTaskComplete(task, owinWapper.ReqData, owinWapper.OwinContext, owinWapper.Keep); } else { task.ContinueWith(new Action <Task>(owinWapper.WorkTaskComplete)); } return(true); }