예제 #1
0
        /// <summary>
        /// Connect to a kRPC server on the specified IP address and port numbers. If
        /// streamPort is 0, does not connect to the stream server.
        /// Passes an optional name to the server to identify the client (up to 32 bytes of UTF-8 encoded text).
        /// </summary>
        public Connection(string name = "", IPAddress address = null, int rpcPort = 50000, int streamPort = 50001)
        {
            if (address == null)
            {
                address = IPAddress.Loopback;
            }

            rpcClient = new TcpClient();
            rpcClient.Connect(address, rpcPort);
            var rpcStream = rpcClient.GetStream();

            rpcStream.Write(Encoder.rpcHelloMessage, 0, Encoder.rpcHelloMessage.Length);
            var clientName = Encoder.EncodeClientName(name);

            rpcStream.Write(clientName, 0, clientName.Length);
            var clientIdentifier = new byte[Encoder.clientIdentifierLength];

            rpcStream.Read(clientIdentifier, 0, Encoder.clientIdentifierLength);

            if (streamPort == 0)
            {
                StreamManager = null;
            }
            else
            {
                StreamManager = new StreamManager(this, address, streamPort, clientIdentifier);
            }
        }
예제 #2
0
파일: Connection.cs 프로젝트: zentarul/krpc
        public Connection(string name = "", IPAddress address = null, int rpcPort = 50000, int streamPort = 50001)
        {
            if (address == null)
            {
                address = IPAddress.Loopback;
            }

            rpcClient = new TcpClient();
            rpcClient.Connect(address, rpcPort);
            rpcStream = rpcClient.GetStream();
            rpcStream.Write(Encoder.RPCHelloMessage, 0, Encoder.RPCHelloMessage.Length);
            var clientName = Encoder.EncodeClientName(name);

            rpcStream.Write(clientName, 0, clientName.Length);
            var clientIdentifier = new byte[Encoder.ClientIdentifierLength];

            rpcStream.Read(clientIdentifier, 0, Encoder.ClientIdentifierLength);
            codedRpcStream = new CodedOutputStream(rpcStream, true);

            if (streamPort != 0)
            {
                streamClient = new TcpClient();
                streamClient.Connect(address, streamPort);
                var streamStream = streamClient.GetStream();
                streamStream.Write(Encoder.StreamHelloMessage, 0, Encoder.StreamHelloMessage.Length);
                streamStream.Write(clientIdentifier, 0, clientIdentifier.Length);
                var recvOkMessage = new byte [Encoder.OkMessage.Length];
                streamStream.Read(recvOkMessage, 0, Encoder.OkMessage.Length);
                if (!recvOkMessage.SequenceEqual(Encoder.OkMessage))
                {
                    throw new InvalidOperationException("Did not receive OK message from server");
                }
                StreamManager = new StreamManager(this, streamClient);
            }
        }
예제 #3
0
 internal UpdateThread(StreamManager manager, IPAddress address, int port, byte[] clientIdentifier)
 {
     this.manager          = manager;
     this.address          = address;
     this.port             = port;
     this.clientIdentifier = clientIdentifier;
 }
예제 #4
0
        public Connection(string name = "", IPAddress address = null, int rpcPort = 50000, int streamPort = 50001)
        {
            if (address == null)
            {
                address = IPAddress.Loopback;
            }

            rpcClient = new TcpClient();
            rpcClient.Connect(address, rpcPort);
            rpcStream      = rpcClient.GetStream();
            codedRpcStream = new CodedOutputStream(rpcStream, true);
            var request = new ConnectionRequest();

            request.Type       = Type.Rpc;
            request.ClientName = name;
            codedRpcStream.WriteLength(request.CalculateSize());
            request.WriteTo(codedRpcStream);
            codedRpcStream.Flush();
            int size     = ReadMessageData(rpcStream, ref responseBuffer);
            var response = ConnectionResponse.Parser.ParseFrom(new CodedInputStream(responseBuffer, 0, size));

            if (response.Status != ConnectionResponse.Types.Status.Ok)
            {
                throw new ConnectionException(response.Message);
            }

            if (streamPort != 0)
            {
                streamClient = new TcpClient();
                streamClient.Connect(address, streamPort);
                var streamStream = streamClient.GetStream();
                request                  = new ConnectionRequest();
                request.Type             = Type.Stream;
                request.ClientIdentifier = response.ClientIdentifier;
                var codedStreamStream = new CodedOutputStream(streamStream, true);
                codedStreamStream.WriteLength(request.CalculateSize());
                request.WriteTo(codedStreamStream);
                codedStreamStream.Flush();
                size     = ReadMessageData(streamStream, ref responseBuffer);
                response = ConnectionResponse.Parser.ParseFrom(new CodedInputStream(responseBuffer, 0, size));
                if (response.Status != ConnectionResponse.Types.Status.Ok)
                {
                    throw new ConnectionException(response.Message);
                }
                StreamManager = new StreamManager(this, streamClient);
            }

            Services.KRPC.Service.AddExceptionTypes(this);
        }
예제 #5
0
 internal UpdateThread(StreamManager manager, IPAddress address, int port, byte[] clientIdentifier)
 {
     this.manager = manager;
     this.address = address;
     this.port = port;
     this.clientIdentifier = clientIdentifier;
 }
예제 #6
0
 public UpdateThread(StreamManager streamManager, TcpClient streamClient)
 {
     manager = streamManager;
     stream  = streamClient.GetStream();
 }
예제 #7
0
 internal Stream(Connection connection, Request request)
 {
     streamManager = connection.StreamManager;
     Id            = streamManager.AddStream(request, typeof(ReturnType));
 }
예제 #8
0
 public UpdateThread (StreamManager streamManager, TcpClient streamClient)
 {
     manager = streamManager;
     stream = streamClient.GetStream ();
 }