Esempio n. 1
0
        public void StartSend(byte[] sendBuffer, int start, int len, Action whenSendCompleted)
        {
            //must be in opened state
            if (_workingState != WorkingState.Rest)
            {
                throw new Exception("sending error: state is not= opened");
            }
            //--------------------------------------------------------------
#if DEBUG
            if (this.whenSendCompleted != null)
            {
                //must be null
                throw new Exception("sending something?...");
            }
#endif
            this.whenSendCompleted = whenSendCompleted;
            this._workingState     = WorkingState.Sending;
            sendIO.EnqueueOutputData(sendBuffer, len);
            sendIO.StartSendAsync();
        }
Esempio n. 2
0
 public void Write(string content)
 {
     byte[] dataToSend = CreateSendBuffer(content);
     sendIO.EnqueueOutputData(dataToSend, dataToSend.Length);
     sendIO.StartSendAsync();
 }
Esempio n. 3
0
 public void EnqueueSendingData(byte[] dataToSend, int count) => _sendIO.EnqueueOutputData(dataToSend, count);
        public void End()
        {
            switch (writeContentState)
            {
            //generate head
            case WriteContentState.HttpHead:
            {
                headerStBuilder.Length = 0;
                headerStBuilder.Append("HTTP/1.1 ");
                HeaderAppendStatusCode(headerStBuilder, StatusCode);
                HeaderAppendConnectionType(headerStBuilder, this.context.KeepAlive);
                //--------------------------------------------------------------------------------------------------------
                headerStBuilder.Append("Content-Type: " + GetContentType(this.ContentType));
                switch (ContentTypeCharSet)
                {
                case TextCharSet.Utf8:
                    headerStBuilder.Append(" ; charset=utf-8\r\n");
                    break;

                case TextCharSet.Ascii:
                    headerStBuilder.Append("\r\n");
                    break;

                default:
                    throw new NotSupportedException();
                }
                //--------------------------------------------------------------------------------------------------------
                switch (ContentEncoding)
                {
                case WebServers.ContentEncoding.Plain:
                    //nothing
                    break;

                case WebServers.ContentEncoding.Gzip:
                    headerStBuilder.Append("Content-Encoding: gzip\r\n");
                    break;

                default:
                    throw new NotSupportedException();
                }
                //--------------------------------------------------------------------------------------------------------
                //Access-Control-Allow-Origin
                if (AllowCrossOriginPolicy != null)
                {
                    AllowCrossOriginPolicy.WriteHeader(headerStBuilder);
                }
                //--------------------------------------------------------------------------------------------------------
                switch (this.TransferEncoding)
                {
                default:
                case ResponseTransferEncoding.Identity:
                {
                    headerStBuilder.Append("Content-Length: ");
                    headerStBuilder.Append(contentByteCount);
                    headerStBuilder.Append("\r\n");
                    //-----------------------------------------------------------------
                    headerStBuilder.Append("\r\n");                //end header part
                    writeContentState = WriteContentState.HttpBody;
                    //-----------------------------------------------------------------
                    //switch transfer encoding method of the body***
                    var    headBuffer = Encoding.UTF8.GetBytes(headerStBuilder.ToString().ToCharArray());
                    byte[] dataToSend = new byte[headBuffer.Length + contentByteCount];
                    Buffer.BlockCopy(headBuffer, 0, dataToSend, 0, headBuffer.Length);
                    var pos = bodyMs.Position;
                    bodyMs.Position = 0;
                    bodyMs.Read(dataToSend, headBuffer.Length, contentByteCount);
                    //----------------------------------------------------
                    //copy data to send buffer
                    sendIO.EnqueueOutputData(dataToSend, dataToSend.Length);

                    //----------------------------------------------------
                    ResetAll();
                }
                break;

                case ResponseTransferEncoding.Chunked:
                {
                    headerStBuilder.Append("Transfer-Encoding: " + GetTransferEncoding(TransferEncoding) + "\r\n");
                    headerStBuilder.Append("\r\n");
                    writeContentState = WriteContentState.HttpBody;

                    //chunked transfer
                    var headBuffer = Encoding.UTF8.GetBytes(headerStBuilder.ToString().ToCharArray());
                    sendIO.EnqueueOutputData(headBuffer, headBuffer.Length);
                    WriteContentBodyInChunkMode();
                    ResetAll();
                }
                break;
                }
            }
            break;

            //==============================
            case WriteContentState.HttpBody:
            {
                //in chunked case,
                WriteContentBodyInChunkMode();
                ResetAll();
            }
            break;

            default:
            {
                throw new NotSupportedException();
            }
            }

            //-----------------------
            //send

            StartSend();
        }
Esempio n. 5
0
 internal void SendExternalRaw(byte[] data)
 {
     sendIO.EnqueueOutputData(data, data.Length);
     sendIO.StartSendAsync();
 }
Esempio n. 6
0
 internal override void EnqueueSendData(byte[] buffer, int len)
 {
     _sendIO.EnqueueOutputData(buffer, len);
 }