コード例 #1
0
ファイル: Server.cs プロジェクト: Samaed/RockTheSound
 //Préviens le départ d'un client
 private void sendRemoveClient(Client client)
 {
     foreach (Client cl in _clientList)
     {
         if (cl.Id != client.Id)
         {
             cl.Send(NetworkParser.getCode(NetworkParser.Operation.REMOVE_MEMBER) + ";" + client.Id);
         }
     }
 }
コード例 #2
0
ファイル: Server.cs プロジェクト: Samaed/RockTheSound
 //Préviens qu'un client vient d'arriver
 private void sendNewClient(Client client)
 {
     foreach (Client cl in _clientList)
     {
         if (cl.Id != client.Id)
         {
             cl.Send(NetworkParser.getCode(NetworkParser.Operation.NEW_MEMBER) + ";" + client.Id + ";" + client.Name);
         }
     }
 }
コード例 #3
0
ファイル: Server.cs プロジェクト: Samaed/RockTheSound
        //Reception d'un fichier
        private void receiveFile(string[] words, Client client)
        {
            ServerMainFrame.GetInstance().addLog(client.Id + "-" + client.Name + " receiving file : " + words[2]);
            int BufferSize = 1000;
            byte[] RecData = new byte[BufferSize];
            int RecBytes;

            NetworkStream netstream = null;
            try
            {
                netstream = new NetworkStream(client.Socket);
                string folderPath = ConfigurationManager.AppSettings["SamplesPath"];
                bool isExists = System.IO.Directory.Exists(folderPath);
                if (!isExists)
                    System.IO.Directory.CreateDirectory(folderPath);

                words[2] = words[2].Substring(words[2].LastIndexOf("\\") + 1);

                int fileNonOverrideIndex = 0;
                string[] fileNameSplitted = words[2].Split(new char[] { '.' });
                string saveFileName = folderPath + "\\" + words[2];

                while (System.IO.File.Exists(saveFileName))
                {
                    fileNonOverrideIndex++;
                    saveFileName = folderPath + "\\";

                    for (int i = 0; i < fileNameSplitted.Length - 1; i++)
                    {
                        saveFileName += fileNameSplitted[i];
                    }

                    saveFileName += "(" + fileNonOverrideIndex + ")." + fileNameSplitted[fileNameSplitted.Length - 1];
                }

                if (saveFileName != string.Empty)
                {
                    int totalrecbytes = 0;
                    FileStream Fs = new FileStream(saveFileName, FileMode.OpenOrCreate, FileAccess.Write);
                    bool stop = false;
                    while ((RecBytes = netstream.Read(RecData, 0, RecData.Length)) > 4 && !stop)
                    {
                        Fs.Write(RecData, 0, RecBytes);
                        totalrecbytes += RecBytes;
                        stop = RecBytes < 999;
                    }
                    Console.WriteLine(totalrecbytes);
                    Fs.Close();
                }
                netstream.Close();
                Sound newSound = new RTS.Model.Sound(words[1], words[2], saveFileName, new byte[1]);
                if (Math.Abs(newSound.FileSize - long.Parse(words[3])) < 250)
                {
                    ServerSoundLibrary.GetInstance().AddSound(newSound);
                    ServerMainFrame.GetInstance().addLog(client.Id + "-" + client.Name + " file received : " + words[2]);
                }
                else
                {
                    System.IO.File.Delete(saveFileName);
                    ServerMainFrame.GetInstance().addLog(client.Id + "-" + client.Name + " error on file size : " + words[2] + " (orig : " + words[3] + ",received :" + newSound.FileSize + ")");
                }
                client.GetNextServerSideNeededSample();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #4
0
ファイル: Server.cs プロジェクト: Samaed/RockTheSound
 //Gère une réception
 private void Handle(Client client, string received)
 {
     string[] words = received.Split(new char[] { ';' });
     NetworkParser.Operation toDo = NetworkParser.getOperation(words[0]);
     switch (toDo)
     {
         case NetworkParser.Operation.GIVE_FILE:
             receiveFile(words, client);
             break;
         case NetworkParser.Operation.SAMPLE_LIST:
             client.SetSampleList(words);
             break;
         case NetworkParser.Operation.READY:
             client.ClientReadyToReceive = true;
             break;
         case NetworkParser.Operation.READY_TO_RECEIVE:
             client.ClientReadySendSound(words[1]);
             break;
     }
 }
コード例 #5
0
ファイル: Server.cs プロジェクト: Samaed/RockTheSound
        //Service d'écoute TCP ( 1 client )
        public void TcpService()
        {
            while (true)//Boucle de connexion à un client
            {
                try
                {
                    Socket soc = _listener.AcceptSocket();
                    ServerMainFrame.GetInstance().addLog("Connected: " + soc.RemoteEndPoint);
                    Client client = new Client();//création d'un compte client
                    try
                    {
                        Stream s = new NetworkStream(soc);
                        StreamReader sr = new StreamReader(s);
                        StreamWriter sw = new StreamWriter(s);
                        sw.AutoFlush = true;
                        int nbOK = 0;
                        sw.WriteLine("Welcome to me ! What's your name?");//Seems legit
                        string name = sr.ReadLine();//demande du nom

                        ServerMainFrame.GetInstance().addLog("id " + client.Id + " is " + name);
                        client.Name = name;
                        client.Socket = soc;

                        _clientList.Add(client);//je l'ajoute à la liste des clients connectés
                        ClientMove = true;
                        sw.WriteLine(NetworkParser.getCode(NetworkParser.Operation.HELLO) + name + ";" + client.Id);//Pour lui envoyer son id
                        sendNewClient(client);
                        foreach (Client cl in _clientList)
                        {
                            if (cl != client)
                            {
                                //Envoi de la liste des clients connectés
                                sw.WriteLine(NetworkParser.getCode(NetworkParser.Operation.NEW_MEMBER) + ";" + cl.Id + ";" + cl.Name);
                                sr.ReadLine();
                            }
                        }
                        while (true)
                        {

                            string received = sr.ReadLine();//On attends un envoie du client
                            if (received == NetworkParser.getCode(NetworkParser.Operation.DISCONNECT))
                            {//le client veut se déconnecter
                                break;
                            }

                            if (received == "" || received == null)
                            {
                                sw.WriteLine(NetworkParser.getCode(NetworkParser.Operation.NONE));//Qestion vide : réponse vide
                            }
                            else if (received == NetworkParser.getCode(NetworkParser.Operation.OK) && nbOK++ >= 3)
                            {
                                nbOK = 0;
                            }
                            else
                            {
                                if (received != NetworkParser.getCode(NetworkParser.Operation.OK))
                                {
                                    nbOK = 0;
                                }
                                Handle(client, received);
                                sw.WriteLine(NetworkParser.getCode(NetworkParser.Operation.OK));
                            }

                        }
                        s.Close();
                    }
                    catch (Exception e)//si déconnection pour une raison inconnue
                    {
                        ServerMainFrame.GetInstance().addLog(client.Id + " : " + e.Message);
                    }
                    ServerMainFrame.GetInstance().addLog(client.Id + "-" + client.Name + " disconnected (" + soc.RemoteEndPoint + ")");
                    soc.Close();
                    sendRemoveClient(client);
                    _clientList.Remove(client);
                    ClientMove = true;
                }
                catch (Exception) { }
            }
        }
コード例 #6
0
ファイル: Server.cs プロジェクト: Samaed/RockTheSound
 //Rechargement du serveur
 public void Reload()
 {
     ServerMainFrame.GetInstance().addLog("RELOADING SERVER");
     Client[] clList = new Client[_clientList.Count];
     int i = 0;
     foreach (Client cl in _clientList)//Utile car beaucoup de traitements asynchrones
     {
         clList[i++] = cl;
     }
     foreach (Client cl in clList)
     {
         cl.Send("disconnect");
     }
     this._listener.Stop();
     this._udpSocket.Close();
     this._connected = false;
     this._clientList = new List<Client>();
     this._clientMove = true;
     _portTCP = int.Parse(ConfigurationManager.AppSettings["TCPPort"]);
     _portUDP = int.Parse(ConfigurationManager.AppSettings["UDPPort"]);
     _samplesPath = ConfigurationManager.AppSettings["SamplesPath"];
     this.Start();
 }