Esempio n. 1
0
 private void AcceptIncomingConnections()
 {
     while (true)
     {
         SocketHandler   SocketHandler     = new SocketHandler(listenSocket.Accept());
         RequestReceiver client            = new RequestReceiver(SocketHandler);
         Thread          currentUserThread = new Thread(client.StartReceiving);
         currentUserThread.Start();
         Clients.Add(SocketHandler, ClientStatus.GetInstance(currentUserThread.ManagedThreadId.ToString()));
     }
 }
Esempio n. 2
0
        public override Response Handle(Request request)
        {
            ClientStatus status = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString());
            Chat         chat   = new Chat(int.Parse(request.Data["NewChatId"]));

            if (!UserIsInChat(chat))
            {
                return(ResponseError.DefineError(request, ResponseErrorType.InvalidChatError));
            }
            else
            {
                status.Chat = chat;
                return(new Response(ResponseStatus.Success, onRequest: request.Type));
            }
        }
Esempio n. 3
0
 public override Response Handle(Request request)
 {
     try
     {
         string user_id = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()).User.Id.ToString();
         db.Execute(String.Format("INSERT INTO chats (title, creator,creation_date) VALUES ('{0}','{1}',now())", request.Data["Title"], user_id));
         List <List <string> > chat = db.Execute(String.Format("SELECT id, title FROM chats WHERE title = '{0}' and creator = '{1}' ORDER BY id DESC LIMIT 1",
                                                               request.Data["Title"],
                                                               user_id));
         db.Execute(String.Format("INSERT INTO userchat (user_id,chat_id) VALUES ('{0}','{1}')", user_id, chat[0][0]));
         return(new Response(status: ResponseStatus.Success, onRequest: request.Type, data: new Dictionary <string, string>
         {
             ["Chat"] = JsonConvert.SerializeObject(chat[0])
         }));
     }
     catch (MySqlException)
     {
         return(ResponseError.DefineError(request, ResponseErrorType.DatabaseAccessError));
     }
 }
Esempio n. 4
0
        public override Response Handle(Request request)
        {
            Chat         Chat   = new Chat(int.Parse(request.Data["Chat"]));
            ClientStatus status = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString());

            if (!UserIsInChat(Chat))
            {
                return(ResponseError.DefineError(request, ResponseErrorType.InvalidChatError));
            }
            else
            {
                Dictionary <string, string> ResponseData = new Dictionary <string, string>
                {
                    ["Author"]  = status.User.Username,
                    ["Message"] = request.Data["Message"]
                };

                foreach (KeyValuePair <SocketHandler, ClientStatus> ClientData in Server.Clients)
                {
                    if (ClientData.Value.User.IsParticipant(Chat) && ClientData.Value.Chat != null && ClientData.Value.Chat.Id == Chat.Id)
                    {
                        Console.WriteLine("SENDED TO {0}", ClientData.Value.User.Username);
                        ClientData.Key.Send(new Response(status: ResponseStatus.Success, onRequest: RequestType.SpreadMessage, data: ResponseData));
                    }
                }
                try
                {
                    Chat.AddMessage(status.User, request.Data["Message"]);
                }
                catch (MySqlException)
                {
                    return(ResponseError.DefineError(request, ResponseErrorType.DatabaseAccessError));
                }
                return(new Response(ResponseStatus.Success, onRequest: request.Type));
            }
        }
Esempio n. 5
0
 public void Dispose()
 {
     ClientStatus.RemoveInstance(Thread.CurrentThread.ManagedThreadId.ToString());
     Server.Clients.Remove(this);
     socket.Close();
 }
Esempio n. 6
0
 private void Disconnect()
 {
     ClientStatus.RemoveInstance(Thread.CurrentThread.ManagedThreadId.ToString());
     Server.Clients.Remove(SocketHandler);
     SocketHandler.Close();
 }
Esempio n. 7
0
 protected bool UserIsInChat(Chat chat)
 {
     return(ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()).User.GetChats().Find(_chat => _chat.Id == chat.Id) != null);
 }