Esempio n. 1
0
        private Message InternalRecieveMessage()
        {
            if(!IsAlive)
                throw new RemotingException("TCP error: Connection closed!");

            Message message = new Message();
            message.Connection = this;

            byte[] hdr = reader.ReadBytes(magic.Length);
            for(int i = 0; i < magic.Length; i++)
                if(hdr[i] != magic[i])
                    throw new RemotingException("TCP error: Invalid message header!");

            if(!IsVersionCompatible(reader.ReadByte()))
                throw new RemotingException("TCP error: Invalid protocol version not compatible!");

            switch((MessageType)reader.ReadByte())
            {
            case MessageType.Request:
                message.Type = MessageType.Request;
                break;
            case MessageType.Response:
                message.Type = MessageType.Response;
                break;
            default:
                throw new RemotingException("TCP error: Invalid message type!");
            }

            id = new Guid(reader.ReadBytes(16));
            message.ID = new Guid(reader.ReadBytes(16));

            TransportHeaders headers = new TransportHeaders();
            string key;
            while((key = reader.ReadString()).Length != 0)
                headers[key] = reader.ReadString();
            message.Headers = headers;
            message.Stream = new InputStream(this);
            return message;
        }
Esempio n. 2
0
        private Stream InternalSendMessage(Message message)
        {
            if(!IsAlive)
                throw new RemotingException("TCP error: Connection closed!");

            writer.Write(magic);
            writer.Write(Version);
            writer.Write((byte)message.Type);

            writer.Write(ThisMachineID.ToByteArray());
            writer.Write(message.ID.ToByteArray());

            foreach(DictionaryEntry entry in message.Headers)
            {
                writer.Write((string)entry.Key);
                writer.Write((string)entry.Value);
            }
            writer.Write("");
            Stream outStream = new OutputStream(this);
            if(message.Stream != null)
            {
                MemoryStream ms = message.Stream as MemoryStream;
                if(ms != null)
                    try
                    {
                        outStream.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    }
                    catch(UnauthorizedAccessException)
                    {
                        ms = null;
                    }
                if(ms == null)
                {
                    byte[] buffer = new byte[DefaultBufferSize];
                    while(true)
                    {
                        int read = message.Stream.Read(buffer, 0, buffer.Length);
                        if(read == 0)
                            break;
                        outStream.Write(buffer, 0, read);
                    }
                }
                outStream.Close();
                return null;
            }
            return outStream;
        }
Esempio n. 3
0
        public Stream SendMessage(Message message)
        {
            if(!IsAlive)
                throw new RemotingException("TCP connection closed!");

            if(message.Stream != null && message.Stream is OutputStream)
            {
                message.Stream.Close();
                return null;
            }
            lock(sendLock)
            {
                while(sendLocked)
                    Monitor.Wait(sendLock);
                sendLocked = true;
                try
                {
                    return InternalSendMessage(message);
                }
                catch(IOException e)
                {
                    Kill();
                    throw new RemotingException("TCP error!", e);
                }
            }
        }