Ejemplo n.º 1
0
        protected override void OnStart()
        {
            base.OnStart();

            TcpServiceAddress tcpAddress = (TcpServiceAddress)Address;
            IPEndPoint        endPoint   = tcpAddress.ToEndPoint();

            listener = new TcpListener(endPoint);
            listener.Server.ReceiveTimeout = 0;

            try {
                // listener.Bind(endPoint);
                int curReceiveBufSize = listener.Server.ReceiveBufferSize;
                if (curReceiveBufSize < 256 * 1024)
                {
                    listener.Server.ReceiveBufferSize = 256 * 1024;
                }
                listener.Start(150);
            } catch (SocketException e) {
                Logger.Error("Socket Error while starting the TCP Admin service.", e);
                throw;
            }

            polling = true;

            Thread thread = new Thread(Poll);

            thread.IsBackground = true;
            thread.Start();
        }
Ejemplo n.º 2
0
        public byte[] ToBytes(IServiceAddress address)
        {
            TcpServiceAddress tcpAddress = (TcpServiceAddress)address;

            int length = tcpAddress.IsIPv4 ? 4 : 16;

            byte[] buffer = new byte[length + 2 + 4];
            Util.ByteBuffer.WriteInt2((short)length, buffer, 0);
            Array.Copy(tcpAddress.Address, 0, buffer, 2, length);
            Util.ByteBuffer.WriteInt4(tcpAddress.Port, buffer, length + 2);
            return(buffer);
        }
Ejemplo n.º 3
0
        public int CompareTo(TcpServiceAddress other)
        {
            if (family != other.family)
            {
                throw new ArgumentException("The given address is not of the same family of this address.");
            }

            for (int i = 0; i < address.Length; ++i)
            {
                byte dbi = other.address[i];
                byte sbi = address[i];
                if (other.address[i] != address[i])
                {
                    return(sbi - dbi);
                }
            }
            return(port - other.port);
        }
Ejemplo n.º 4
0
        public override bool Equals(object obj)
        {
            TcpServiceAddress dest_ob = (TcpServiceAddress)obj;

            if (port != dest_ob.port)
            {
                return(false);
            }
            if (family != dest_ob.family)
            {
                return(false);
            }

            for (int i = 0; i < address.Length; ++i)
            {
                if (dest_ob.address[i] != address[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 5
0
        private TcpConnection GetConnection(TcpServiceAddress address)
        {
            TcpConnection c;

            lock (connectionPool) {
                // If there isn't, establish a connection,
                if (!connectionPool.TryGetValue(address, out c))
                {
                    Socket socket = new Socket(address.IsIPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6,
                                               SocketType.Stream, ProtocolType.IP);
                    socket.Connect(address.ToIPAddress(), address.Port);
                    socket.ReceiveTimeout = (30 * 1000);                   // 30 second timeout,
                    socket.NoDelay        = true;
                    int curSendBufSize = socket.SendBufferSize;
                    if (curSendBufSize < 256 * 1024)
                    {
                        socket.SendBufferSize = 256 * 1024;
                    }

                    int curReceiveBufSize = socket.ReceiveBufferSize;
                    if (curReceiveBufSize < 256 * 1024)
                    {
                        socket.ReceiveBufferSize = 256 * 1024;
                    }

                    c = new TcpConnection(this, socket);
                    c.Connect();
                    connectionPool[address] = c;
                }
                else
                {
                    c.AddLock();
                }
            }
            return(c);
        }
Ejemplo n.º 6
0
 public MessageProcessor(TcpServiceConnector connector, TcpServiceAddress address, ServiceType serviceType)
 {
     this.connector   = connector;
     this.address     = address;
     this.serviceType = serviceType;
 }
Ejemplo n.º 7
0
 private void InvalidateConnection(TcpServiceAddress address)
 {
     lock (connectionPool) {
         connectionPool.Remove(address);
     }
 }
Ejemplo n.º 8
0
 public IServiceAddress FromString(string s)
 {
     return(TcpServiceAddress.Parse(s));
 }
Ejemplo n.º 9
0
 public TcpAdminService(IServiceFactory serviceFactory, TcpServiceAddress address, string password)
     : base(address, new TcpServiceConnector(password), serviceFactory)
 {
 }
Ejemplo n.º 10
0
 public IMessageProcessor Connect(TcpServiceAddress address, ServiceType type)
 {
     return(new MessageProcessor(this, address, type));
 }
Ejemplo n.º 11
0
 public TcpProxyServiceConnector(TcpServiceAddress proxyAddress, string password)
 {
     this.proxyAddress = proxyAddress;
     this.password     = password;
 }
Ejemplo n.º 12
0
            public void Process(object state)
            {
                Socket socket = (Socket)state;

                Stream socket_in  = new NetworkStream(socket, FileAccess.Read);
                Stream socket_out = new NetworkStream(socket, FileAccess.Write);

                try {
                    // 30 minute timeout on proxy connections,
                    socket.SendTimeout = 30 * 60 * 1000;

                    // Wrap the input stream in a data and buffered input stream,
                    BufferedStream bin = new BufferedStream(socket_in, 1024);
                    BinaryReader   din = new BinaryReader(bin);

                    // Wrap the output stream in a data and buffered output stream,
                    BufferedStream bout = new BufferedStream(socket_out, 1024);
                    BinaryWriter   dout = new BinaryWriter(bout);

                    // Perform the handshake,
                    DateTime systemtime = DateTime.Now;
                    dout.Write(systemtime.ToUniversalTime().ToBinary());
                    dout.Flush();
                    long back = din.ReadInt64();
                    if (systemtime.ToUniversalTime().ToBinary() != back)
                    {
                        throw new IOException("Bad protocol request");
                    }
                    dout.Write("CloudB Proxy Service");
                    dout.Flush();
                    string net_password = din.ReadString();

                    // The connector to proxy commands via,
                    TcpServiceConnector connector = new TcpServiceConnector(net_password);

                    // The rest of the communication will be command requests;
                    while (true)
                    {
                        // Read the command,
                        char command = din.ReadChar();
                        if (command == '0')
                        {
                            // Close connection if we receive a '0' command char
                            dout.Close();
                            din.Close();
                            return;
                        }

                        int  addressCode = din.ReadInt32();
                        Type addressType = ServiceAddresses.GetAddressType(addressCode);
                        if (addressType == null || addressType != typeof(TcpServiceAddress))
                        {
                            throw new ApplicationException("Invalid address type.");
                        }

                        int    addressLength = din.ReadInt32();
                        byte[] addressBytes  = new byte[addressLength];
                        din.Read(addressBytes, 0, addressLength);

                        IServiceAddressHandler handler = ServiceAddresses.GetHandler(addressType);
                        TcpServiceAddress      address = (TcpServiceAddress)handler.FromBytes(addressBytes);
                        IEnumerable <Message>  request = service.MessageSerializer.Deserialize(din.BaseStream);

                        Message response;

                        // Proxy the command over the network,
                        if (command == 'a')
                        {
                            response = connector.Connect(address, ServiceType.Admin).Process(request);
                        }
                        else if (command == 'b')
                        {
                            response = connector.Connect(address, ServiceType.Block).Process(request);
                        }
                        else if (command == 'm')
                        {
                            response = connector.Connect(address, ServiceType.Manager).Process(request);
                        }
                        else if (command == 'r')
                        {
                            response = connector.Connect(address, ServiceType.Root).Process(request);
                        }
                        else
                        {
                            throw new IOException("Unknown command to proxy: " + command);
                        }

                        // Return the result,
                        service.MessageSerializer.Serialize(response, dout.BaseStream);
                        dout.Flush();
                    }
                } catch (SocketException e) {
                    if (e.ErrorCode == (int)SocketError.ConnectionReset)
                    {
                        // Ignore connection reset messages,
                    }
                } catch (IOException e) {
                    if (e is EndOfStreamException)
                    {
                        // Ignore this one oo,
                    }
                    else
                    {
                        service.Logger.Error("IO Error during connection input", e);
                    }
                } finally {
                    // Make sure the socket is closed before we return from the thread,
                    try {
                        socket.Close();
                    } catch (IOException e) {
                        service.Logger.Error("IO Error on connection close", e);
                    }
                }
            }
Ejemplo n.º 13
0
 public TcpProxyNetworkClient(TcpServiceAddress managerAddress, TcpServiceAddress proxyAddress, string password, INetworkCache cache)
     : base(managerAddress, new TcpProxyServiceConnector(proxyAddress, password), cache)
 {
 }