public TcpConnection(TcpServiceConnector connector, Socket socket) { this.connector = connector; this.socket = socket; lockCount = 1; lastLockTimestamp = DateTime.Now; }
public ConnectionDestroyThread(TcpServiceConnector connector) { this.connector = connector; thread = new Thread(Execute); thread.Name = "TcpServceConnector::ConnectionDestroy"; thread.IsBackground = true; thread.Start(); }
public MessageProcessor(TcpServiceConnector connector, TcpServiceAddress address, ServiceType serviceType) { this.connector = connector; this.address = address; this.serviceType = serviceType; }
public override bool HandleCommandLine(CommandLine commandLine) { string protocol = commandLine.GetOptionValue("protocol", "tcp"); string address = commandLine.GetOptionValue("address", null); string format = commandLine.GetOptionValue("format", "binary"); if (String.IsNullOrEmpty(address)) return false; IServiceConnector connector; if (protocol.Equals("tcp", StringComparison.InvariantCultureIgnoreCase)) { string netPassword = commandLine.GetOptionValue("password"); if (String.IsNullOrEmpty(netPassword)) throw new ArgumentException("Netwrok password required for TCP/IP protocol."); connector = new TcpServiceConnector(netPassword); } else if (protocol.Equals("http", StringComparison.InvariantCultureIgnoreCase)) { string user = commandLine.GetOptionValue("user"); string password = commandLine.GetOptionValue("password"); if (String.IsNullOrEmpty(user)) throw new ArgumentException("User name not specified. for HTTP connection."); if (String.IsNullOrEmpty(password)) throw new ArgumentException("Password not specofoed for HTTP connection."); connector = new HttpServiceConnector(user, password); } else { throw new ArgumentException("Invalid protocol '" + protocol + "'."); } IMessageSerializer serializer; if (format.Equals("xml", StringComparison.InvariantCultureIgnoreCase)) { serializer = new XmlRpcMessageSerializer(); } else if (format.Equals("binary", StringComparison.InvariantCultureIgnoreCase)) { serializer = new BinaryRpcMessageSerializer(); } else if (format.Equals("json", StringComparison.InvariantCultureIgnoreCase)) { if (JsonRpcMessageSerializer == null) throw new ApplicationException("The JSON serializer was not installed."); serializer = JsonRpcMessageSerializer; } else { throw new ArgumentException("Invalid message format."); } connector.MessageSerializer = serializer; NetworkProfile networkProfile = new NetworkProfile(connector); NetworkConfigSource configSource = new NetworkConfigSource(); configSource.AddNetworkNode(address); networkProfile.Configuration = configSource; ((CloudAdmin) Application).SetNetworkContext(new NetworkContext(networkProfile)); return true; }
public override CommandResultCode Execute(IExecutionContext context, CommandArguments args) { if (Application.ActiveContext != null && Application.ActiveContext.IsIsolated) { Error.WriteLine("a context is already opened: try to disconnect first"); Error.WriteLine(); return CommandResultCode.ExecutionFailed; } if (!args.MoveNext()) return CommandResultCode.SyntaxError; if (args.Current != "to") return CommandResultCode.SyntaxError; if (!args.MoveNext()) return CommandResultCode.SyntaxError; string address = args.Current; IServiceAddress serviceAddress; try { serviceAddress = ServiceAddresses.ParseString(address); } catch(Exception) { Error.WriteLine("Invalid service address specified: {0}", address); return CommandResultCode.ExecutionFailed; } NetworkConfigSource configSource = new NetworkConfigSource(); try { configSource.AddNetworkNode(serviceAddress); } catch(Exception e) { Error.WriteLine("The address '" + address + "' is invalid: " + e.Message); return CommandResultCode.ExecutionFailed; } string protocol = "tcp"; string credentials = String.Empty; string format = "binary"; if (args.MoveNext()) { if (args.Current == "identified") { if (!args.MoveNext()) return CommandResultCode.SyntaxError; if (args.Current != "by") return CommandResultCode.SyntaxError; if (!args.MoveNext()) return CommandResultCode.SyntaxError; credentials = args.Current; if (args.MoveNext()) { if (args.Current != "on") return CommandResultCode.SyntaxError; protocol = args.Current; if (args.MoveNext()) { if (args.Current != "with") return CommandResultCode.SyntaxError; format = args.Current; } } } else if (args.Current == "on") { if (!args.MoveNext()) return CommandResultCode.SyntaxError; protocol = args.Current; if (args.MoveNext()) { if (args.Current != "with") return CommandResultCode.SyntaxError; format = args.Current; } } else if (args.Current == "with") { if (!args.MoveNext()) return CommandResultCode.SyntaxError; format = args.Current; } else { return CommandResultCode.SyntaxError; } } IServiceConnector connector; if (protocol == "tcp") { if (String.IsNullOrEmpty(credentials)) { while(String.IsNullOrEmpty(credentials = Readline.ReadPassword("password: "******"please provide a valid password..."); } Out.WriteLine(); } connector = new TcpServiceConnector(credentials); } else if (protocol == "http") { string userName = credentials; string password = null; int index = credentials.IndexOf(':'); if (index != -1) { password = credentials.Substring(index + 1); userName = credentials.Substring(0, index); } if (String.IsNullOrEmpty(password)) { while(String.IsNullOrEmpty(password = Readline.ReadPassword("password: "******"please provide a valid password..."); } Out.WriteLine(); } connector = new HttpServiceConnector(userName, password); } else { return CommandResultCode.SyntaxError; } IMessageSerializer serializer; if (format == "binary") { serializer = new BinaryRpcMessageSerializer(); } else if (format == "xml") { serializer = new XmlRpcMessageSerializer(); } else if (format == "json") { if (JsonRpcMessageSerializer == null) { Error.WriteLine("JSON serializer was not installed."); Error.WriteLine(); return CommandResultCode.ExecutionFailed; } serializer = JsonRpcMessageSerializer; } else { return CommandResultCode.SyntaxError; } connector.MessageSerializer = serializer; NetworkProfile networkProfile = new NetworkProfile(connector); networkProfile.Configuration = configSource; //TODO: test the connection is correct ... ((CloudAdmin)Application).SetNetworkContext(new NetworkContext(networkProfile)); Out.WriteLine("connected successfully to {0}" , address); Out.WriteLine(); return CommandResultCode.Success; }
public TcpMessageProcessor(TcpServiceConnector connector, TcpServiceAddress address, ServiceType serviceType) { this.connector = connector; this.address = address; this.serviceType = serviceType; }
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); } } }
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); } } }
public override CommandResultCode Execute(IExecutionContext context, CommandArguments args) { if (Application.ActiveContext != null && Application.ActiveContext.IsIsolated) { Error.WriteLine("a context is already opened: try to disconnect first"); Error.WriteLine(); return(CommandResultCode.ExecutionFailed); } if (!args.MoveNext()) { return(CommandResultCode.SyntaxError); } if (args.Current != "to") { return(CommandResultCode.SyntaxError); } if (!args.MoveNext()) { return(CommandResultCode.SyntaxError); } string address = args.Current; IServiceAddress serviceAddress; try { serviceAddress = ServiceAddresses.ParseString(address); } catch (Exception) { Error.WriteLine("Invalid service address specified: {0}", address); return(CommandResultCode.ExecutionFailed); } NetworkConfigSource configSource = new NetworkConfigSource(); try { configSource.AddNetworkNode(serviceAddress); } catch (Exception e) { Error.WriteLine("The address '" + address + "' is invalid: " + e.Message); return(CommandResultCode.ExecutionFailed); } string protocol = "tcp"; string credentials = String.Empty; string format = "binary"; if (args.MoveNext()) { if (args.Current == "identified") { if (!args.MoveNext()) { return(CommandResultCode.SyntaxError); } if (args.Current != "by") { return(CommandResultCode.SyntaxError); } if (!args.MoveNext()) { return(CommandResultCode.SyntaxError); } credentials = args.Current; if (args.MoveNext()) { if (args.Current != "on") { return(CommandResultCode.SyntaxError); } protocol = args.Current; if (args.MoveNext()) { if (args.Current != "with") { return(CommandResultCode.SyntaxError); } format = args.Current; } } } else if (args.Current == "on") { if (!args.MoveNext()) { return(CommandResultCode.SyntaxError); } protocol = args.Current; if (args.MoveNext()) { if (args.Current != "with") { return(CommandResultCode.SyntaxError); } format = args.Current; } } else if (args.Current == "with") { if (!args.MoveNext()) { return(CommandResultCode.SyntaxError); } format = args.Current; } else { return(CommandResultCode.SyntaxError); } } IServiceConnector connector; if (protocol == "tcp") { if (String.IsNullOrEmpty(credentials)) { while (String.IsNullOrEmpty(credentials = Readline.ReadPassword("password: "******"please provide a valid password..."); } Out.WriteLine(); } connector = new TcpServiceConnector(credentials); } else if (protocol == "http") { string userName = credentials; string password = null; int index = credentials.IndexOf(':'); if (index != -1) { password = credentials.Substring(index + 1); userName = credentials.Substring(0, index); } if (String.IsNullOrEmpty(password)) { while (String.IsNullOrEmpty(password = Readline.ReadPassword("password: "******"please provide a valid password..."); } Out.WriteLine(); } // TODO: connector = new HttpServiceConnector(userName, password); Out.WriteLine("Not supported yet."); return(CommandResultCode.ExecutionFailed); } else { return(CommandResultCode.SyntaxError); } IMessageSerializer serializer; if (format == "binary") { serializer = new BinaryRpcMessageSerializer(); } else if (format == "xml") { //TODO: serializer = new XmlRpcMessageSerializer(); return(CommandResultCode.ExecutionFailed); } else if (format == "json") { if (JsonRpcMessageSerializer == null) { Error.WriteLine("JSON serializer was not installed."); Error.WriteLine(); return(CommandResultCode.ExecutionFailed); } serializer = JsonRpcMessageSerializer; } else { return(CommandResultCode.SyntaxError); } connector.MessageSerializer = serializer; NetworkProfile networkProfile = new NetworkProfile(connector); networkProfile.Configuration = configSource; //TODO: test the connection is correct ... ((CloudAdmin)Application).SetNetworkContext(new NetworkContext(networkProfile)); Out.WriteLine("connected successfully to {0}", address); Out.WriteLine(); return(CommandResultCode.Success); }
public override bool HandleCommandLine(CommandLine commandLine) { string protocol = commandLine.GetOptionValue("protocol", "tcp"); string address = commandLine.GetOptionValue("address", null); string format = commandLine.GetOptionValue("format", "binary"); if (String.IsNullOrEmpty(address)) { return(false); } IServiceConnector connector; if (protocol.Equals("tcp", StringComparison.InvariantCultureIgnoreCase)) { string netPassword = commandLine.GetOptionValue("password"); if (String.IsNullOrEmpty(netPassword)) { throw new ArgumentException("Netwrok password required for TCP/IP protocol."); } connector = new TcpServiceConnector(netPassword); } else if (protocol.Equals("http", StringComparison.InvariantCultureIgnoreCase)) { string user = commandLine.GetOptionValue("user"); string password = commandLine.GetOptionValue("password"); if (String.IsNullOrEmpty(user)) { throw new ArgumentException("User name not specified. for HTTP connection."); } if (String.IsNullOrEmpty(password)) { throw new ArgumentException("Password not specofoed for HTTP connection."); } //TODO: connector = new HttpServiceConnector(user, password); throw new NotSupportedException("HTTP not supported yet."); } else { throw new ArgumentException("Invalid protocol '" + protocol + "'."); } IMessageSerializer serializer; if (format.Equals("xml", StringComparison.InvariantCultureIgnoreCase)) { //TODO: serializer = new XmlRpcMessageSerializer(); throw new NotSupportedException("XML format not supported yet."); } else if (format.Equals("binary", StringComparison.InvariantCultureIgnoreCase)) { serializer = new BinaryRpcMessageSerializer(); } else if (format.Equals("json", StringComparison.InvariantCultureIgnoreCase)) { if (JsonRpcMessageSerializer == null) { throw new ApplicationException("The JSON serializer was not installed."); } serializer = JsonRpcMessageSerializer; } else { throw new ArgumentException("Invalid message format."); } connector.MessageSerializer = serializer; NetworkProfile networkProfile = new NetworkProfile(connector); NetworkConfigSource configSource = new NetworkConfigSource(); configSource.AddNetworkNode(address); networkProfile.Configuration = configSource; ((CloudAdmin)Application).SetNetworkContext(new NetworkContext(networkProfile)); return(true); }