Esempio n. 1
0
        private void Process(object arg)
        {
            MemoryStream stream;
            Socket       client = (Socket)arg;

            try {
                byte[] buffer = new byte[bufferSize];
                while (true)
                {
                    stream = new MemoryStream();
                    int totalBytes = 0;
                    Console.Write("took ");
                    do
                    {
                        short bytes = (short)client.Receive(buffer);
                        stream.Write(buffer, 0, bytes);
                        Console.Write(bytes + ", ");
                        totalBytes += bytes;
                    }while (client.Available > 0);
                    Console.WriteLine();
                    Console.WriteLine("Сообщение получено от " + client.RemoteEndPoint.ToString() + "в размере " + totalBytes);
                    dbRequest request = (dbRequest)DbObject.Deserialize(stream);
                    stream.Close();
                    dbResult result = ProcessDbObject(request);
                    SendDbResult(client, result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Разъединение " + client.RemoteEndPoint.ToString());
                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }
        }
Esempio n. 2
0
        private dbResult ProcessDbObject(dbRequest request)
        {
            dbResult result = new dbResult(request.action, request.selectAction, false);

            try
            {
                switch (request.selectAction)
                {
                case dbSelectAction.SELECT_ROLES:
                    result.objects = dbHandle.GetRoles();
                    break;

                case dbSelectAction.SELECT_CHAT_TYPES:
                    result.objects = dbHandle.GetChatTypes();
                    break;

                case dbSelectAction.SELECT_ALL_USERS:
                    result.objects = dbHandle.getAllUsers(request.currentUser);
                    break;
                }
                switch (request.action)
                {
                case dbAction.ADD_USER:
                    User newUser = (User)request.entity;
                    if (dbHandle.addUser(request.currentUser, newUser))
                    {
                        result.objects = new Entity[] { newUser };
                    }
                    break;

                case dbAction.VERIFY_USER:
                    User user = (User)request.entity;
                    user                = dbHandle.fillUserInfo(user.login, user.password);
                    result.objects      = new Entity[] { user };
                    result.isSuccessful = true;
                    break;
                }
                result.isSuccessful = true;
                Console.WriteLine("Отправлен результат " + result.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result.message = e.Message;
                result.objects = null;
            }
            finally
            {
            }
            return(result);
        }
        public void sendRequest(dbRequest request)
        {
            byte[]       bytes      = DbObject.Serialize(request);
            MemoryStream stream     = new MemoryStream(bytes);
            int          totalBytes = 0;

            Console.WriteLine("need to send " + bytes.Length + " bytes");
            while (stream.Position < stream.Length)
            {
                byte[] buffer    = new byte[socket.ReceiveBufferSize];
                int    byteCount = stream.Read(buffer, 0, socket.ReceiveBufferSize);
                totalBytes += byteCount;
                this.socket.Send(buffer, byteCount, SocketFlags.None);
            }
            Console.WriteLine("sent " + totalBytes + " bytes");
            stream.Close();
        }
        public void requestRoles()
        {
            dbRequest request = new dbRequest(dbAction.NONE, dbSelectAction.SELECT_ROLES, null, null);

            sendRequest(request);
        }
        public void signUpUser(User newUser)
        {
            dbRequest request = new dbRequest(dbAction.ADD_USER, dbSelectAction.NONE, currentUser, newUser);

            sendRequest(request);
        }
        public void getAllUsers()
        {
            dbRequest request = new dbRequest(dbAction.NONE, dbSelectAction.SELECT_ALL_USERS, currentUser, null);

            sendRequest(request);
        }
        public void setCurrentUser(User newUser)
        {
            dbRequest request = new dbRequest(dbAction.VERIFY_USER, dbSelectAction.NONE, null, newUser);

            sendRequest(request);
        }