Exemplo n.º 1
0
        internal bool OnHandleRequest(Socket client, string username, string path, NameValueCollection query, int range)
        {
            int session = 0;
            if (query["session-id"] != null) {
                session = Int32.Parse (query["session-id"]);
            }

            if (!sessions.ContainsKey (session) && path != "/server-info" && path != "/content-codes" &&
                path != "/login") {
                ws.WriteResponse (client, HttpStatusCode.Forbidden, "invalid session id");
                return true;
            }

            if (session != 0) {
                sessions[session].LastActionTime = DateTime.Now;
            }

            int clientRev = 0;
            if (query["revision-number"] != null) {
                clientRev = Int32.Parse (query["revision-number"]);
            }

            int delta = 0;
            if (query["delta"] != null) {
                delta = Int32.Parse (query["delta"]);
            }

            if (path == "/server-info") {
                ws.WriteResponse (client, GetServerInfoNode ());
            } else if (path == "/content-codes") {
                ws.WriteResponse (client, ContentCodeBag.Default.ToNode ());
            } else if (path == "/login") {
                ExpireSessions ();

                if (maxUsers > 0 && sessions.Count + 1 > maxUsers) {
                    ws.WriteResponse (client, HttpStatusCode.ServiceUnavailable, "too many users");
                    return true;
                }

                session = random.Next ();
                User user = new User (DateTime.Now, (client.RemoteEndPoint as IPEndPoint).Address, username);

                lock (sessions) {
                    sessions[session] = user;
                }

                ws.WriteResponse (client,
                    new ContentNode ("dmap.loginresponse",
                        new ContentNode ("dmap.status", 200),
                        new ContentNode ("dmap.sessionid", session))
                );
                OnUserLogin (user);
            } else if (path == "/logout") {
                User user = sessions[session];

                lock (sessions) {
                    sessions.Remove (session);
                }

                ws.WriteOk (client);
                OnUserLogout (user);

                return false;
            } else if (HandleRequest (client, username, path, query, range, delta, clientRev)) {
                return true;
            } else {
                ws.WriteResponse (client, HttpStatusCode.Forbidden, "GO AWAY");
            }

            return true;
        }
Exemplo n.º 2
0
 private void OnUserLogout(User user)
 {
     UserHandler handler = UserLogout;
     if (handler != null) {
         try {
             handler (this, new UserArgs (user));
         } catch (Exception e) {
             Console.Error.WriteLine ("Exception in UserLogout event handler: " + e);
         }
     }
 }
Exemplo n.º 3
0
 public UserArgs(User user)
 {
     this.user = user;
 }