Пример #1
0
 /// <summary>
 /// 处理信息,并返回响应
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public abstract string ProcessContext(HttpListenContext context);
Пример #2
0
        /// <summary>
        /// This method is invoked when an asynchronous receive operation completes.
        /// If the remote host closed the connection, then the socket is closed.
        /// If data was received then the data is echoed back to the client.
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            try
            {
                // check if the remote host closed the connection
                var token = ((HttpListenUserToken)e.UserToken);

                Logger.Warning(string.Format("IP:{0} SocketError:{1} BufOffsize:{2} BufCount:{3} Transfer:{4}", e.RemoteEndPoint, e.SocketError, e.Offset, e.Count, e.BytesTransferred));
                if (e.SocketError == SocketError.Success)
                {
                    #region 数据接收处理

                    if (e.BytesTransferred > 0)
                    {
                        if (token.Data == null)
                        {
                            token.Data = new byte[_maxPageSize];
                        }
                        int count = Math.Min(_maxPageSize - token.DataLength, e.BytesTransferred);
                        Buffer.BlockCopy(e.Buffer, e.Offset, token.Data, token.DataLength, count);
                        token.DataLength += count;
                    }

                    #endregion

                    //if (hasFinished)
                    //{
                    #region 数据发送

                    //解析数据
                    string recContent = token.Data != null
                                            ? _encoding.GetString(token.Data, 0, token.DataLength)
                                            : string.Empty;

                    HttpListenContext context = null;
                    if (token.DataLength > 0)
                    {
                        context = new HttpListenContext(recContent);                       //把收到的文件夹解析成thhp包头,内容竺信息
                    }
                    string data;
                    if (context != null && context.IsValidContext &&
                        (context.HttpMethod == "GET" || context.HttpMethod == "POST"))
                    {
                        try
                        {
                            data = SuccessResponse(ProcessContext(context));  //ProcessContext得到要发送至设备的数据,并且发送给设备
                        }
                        catch (Exception exception)
                        {
                            data = SuccessResponse(ToJsonMessage(exception.Message));
                            Logger.Error(exception, "HttpListen Error");
                        }
                    }
                    else if (context == null)
                    {
                        data = SuccessResponse(ToJsonMessage("无效数据!"));
                        Logger.Error("HttpListenContext不能为空");
                    }
                    else
                    {
                        Logger.Error("HttpListenContext无效");
                        data = SuccessResponse(ToJsonMessage("无效数据!"));
                    }
                    var buf = _encoding.GetBytes(data);
                    token.IsReset = true;
                    _mBufferManager.FreeBuffer(e);
                    e.SetBuffer(buf, 0, buf.Length);
                    // read the next block of data send from the client
                    bool willRaiseEvent = token.ClientSocket.SendAsync(e);
                    if (!willRaiseEvent)
                    {
                        ProcessSend(e);
                    }

                    #endregion
                    //}
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Error, ex.StackTrace.ToString(), ex);
                CloseClientSocket(e);
            }
            finally
            {
                if (e.SocketError == SocketError.SocketError)
                {
                    CloseClientSocket(e);
                }
            }
        }