Example #1
0
        public async Task <IHttpActionResult> Join(JoinChannelBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ChannelDataRepository repository = new ChannelDataRepository();

            var channelModel = await repository.FindAsync(model.ChannelName);

            if (channelModel == null)
            {
                return(GetErrorResult(new IdentityResult("Channel not exists")));
            }

            var channel = ChannelsInfo.GetChannelInfo(channelModel.Name);

            if (channel != null)
            {
                channel.Members.Add(model.User);
                return(Ok(Convert.ToInt32(channel.Port)));
            }

            int         initPort       = 5000;
            VoiceServer newVoiceServer = new VoiceServer();

            while (initPort < 65000)
            {
                try
                {
                    newVoiceServer.Start(initPort);

                    ChannelsInfo.ListOfSockets.Add(initPort.ToString(), newVoiceServer);
                    ChannelsInfo.AddChannel(channelModel, initPort.ToString());
                    ChannelsInfo.AddMember(channelModel.Name, model.User);
                    break;
                }
                catch (Exception e)
                {
                    ChannelsInfo.ListOfSockets.Remove(initPort.ToString());
                    initPort++;
                }
            }

            using (UserDataRepository db = new UserDataRepository())
            {
                var user = db.FindByName(model.User);
                if (!user.RecenltyVisitedRooms.Split(',').Contains(model.ChannelName))
                {
                    db.UpdateVisitedChannels(model.User, model.ChannelName);
                }
            }

            return(Ok(initPort));
        }