Пример #1
0
 public Client(int id, int port, MJPEGVideo vid)
 {
     //Set sequence number to 1 as default, set parameters to vars
     seqNo        = 1;
     clientID     = id;
     portNo       = port;
     currentVideo = vid;
     //Create RTP model and new timer for video
     rtpModel       = new RTP(port);
     videoCountdown = new Timer();
 }
Пример #2
0
        private void ClientConnection(Socket sock)      //Maintain client communication!
        {
            //Initialize necessary vars: rcvBuffer for receiving bytes from socket, new client, RTP model, current video etc
            byte[]     rcvBuffer  = new byte[1024];
            int        i          = 0;
            MJPEGVideo currentVid = null;

            _rtpModel = null;
            Client newCli = null;

            try
            {
                while (true)
                {
                    //Receive bytes; if not receiving, break
                    int numBytes = sock.Receive(rcvBuffer);
                    if (numBytes <= 0)
                    {
                        break;
                    }
                    //Get UTF8 string, print to view
                    string msg = Encoding.UTF8.GetString(rcvBuffer, 0, numBytes);
                    AddClientActivity(msg);
                    //Break down message to find request type
                    char[]   delimiters  = { ',', ':', ';', '/', '\n', '\r', ' ' };
                    string[] brokenMsg   = msg.Split(delimiters);
                    string   requestType = brokenMsg[0];
                    //if SETUP, do SETUP!
                    if (requestType == "SETUP")
                    {
                        //index 20 is client port #
                        int clientPortNo = int.Parse(brokenMsg[20]);
                        //Create RTP model with client port
                        _rtpModel = new RTP(clientPortNo);
                        //Create new video with video name (index 6)
                        currentVid = new MJPEGVideo(brokenMsg[6]);
                        //Create new client and add to client list
                        newCli = new Client(GenerateRandomInt(), clientPortNo, currentVid);
                        clients.Add(newCli);
                        i = clientCount++;
                        //Add timer to elapsed (client timer) delegate
                        clients.ElementAt(i).GetClientTimer().Elapsed += FileProcessingTimer;
                        //Send client response
                        sock.Send(clients.ElementAt(i).ClientUTF8Response());
                    }
                    else
                    {
                        switch (requestType)
                        {
                        case "PLAY":
                            //Send client response and start timer
                            sock.Send(clients.ElementAt(i).ClientUTF8Response());
                            clients.ElementAt(i).StartClientTimer();
                            break;

                        case "PAUSE":
                            //Send client response and stop timer
                            sock.Send(clients.ElementAt(i).ClientUTF8Response());
                            clients.ElementAt(i).StopClientTimer();
                            break;

                        case "TEARDOWN":
                            //Send client response, initiate teardown and remove client ID from list
                            sock.Send(clients.ElementAt(i).ClientUTF8Response());
                            clients.ElementAt(i).InitiateTeardown();
                            uniqueClientIDs.Remove(clients.ElementAt(i).GetClientID());
                            break;

                        default:
                            //Just in case..
                            break;
                        }
                    }
                }
            } catch (SocketException e)
            {
                Console.WriteLine(e.ToString());
                //Safely close socket
                if (sock != null)
                {
                    sock.Close();
                }
            } finally
            {
                //Stop timer and safely close socket no matter what
                clients.ElementAt(i).StopClientTimer();
                if (sock != null)
                {
                    sock.Close();
                }
            }
        }