Exemple #1
0
        private void SendAcknowledgment(ApplicationProtocolHeader head)
        {
            _Logger.Debug("Sending header acknowledgment");

            try
            {
                WorkSocket.Send(head.WrapHeaderData());
            }
            catch (Exception)
            {
                _Logger.Error("Error sending header acknowledgment");
                CloseConnectionGracefully();
            }
        }
Exemple #2
0
        private void ReceiveMessageData()
        {
            ApplicationProtocolHeader header = new ApplicationProtocolHeader(_ProtocolPrefixBuffer);

            //Reset bytes read, as we will now start to receive the rest of the data
            _BytesRead = 0;

            //Set buffer to appropriate size
            PrepareBuffer(header.Lenght);

            _Logger.Debug("Starts reading message data");

            try
            {
                while (this._BytesRead < _ReceiveBuffer.Length)
                {
                    _BytesRead += this.WorkSocket.Receive(this._ReceiveBuffer, _BytesRead, _ReceiveBuffer.Length - _BytesRead, SocketFlags.None);
                }
            }
            catch (Exception e)
            {
                _Logger.Debug("Error receiving message data");
                _Logger.Error(e.Message);

                CloseConnectionGracefully();
                return;
            }

            MessageObject dataobj = new MessageObject(this.WorkSocket, header, _ReceiveBuffer);

            //Pass dataobject to whoever listens for it
            //TODO: This may throw if no handler for that message is present


            try
            {
                NotifyCompleteDataReceived(dataobj);
            }
            catch (KeyNotFoundException)
            {
                _Logger.Error("No handler for specified messagetype. Shutting down connection");

                CloseConnectionGracefully();
                return;
            }

            PrepareToReadNextHeader();
        }
 public MessageObject(Socket sock, ApplicationProtocolHeader head, byte[] data)
 {
     UsedConnection = sock;
     MessageHeader  = head;
     CompleteData   = data;
 }