Exemple #1
0
        /** 
         * Function to send a message to the server
         * \param stream - The stream to write to
         * \param msg - The message to send
         * \return - Retun true on success, false on failure
         */
        private bool sendMessage(NetworkStream stream, Message msg)
        {
            string json = JsonConvert.SerializeObject(msg);

            using (MemoryStream mstream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(mstream))
                {
                    byte[] encodedJson = Encoding.UTF8.GetBytes(json);
                    writer.Write(hostToBig_i32(encodedJson.Length));
                    writer.Write(encodedJson);
                }

                byte[] request = mstream.ToArray();
                try
                {
                    stream.Write(request, 0, request.Length);
                    stream.Flush();
                }
                catch(SocketException ex)
                {
                    Message emsg = new Message("error_socket", null);
                    emsg.AddParameter("error_code", ex.ErrorCode);
                    emsg.AddParameter("message", ex.Message);                    
                    recvq.Enqueue(emsg);
                    return false;
                }
            }

            return true;
        }
Exemple #2
0
        /**
         * Function used to handle messages coming from the GUI client
         * \param msg - The message to handle
         */
        private void dispatchSendMsg(Message msg)
        {
            switch(msg.Command)
            {
                case "connect":
                    // Try to connect to server
                    Client = new TcpClient();
                    string host = msg.Arguments["host"].ToString();
                    int port = Convert.ToInt32(msg.Arguments["port"]);

                    try
                    {
                        Client.Connect(host, port);
                    }
                    catch(Exception ex)
                    {
                        msg.Command = "connect_failed";
                        msg.Arguments.Add("message", ex.Message);
                        recvq.Enqueue(msg);                    
                        return;
                    }

                    if (Client.Connected)
                    {
                        // Store the stream from the TCP client
                        ClientStream = Client.GetStream();
                        // Empty the network buffer
                        recvBuffer.Clear();
                        // Update the message to a connection response message and send it back to GUI client
                        msg.Command = "connect_ok";
                        recvq.Enqueue(msg);                        
                    }
                    else
                    {
                        msg.Command = "connect_failed";
                        msg.Arguments.Add("message", "Connection failed");
                        recvq.Enqueue(msg);                        
                    }
                    break;

                case "disconnect":
                    // Disconnect from server and send response back to GUI client
                    if (ClientStream != null)
                        ClientStream.Close();
                    ClientStream = null;

                    if(Client != null && Client.Connected)            
                        Client.Close();                            
                    Client = null;

                    msg.Command = "disconnect_ok";
                    recvq.Enqueue(msg);                    
                    break;

                default:                    
                    if (ClientStream == null)
                    {
                        Message responseMsg = new Message("error", null);
                        responseMsg.AddParameter("message", "Can not send message, not connected to peer");                        
                        recvq.Enqueue(responseMsg);
                        return;
                    }

                    // Send message to server
                    if(!sendMessage(ClientStream, msg))
                    {
                        Message responseMsg = new Message("error", null);
                        responseMsg.AddParameter("message", "Unable to send message");                        
                        recvq.Enqueue(responseMsg);
                        return;
                    }
                    break;
            }            
        }