Beispiel #1
0
        /// <summary>
        /// 读操作
        /// </summary>
        /// <param name="owinSocket"></param>
        /// <param name="buffer"></param>
        /// <param name="nread"></param>
        /// <param name="ex"></param>
        /// <param name="state"></param>
        private void OnReadCompleteCallback(OwinSocket owinSocket, byte[] buffer, int nread, Exception ex, object state)
        {
            if (nread < 1 || ex != null || buffer == null)
            {
                owinSocket.Dispose();
                RequestDataFactory.Recover(_requestData);
                _requestData = null;
                return;
            }
            Buffer.BlockCopy(buffer, 0, _requestData, _requestDataOffset, nread);

            _requestDataOffset += nread;
            _requestDataSize   += nread;
            if (_requestDataSize > 15)
            {
                int length = CommonUtil.GetBytesRealLength(_requestData, _requestDataSize, false);
                if (length > 0)
                {
                    AddToProcessThread(length, owinSocket);
                    return;
                }
            }
            if (_requestDataSize > 4000)
            {
                owinSocket.Dispose();
                RequestDataFactory.Recover(_requestData);
                _requestData = null;
                return;
            }
            owinSocket.Read(new Action <OwinSocket, byte[], int, Exception, object>(OnReadCompleteCallback), null);
        }
Beispiel #2
0
        private static void SendWelcomePage(OwinSocket socket, RequestData requestData)
        {
            string text = "<!DOCTYPE html>\r\n<html><head><title>OwinDog Server</title></head><body><h2 style=\"color:red;\">Hello, OwinDog Host Server!</h2><br/><span style=\"font-size:14px;\">" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + "</span></body></html>";
            string s    = string.Format("HTTP/1.1 200 OK\r\nServer: OwinDog/1.0\r\nContent-Length:{0}\r\n\r\n{1}", Encoding.ASCII.GetByteCount(text), text);

            socket.Write(Encoding.ASCII.GetBytes(s), new Action <OwinSocket, int, Exception, object>(WriteCallBack), requestData);
        }
Beispiel #3
0
        /// <summary>
        /// 调用libuv读数据后的回调方法 websocket情况下 此时的 ExcuteUserCallbackFunc 的参数 customeAsyncResult 的 UserCallbackFunc 开始解析
        /// </summary>
        /// <param name="sck"></param>
        /// <param name="data"></param>
        /// <param name="nread"></param>
        /// <param name="e"></param>
        /// <param name="state"></param>
        private void OnReadComplite(OwinSocket sck, byte[] data, int nread, Exception e, object state)
        {
            CustomeAsyncResult customeAsyncResult = (CustomeAsyncResult)state;

            if (nread < 1 || data == null || e != null)
            {
                customeAsyncResult.SocketIsErrOrClose = (true);
                customeAsyncResult.RealRecvSize       = -1;
                _socketError = true;
            }
            else
            {
                int realRecvSize = Math.Min(nread, customeAsyncResult.RecvLength);
                Buffer.BlockCopy(data, 0, customeAsyncResult.RecvBuffer, customeAsyncResult.RecvOffset, realRecvSize);
                customeAsyncResult.RealRecvSize = realRecvSize;
                if (realRecvSize < nread)
                {
                    int num3 = checked (nread - realRecvSize);
                    _requestData._preLoadedBody = new byte[num3];
                    Buffer.BlockCopy(data, realRecvSize, _requestData._preLoadedBody, 0, num3);
                }
            }
            customeAsyncResult.IsCompleted = (true);
            ((AutoResetEvent)customeAsyncResult.AsyncWaitHandle).Set();
            if (customeAsyncResult.UserCallbackFunc != null)
            {
                _simpleThreadPool.UnsafeQueueUserWorkItem(new Action <object>(ExcuteUserCallbackFunc), customeAsyncResult);
            }
        }
Beispiel #4
0
 /// <summary>
 /// 放入线程池
 /// </summary>
 /// <param name="handleSize"></param>
 /// <param name="owinSocket"></param>
 private void AddToProcessThread(int handleSize, OwinSocket owinSocket)
 {
     _simpleThreadPool.UnsafeQueueUserWorkItem(new Action <object>(ProcessRequestData), new WorkThreadState
     {
         HandleSize = handleSize,
         OwinSocket = owinSocket
     });
 }
Beispiel #5
0
        /// <summary>
        /// 写完后 如果是websocket的话 直接再调用 读 ExcuteUserCallbackFunc writehandle 接口 回掉
        /// </summary>
        /// <param name="owinSocket"></param>
        /// <param name="num"></param>
        /// <param name="ex"></param>
        /// <param name="obj"></param>
        private void OnWriteComplete(OwinSocket owinSocket, int num, Exception ex, object obj)
        {
            CustomeAsyncResult customeAsyncResult = (CustomeAsyncResult)obj;

            if (num != 0 || ex != null)
            {
                _socketError = true;
                customeAsyncResult.SocketIsErrOrClose = (true);
            }
            customeAsyncResult.IsCompleted = (true);
            ((AutoResetEvent)customeAsyncResult.AsyncWaitHandle).Set();
            if (customeAsyncResult.UserCallbackFunc != null)
            {
                _simpleThreadPool.UnsafeQueueUserWorkItem(new Action <object>(ExcuteUserCallbackFunc), customeAsyncResult);
            }
        }
Beispiel #6
0
        private static void WriteCallBack(OwinSocket socket, int num, Exception ex, object obj)
        {
            RequestData requestData = obj as RequestData;

            if (requestData == null)
            {
                socket.Dispose();
                return;
            }
            if (!requestData.IsKeepAlive() || ex != null)
            {
                requestData.SaveToPoll();
                socket.Dispose();
                return;
            }
            byte[] preLoadedBody = requestData._preLoadedBody;
            requestData.SaveToPoll();
            OwinHttpWorkerManage.Start(socket, preLoadedBody);
        }
Beispiel #7
0
 /// <summary>
 /// 执行请求的入口
 /// </summary>
 /// <param name="owinSocket"></param>
 public void Start(OwinSocket owinSocket)
 {
     if (_requestDataSize > 13)
     {
         int handleSize = CommonUtil.GetBytesRealLength(_requestData, _requestDataSize, false);
         if (handleSize > 0)
         {
             AddToProcessThread(handleSize, owinSocket);
             return;
         }
     }
     if (_requestDataSize > 4000)
     {
         owinSocket.Dispose();
         RequestDataFactory.Recover(_requestData);
         _requestData = null;
         return;
     }
     owinSocket.Read(new Action <OwinSocket, byte[], int, Exception, object>(OnReadCompleteCallback), null);
 }
Beispiel #8
0
 public static void OwinHttpProcess(OwinSocket owinSocket)
 {
     new OwinHttpWorker(null).Start(owinSocket);
 }
Beispiel #9
0
 public static void Start(OwinSocket owinSocket, byte[] array)
 {
     new OwinHttpWorker(array).Start(owinSocket);
 }
Beispiel #10
0
 /// <summary>
 /// 写操作完毕的回调事件
 /// </summary>
 /// <param name="owinSocket"></param>
 /// <param name="status"></param>
 /// <param name="ex"></param>
 /// <param name="state"></param>
 private void OnWriteCompleteToClose(OwinSocket owinSocket, int status, Exception ex, object state)
 {
     owinSocket.Dispose();
 }