示例#1
0
        private void OnDataReceived(IAsyncResult ar)
        {
            try
            {
                bool dataReceived = false;
                int  size         = m_Socket.EndReceive(ar);
                int  ptr0         = m_PrevSize;
                m_PrevSize += size;

                do
                {
                    dataReceived = false;

                    char term = m_Server.PacketSeparator;


                    for (int i = ptr0; i < m_PrevSize; i++)
                    {
                        if (m_RecvBuffer[i] == term)
                        {
                            dataReceived = true;
                            string message = Encoding.UTF8.GetString(m_RecvBuffer, 0, i);

                            for (int j = i + 1; j < m_PrevSize; j++)
                            {
                                m_RecvBuffer[j - (i + 1)] = m_RecvBuffer[j];
                            }

                            ptr0       = 0;
                            m_PrevSize = m_PrevSize - i - 1;

                            if (DataReceived != null)
                            {
                                DataReceived(this, new Utf8TcpPeerEventArgs(this, message));
                            }

                            break;
                        }
                    }
                } while (dataReceived);

                m_Socket.BeginReceive(m_RecvBuffer, m_PrevSize, m_RecvBuffer.Length - m_PrevSize, SocketFlags.None, OnDataReceived, null);
            }
            catch (SocketException ex)
            {
                m_Server.Logger(ex.Message);
                CloseConnection(ex.Message);
            }
            catch (ObjectDisposedException ex)
            {
                m_Server.Logger(ex.Message);
                CloseConnection(ex.Message);
            }
        }
示例#2
0
        private void ExecHttpRequest(Utf8TcpPeer peer, List <string> httpdata)
        {
            try
            {
                if (Authenticator != null)
                {
                    string authstr    = httpdata.FirstOrDefault(s => s.StartsWith("Authorization:"));
                    bool   authorized = false;

                    if (authstr != null)
                    {
                        string user, password;
                        ParseAuthenticationString(authstr, out user, out password);
                        authorized = Authenticator(user, password);
                    }

                    if (!authorized)
                    {
                        SendHttp(peer, "401 Not Authorized", "text/html", ERROR_401, "WWW-Authenticate: Basic realm=\"moonsharp-remote-debugger\"");
                        return;
                    }
                }

                HttpResource res = GetResourceFromPath(httpdata[0]);

                if (res == null)
                {
                    SendHttp(peer, "404 Not Found", "text/html", ERROR_404);
                }
                else
                {
                    SendHttp(peer, "200 OK", res.GetContentTypeString(), res.Data);
                }
            }
            catch (Exception ex)
            {
                m_Server.Logger(ex.Message);

                try
                {
                    SendHttp(peer, "500 Internal Server Error", "text/html", ERROR_500);
                }
                catch (Exception ex2)
                {
                    m_Server.Logger(ex2.Message);
                }
            }
        }