?? serverpush.cs
字號:
/*
Copyright 2006 Christian Gross http://www.devspace.com
From the book Ajax Patterns and Best Practices
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Web;
using System.Collections.Generic;
using System.IO;
using Devspace.Commons.Tracer;
using System.Collections;
namespace Devspace.Ajax.PersistentCommunications {
class UserState {
private String _userIdentifier;
private String _data;
private VersionTracker _version;
public UserState(String userIdentifier, String data, VersionTracker version) {
_userIdentifier = userIdentifier;
_data = data;
_version = version;
}
public string UserIdentifier {
get {
return _userIdentifier;
}
}
public string Data {
get {
return _data;
}
set {
_data = value;
}
}
public VersionTracker Version {
get {
return _version;
}
}
}
public class GlobalServerPush : RedirectionHttpHandlerBase {
static VersionTracker _version = new VersionTracker( 10000);
static Hashtable _users = new Hashtable();
private const string constXStateHeader = "X-Last-State";
private const string constUseAction = "user-action";
public GlobalServerPush() {
AssignUserResolver(new UserResolverImplementation());
}
protected internal override string BaseURL {
get {
return "/persistentcommunications/globalserverpush";
}
}
protected internal override string GetURLEnding() {
return _identifiedUser.Identifier;
}
bool CanUserAccessResource(HttpRequest req) {
string resource = req.Path.Substring(this.BaseURL.Length + 1, req.Path.Length - (this.BaseURL.Length + 1));
GenerateOutput.Write("GlobalServerPush.CanUserAccessResource",
"Testing if user (" + _identifiedUser.Identifier + ") can access resource (" + resource + ")");
return(resource == _identifiedUser.Identifier);
}
protected internal override void DoGet(HttpRequest req, HttpResponse resp) {
if (CanUserAccessResource(req)) {
string lastState = req.Headers.Get(constXStateHeader);
if (lastState == null) {
lastState = req.Params.Get(constXStateHeader);
}
GenerateOutput.Write("GlobalServerPush.DoGet", "lastState (" + lastState + ")");
int lastCount = 0;
if (lastState != null) {
lastCount = int.Parse(lastState);
}
string resource = req.Path.Substring(this.BaseURL.Length + 1, req.Path.Length - (this.BaseURL.Length + 1));
UserState state = null;
lock (_users) {
if (_users.Contains(resource)) {
state = _users[resource] as UserState;
}
}
if (state == null) {
resp.StatusDescription = "User does not have a state";
resp.StatusCode = 409;
GenerateOutput.Write("GlobalServerPush.DoGet", "User does not have a state");
return;
}
if (state.Version.GetNew(lastCount)) {
lock (state) {
TextWriter output = resp.Output;
output.WriteLine("Content (" + state.Data + ") Version (" +
state.Version.VersionNumber + ")");
}
resp.AddHeader(constXStateHeader, state.Version.VersionNumber.ToString());
GenerateOutput.Write("GlobalServerPush.DoGet", "Writing something changed");
}
else {
resp.StatusDescription = "No change";
resp.StatusCode = 408;
GenerateOutput.Write("GlobalServerPush.DoGet", "Writing nothing changed");
}
}
else {
GenerateOutput.Write("GlobalServerPush.DoPost", "Cannot access resource");
}
}
protected internal override void DoPost(HttpRequest req, HttpResponse resp) {
if (CanUserAccessResource(req)) {
GenerateOutput.Write("GlobalServerPush.DoPost", "Writing the resource state");
Stream inpStream = req.InputStream;
byte[] toReadBuffer = new byte[inpStream.Length];
if (inpStream.Read(toReadBuffer, 0, toReadBuffer.Length) > 0) {
string buffer = System.Text.Encoding.ASCII.GetString(toReadBuffer);
string resource = req.Path.Substring(this.BaseURL.Length + 1, req.Path.Length - (this.BaseURL.Length + 1));
UserState state = null;
lock (_users) {
if (_users.Contains(resource)) {
state = _users[resource] as UserState;
}
else {
state = new UserState(_identifiedUser.Identifier, buffer, new VersionTracker(10000));
_users.Add(resource, state);
}
state.Data = buffer;
}
state.Version.Increment();
}
}
else {
GenerateOutput.Write("GlobalServerPush.DoPost", "Cannot access resource");
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -