Esempio n. 1
0
        public void Connect(string name, string user_URL, string server_URL)
        {
            this._name = name;

            TcpChannel channel = new TcpChannel(URL.GetPort(user_URL));

            ChannelServices.RegisterChannel(channel, false);

            //get remote server object
            IServer server = (IServer)Activator.GetObject(typeof(IServer), server_URL);

            //weak check
            if (server == null)
            {
                Console.WriteLine("Could not locate server at " + server_URL);
                return;
            }

            _server = server;

            //publish remote client object
            RemotingServices.Marshal(this, URL.GetURI(user_URL), typeof(IClient));

            server.AddClient(user_URL, _name);

            Console.WriteLine("Connected as user " + name + " to server at " + server_URL);

            AddClientURLToFile(user_URL);
        }
Esempio n. 2
0
        public bool Connect(string server_URL)
        {
            //get remote server object
            IServer server = (IServer)Activator.GetObject(typeof(IServer), server_URL);

            //weak check
            if (server == null)
            {
                Console.WriteLine("Could not locate server at " + server_URL);
                return(false);
            }

            _server    = server;
            _serverURL = server_URL;

            //publish remote client object
            RemotingServices.Marshal(this, URL.GetURI(_MyURL), typeof(IClient));

            string client_URL = "tcp://" + GetLocalIPAddress() + ":" + URL.GetPort(_MyURL) + "/" + URL.GetURI(_MyURL);

            server.AddClient(client_URL, _name);

            Console.WriteLine("Connected to server " + server_URL + " as user " + _name);
            return(true);
        }
Esempio n. 3
0
        private void btnRezervare_Click(object sender, EventArgs e)
        {
            string numeClient = txtNumeClient.Text;
            int    nrLocuri   = int.Parse(txtNrLocuri.Text);
            int    idCursa;
            int    idClient;
            Cursa  cursa;

            if (listViewCurse.SelectedItems.Count == 1)
            {
                cursa = new Cursa()
                {
                    Id                = Convert.ToInt32(listViewCurse.SelectedItems[0].SubItems[0].Text),
                    Destinatie        = listViewCurse.SelectedItems[0].SubItems[1].Text,
                    DateTime          = Convert.ToDateTime(listViewCurse.SelectedItems[0].SubItems[2].Text),
                    LocuriDisponibile = Convert.ToInt32(listViewCurse.SelectedItems[0].SubItems[3].Text),
                    Oficiu            = 1
                };
                idCursa = Convert.ToInt32(listViewCurse.SelectedItems[0].SubItems[0].Text);
                if (Server.FindClient(numeClient) == null)
                {
                    Server.AddClient(new Client(numeClient));
                }
                idClient = Server.FindClient(numeClient).Id;
                Rezervare rezervare = new Rezervare(idCursa, idClient, nrLocuri);
                Server.AddRezervare(rezervare);
                cursa.LocuriDisponibile = cursa.LocuriDisponibile - nrLocuri;
                Server.UpdateCursa(cursa);
                ShowClienti(cursa.Id);
            }
        }
Esempio n. 4
0
        private void ReadClientState(TPeer source, ref PacketReader reader)
        {
            // If this connection is new create a client info object for it now
            ClientInfo client;

            if (!_clientsByConnection.TryGetValue(source, out client))
            {
                var playerName = reader.ReadString();
                if (playerName == null)
                {
                    Log.Error("Received a client state packet for a null player name");
                    return;
                }

                var id = _playerIds.Register(playerName);

                client = new ClientInfo(playerName, id);

                _clientsByConnection.Add(source, client);
                _connectionsByPlayerId[id] = source;

                // ReSharper disable once InconsistentlySynchronizedField (Justification: the lock used in the other place protects something else, not this)
                _server.AddClient(source, client);
            }
            else
            {
                //This connection is not new, which means we can avoid reading player name (which would cost an allocation)
                reader.SkipString();
            }

            //Remove this client from all rooms
            for (var i = 0; i < client.Rooms.Count; i++)
            {
                _connectionsByRoom[client.Rooms[i]].Remove(source);
            }
            client.Rooms.Clear();

            //Add this client to the appropriate rooms
            var roomCount = reader.ReadUInt16();

            for (ushort i = 0; i < roomCount; i++)
            {
                var id = reader.ReadUInt16();
                client.Rooms.Add(id);

                List <TPeer> connectionsInRoom;
                if (!_connectionsByRoom.TryGetValue(id, out connectionsInRoom))
                {
                    connectionsInRoom = new List <TPeer>();
                    _connectionsByRoom.Add(id, connectionsInRoom);
                }

                connectionsInRoom.Add(source);
            }

            Log.Debug("Updated client state ('{0}', {1} rooms)", client.PlayerName, client.Rooms.Count);
        }
Esempio n. 5
0
 /// <summary>
 /// Подсоединение к серверу
 /// </summary>
 /// <param name="server"></param>
 public void ConnectTo(IServer server)
 {
     server.AddClient(this);
     Server = server;
 }
        protected override void OnAddedClient(ClientInfo <TPeer> client)
        {
            base.OnAddedClient(client);

            _server.AddClient(client);
        }
Esempio n. 7
0
        private Response handleRequest(Request request)
        {
            Response response = null;

            if (request is LoginRequest)
            {
                Console.WriteLine("Login request ...");
                LoginRequest logReq = (LoginRequest)request;
                //UserDTO udto = logReq.User;
                // User user = DTOUtils.getFromDTO(udto);
                Operator op = logReq.Operator;
                try
                {
                    lock (server)
                    {
                        server.Login(op, this);
                    }
                    return(new OkResponse());
                }
                catch (FirmaTransportException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is GetCurseRequest)
            {
                Console.WriteLine("Get curse request");
                List <Cursa> curse;
                try
                {
                    lock (server)
                    {
                        curse = server.GetAllCurse();
                    }
                    connected = false;
                    return(new AllCurseResponse(curse));
                }
                catch (FirmaTransportException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetRezervariByCursaRequest)
            {
                Console.WriteLine("GetRezervariByCursaRequest ...");
                GetRezervariByCursaRequest senReq = (GetRezervariByCursaRequest)request;
                int idCUrsa = senReq.IdCursa;
                try
                {
                    List <Rezervare> rezervari;
                    lock (server)
                    {
                        rezervari = server.GetAllByCursa(idCUrsa);
                    }
                    return(new RezervariByCursaResponse(rezervari));
                }
                catch (FirmaTransportException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetClientByIdRequest)
            {
                Console.WriteLine("GetClientById Request ...");
                GetClientByIdRequest getReq = (GetClientByIdRequest)request;
                int idClient = getReq.IdClient;
                try
                {
                    Client client;
                    lock (server)
                    {
                        client = server.FindClient(idClient);
                    }
                    return(new ClientResponse(client));
                }
                catch (FirmaTransportException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is AddRezervareRequest)
            {
                Console.WriteLine("AddRezervare request ...");
                AddRezervareRequest addRezReq = (AddRezervareRequest)request;
                //UserDTO udto = logReq.User;
                // User user = DTOUtils.getFromDTO(udto);
                Rezervare rezervare = addRezReq.Rezervare;
                try
                {
                    lock (server)
                    {
                        server.AddRezervare(rezervare);
                    }
                    return(new OkResponse());
                }
                catch (FirmaTransportException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetClientByNameRequest)
            {
                Console.WriteLine("GetClientById Request ...");
                GetClientByNameRequest getReq = (GetClientByNameRequest)request;
                String NumeClient             = getReq.Nume;
                try
                {
                    Client client;
                    lock (server)
                    {
                        client = server.FindClient(NumeClient);
                    }
                    return(new ClientResponse(client));
                }
                catch (FirmaTransportException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is AddClientRequest)
            {
                Console.WriteLine("AddClient request ...");
                AddClientRequest addRezReq = (AddClientRequest)request;
                //UserDTO udto = logReq.User;
                // User user = DTOUtils.getFromDTO(udto);
                Client client = addRezReq.Client;
                try
                {
                    lock (server)
                    {
                        server.AddClient(client);
                    }
                    return(new OkResponse());
                }
                catch (FirmaTransportException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is UpdateCursaRequest)
            {
                Console.WriteLine("UpdateCursa request ...");
                UpdateCursaRequest addRezReq = (UpdateCursaRequest)request;
                //UserDTO udto = logReq.User;
                // User user = DTOUtils.getFromDTO(udto);
                Cursa cursa = addRezReq.cursa;
                try
                {
                    lock (server)
                    {
                        server.UpdateCursa(cursa);
                    }
                    return(new OkResponse());
                }
                catch (FirmaTransportException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }
            return(response);
        }