/// <summary>
        ///     Initializes a new instance of the <see cref="SntpMessageEventArgs" /> class.
        /// </summary>
        /// <param name="channel">Socket channel request is recived on.</param>
        /// <param name="data">Raw data received from socket.</param>
        public SntpMessageEventArgs(SocketChannel channel, SocketBuffer data)
        {
            if (channel == null) throw new ArgumentNullException("channel");
            this.Channel = channel;

            if (data == null) throw new ArgumentNullException("data");
            this.ChannelBuffer = data;

            try
            {
                // Parse the sntp message
                this.RequestMessage = new SntpMessage(data.Buffer);

                // log that the packet was successfully parsed
                Logger.WriteDebug(this, "PACKET with channel id " + this.Channel.ChannelId.ToString() +
                        " successfully parsed from client endpoint " + this.Channel.RemoteEndpoint.ToString());
                Logger.WriteDebug(this.RequestMessage.ToString());

            }
            catch (Exception ex)
            {
                Logger.WriteError(this, "Error parsing message:" + ex.Message.ToString(), ex);
                // disconnect ?
                return;
            }
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="DhcpMessageEventArgs"/> class.
        /// </summary>
        /// <param name="channel">Socket channel request is recived on.</param>
        /// <param name="data">Raw data received from socket.</param>
        public DhcpMessageEventArgs(SocketChannel channel, SocketBuffer data)
        {
            if (channel == null) throw new ArgumentNullException("channel");
            this.Channel = channel;

            if (data == null) throw new ArgumentNullException("data");
            this.ChannelBuffer = data;

            try
            {
                // Parse the dhcp message
                this.RequestMessage = new DhcpMessage(data.Buffer);

                // get message options
                MessageOptions options = new MessageOptions();

                options.MessageType = this.RequestMessage.GetOptionData(DhcpOption.DhcpMessageType);
                // get message type option
                options.ClientId = this.RequestMessage.GetOptionData(DhcpOption.ClientId);
                if (options.ClientId == null)
                {
                    // if the client id is not provided use the client hardware address from the message
                    options.ClientId = this.RequestMessage.ClientHardwareAddress.ToArray();
                }
                // get host name option
                options.HostName = this.RequestMessage.GetOptionData(DhcpOption.Hostname);
                // get address request option
                options.AddressRequest = this.RequestMessage.GetOptionData(DhcpOption.AddressRequest);
                //get server identifier option
                options.ServerIdentifier = this.RequestMessage.GetOptionData(DhcpOption.ServerIdentifier);
                // get paramerter list opiton
                options.ParameterList = this.RequestMessage.GetOptionData(DhcpOption.ParameterList);

                // set the response options object
                this.RequestOptions = options;

                // set the response binding object
                ResponseBinding = new BindingLease(
                    ByteUtility.GetString(this.RequestOptions.ClientId),
                    this.RequestMessage.ClientHardwareAddress,
                    this.RequestMessage.ClientAddress,
                    this.RequestOptions.HostName,
                    DateTime.MinValue,
                    this.RequestMessage.SessionId,
                    LeaseState.Unassigned);

                // log that the packet was successfully parsed
                Logger.WriteDebug(this, "PACKET with message id " +
                        this.RequestMessage.SessionId.ToHexString("0x") + " successfully parsed from client endpoint " +
                        this.Channel.RemoteEndpoint.ToString());

                Logger.WriteDebug(this.RequestMessage.ToString());
            }
            catch (Exception ex)
            {
                Logger.WriteError(this, "Error parsing message:" + ex.Message.ToString(), ex);
                return;
            }
        }
예제 #3
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ClientConnectedEventArgs" /> class.
        /// </summary>
        /// <param name="socket">The channel.</param>
        public SocketEventArgs(Socket socket)
        {
            if (socket == null) throw new ArgumentNullException("socket");

            Channel = new SocketChannel();
            ChannelBuffer = new SocketBuffer();
            Channel.Assign(socket);
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="ClientConnectedEventArgs" /> class.
        /// </summary>
        /// <param name="socket">The channel.</param>
        /// <param name="messageBuffer">The message buffer.</param>
        public ClientConnectedEventArgs(Socket socket, int messageBuffer)
        {
            if (socket == null) throw new ArgumentNullException("socket");

            AllowConnect = true;

            Channel = new SocketChannel();
            ChannelBuffer = new SocketBuffer(messageBuffer);
            Channel.Assign(socket);
        }
예제 #5
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ClientConnectedEventArgs" /> class.
        /// </summary>
        /// <param name="socket">The channel.</param>
        public SocketEventArgs(Socket socket)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            Channel       = new SocketChannel();
            ChannelBuffer = new SocketBuffer();
            Channel.Assign(socket);
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="ClientConnectedEventArgs" /> class.
        /// </summary>
        /// <param name="socket">The channel.</param>
        /// <param name="messageBuffer">The message buffer.</param>
        public ClientConnectedEventArgs(Socket socket, int messageBuffer)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            AllowConnect = true;

            Channel       = new SocketChannel();
            ChannelBuffer = new SocketBuffer(messageBuffer);
            Channel.Assign(socket);
        }
예제 #7
0
        private void SendFailure(SocketChannel channel, Exception ex)
        {
            //Logger.WriteDebug("this, Pipeline => MessageFailure");

            var pos = ex.Message.IndexOfAny(new[] { '\r', '\n' });
            var descr = pos == -1 ? ex.Message : ex.Message.Substring(0, pos);
            byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 400 OK\r\n" +
                    "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
                    "<doctype !html><html><head><title></title></head>" +
                    "<body>" + descr + "</body></html>\r\n");

            channel.Send(response);
            channel.Close();
        }
예제 #8
0
        private void SendChannel(SocketChannel channel, object message)
        {
            //Logger.WriteDebug(this, "Pipeline => SendChannel");

            try
            {
                Stream sendMessage = _encoder.Prepare(message);
                if (sendMessage.Length > 0)
                    SendCompleted(channel.Send(sendMessage));
            }
            catch (Exception ex)
            {
               SendFailure(channel, ex);
            }
        }
예제 #9
0
        private void OnMessageReceived(SocketChannel channel, HttpMessage message)
        {
            //Logger.WriteDebug(this, "Pipeline => OnMessageReceived");

            _service = this;

            var context = new HttpContext
            {
                Application = ApplicationInfo,
                Channel = channel,
                Items = new MemoryItemStorage(),
                Request = (IHttpRequest)message,
                Response = ((IHttpRequest)message).CreateResponse()

            };

            context.Request.RemoteEndPoint = channel.RemoteEndpoint;
            context.Response.AddHeader("X-Powered-By", "MicroServer");
            _moduleManager.InvokeAsync(context, SendResponse);
        }
예제 #10
0
        /// <summary>
        ///     We've received bytes from the socket. Build a message out of them.
        /// </summary>
        /// <param name="buffer">Buffer</param>
        public void ProcessReadBytes(SocketChannel channel, SocketBuffer buffer)
        {
            _channel = channel;

            var receiveBufferOffset = buffer.Offset;
            var bytesLeftInReceiveBuffer = buffer.BytesTransferred;
            while (true)
            {
                if (bytesLeftInReceiveBuffer <= 0)
                    break;

                if (!_isHeaderParsed)
                {
                    var offsetBefore = receiveBufferOffset;
                    receiveBufferOffset = _headerParser.Parse(buffer, receiveBufferOffset);
                    if (!_isHeaderParsed)
                        return;

                    bytesLeftInReceiveBuffer -= receiveBufferOffset - offsetBefore;
                    _frameContentBytesLeft = _message.ContentLength;
                    if (_frameContentBytesLeft == 0)
                    {
                        TriggerMessageReceived(_message);
                        _message = null;
                        _isHeaderParsed = false;
                        continue;
                    }

                    _message.Body = new MemoryStream();
                }

                var bytesRead = BytesProcessed(buffer.Offset, receiveBufferOffset);
                var bytesToWrite = System.Math.Min(_frameContentBytesLeft, buffer.BytesTransferred - bytesRead);
                _message.Body.Write(buffer.Buffer, receiveBufferOffset, bytesToWrite);
                _frameContentBytesLeft -= bytesToWrite;
                receiveBufferOffset += bytesToWrite;
                bytesLeftInReceiveBuffer -= bytesToWrite;
                if (_frameContentBytesLeft == 0)
                {
                    _message.Body.Position = 0;
                    TriggerMessageReceived(_message);
                    Clear();
                }
            }
        }