Recieve() public static method

Receieves a single object over the specified stream
public static Recieve ( NetworkStream stream ) : object
stream System.Net.Sockets.NetworkStream Stream to receive the message from
return object
Example #1
0
        /// <summary>
        /// Tests a connection to see if it is valid
        /// </summary>
        /// <param name="stream">Stream to test</param>
        /// <returns>True if the connection is valid, false otherwise</returns>
        public static bool Test(NetworkStream stream)
        {
            Message.TestMsg(stream);
            object response = Message.Recieve(stream);

            return(response is string && "Test" == (string)response);
        }
Example #2
0
        /// <summary>
        /// Sends an authentication request to the server.  Returns a UserToken
        /// object if successful, or null otherwise.
        /// </summary>
        /// <param name="credentials">Credentials object that contains the username and password of the user.</param>
        /// <param name="register">True if registering a new user, false if verifying a current one.</param>
        /// <returns>UserToken if successful, null otherwise</returns>
        public UserToken Authenticate(Credentials credentials, bool register)
        {
            AuthRequest request = new AuthRequest(credentials, register);
            object      reply;

            using (TcpClient client = new TcpClient()) {
                client.Connect(serverEndPoint);
                NetworkStream networkStream = client.GetStream();

                Message.Send(networkStream, request);
                reply = Message.Recieve(networkStream);
            }

            if (reply == null)
            {
                throw new Exception("BBRequest Error: Expected reply but recieved none");                //Console.Error.WriteLine("BBRequest Error: Expected reply but recieved none");
            }
            else if (!(reply is AuthResponse))
            {
                throw new Exception("BBRequest Error: Expected AuthResponse but recieved unknown response type");
            }
            else
            {
                return(((AuthResponse)reply).token);
            }
        }
Example #3
0
        /// <summary>
        /// Sends a request to the server to verify a user token is valid.
        /// </summary>
        /// <param name="token">Token to verify</param>
        /// <returns>True if the token is valid</returns>
        public bool VerifyToken(UserToken token)
        {
            TokenVerifyRequest request = new TokenVerifyRequest(token);
            object             reply;

            using (TcpClient client = new TcpClient()) {
                client.Connect(serverEndPoint);
                NetworkStream networkStream = client.GetStream();

                Message.Send(networkStream, request);
                reply = Message.Recieve(networkStream);
            }

            if (reply == null)
            {
                throw new Exception("BBRequest Error: Expected reply but recieved none");                //Console.Error.WriteLine("BBRequest Error: Expected reply but recieved none");
            }
            else if (!(reply is TokenVerifyResponse))
            {
                throw new Exception("BBRequest Error: Expected VerifyTokenResponse but recieved unknown response type");
            }
            else
            {
                return(((TokenVerifyResponse)reply).valid);
            }
        }
Example #4
0
        /// <summary>
        /// Sends a BBRequest to the server
        /// </summary>
        /// <param name="request">The BBRequest to send</param>
        /// <returns>If the reqeuest expects a response, returns the response</returns>
        public BBResponse SendRequest(BBRequest request)
        {
            object reply;

            using (TcpClient client = new TcpClient()) {
                client.Connect(serverEndPoint);
                NetworkStream networkStream = client.GetStream();

                Message.Send(networkStream, request);
                reply = Message.Recieve(networkStream);
            }

            if (reply == null)
            {
                throw new Exception("BBRequest Error: Expected reply but recieved none");                //Console.Error.WriteLine("BBRequest Error: Expected reply but recieved none");
            }
            else
            {
                return(reply as BBResponse);
            }
        }