/// <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;
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 7
0
 /// <summary>
 ///     Send a new message to a connected socket.
 /// </summary>
 /// <param name="message">A message type Socket Buffer that contains the data to be sent.</param>
 public void Send(SocketBuffer message)
 {
     Stream sendMessage = new MemoryStream(message.Buffer);
     Send(sendMessage);
 }
Exemplo n.º 8
0
        private int Read(SocketBuffer buffer, ref int offset)
        {
            if (_lookAhead != -1)
            {
                var tmp = _lookAhead;
                _lookAhead = -1;
                return tmp;
            }

            if (offset - buffer.BaseOffset >= buffer.BytesTransferred)
                return -1;

            return buffer.Buffer[offset++];
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Will try to parse everything in the buffer
        /// </summary>
        /// <param name="buffer">Buffer to read from.</param>
        /// <param name="offset">Where to start parsing in the buffer.</param>
        /// <returns>offset where the parser ended.</returns>
        /// <remarks>
        ///     <para>
        ///         Do note that the parser is for the header only. The <see cref="Completed" /> event will
        ///         indicate that there might be body bytes left in the buffer. You have to handle them by yourself.
        ///     </para>
        /// </remarks>
        public int Parse(SocketBuffer buffer, int offset)
        {
            int theByte;
            while ((theByte = Read(buffer, ref offset)) != -1)
            {
                var ch = (char)theByte;
                _parserMethod(ch);
                if (_isCompleted)
                    break;
            }

            _isCompleted = false;
            return offset;
        }
Exemplo n.º 10
0
        /// <summary>
        ///     Send a new message to a connected socket.
        /// </summary>
        /// <param name="message">A message type Socket Buffer that contains the data to be sent.</param>
        public void Send(SocketBuffer message)
        {
            Stream sendMessage = new MemoryStream(message.Buffer);

            Send(sendMessage);
        }
Exemplo n.º 11
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();
                }
            }
        }