随笔 - 9  文章 - 0 评论 - 29 trackbacks - 0

摘要: XNA是微软推出的一个基于DirectX的.net平台游戏开发框架,用来开发XBOX和Windows桌面的游戏,后被移植到了silverlight和windows phone上。内部是一个game loop,不同于事件驱动模式,但也不难掌握,性能方面还是不错的。下面就用XNA创建一个简单的局域网游戏。网络连接无外乎就是那么几个阶段,连接-发送、接收数据-断开。用到的几个主要的类是: using Microsoft.Xna.Framework.Net; //网络连接会话类 NetworkSession networkSession; //...阅读全文
posted @ 2011-12-21 20:25 leslie_ 阅读(683) 评论(1) 编辑

什么是SOPA SOPA的危害

SOPA: 禁止网络盗版法案,即“Stop Online Piracy Act”的 简称。

美国国会审议的能够使互联网用户的生活更加复杂的法案,该法案规定封锁刊登没有专利权的内容的网站。而且,这些网站在哪里注册,在美国还是国外并不重要。在该法案下,有了法庭

的允许,美国司法部长能够要求搜索引擎,ISP公司以及在线广告以及支付公司停止与那些国外的侵犯版权的网站合作,包括从网站链接以及该网站的IP地址来封锁该网站的名字。

支持这项法案的主要是好莱坞和一些大唱片公司。

反对方则包括AOL, eBay, Facebook, Google, LinkedIn, Mozilla, Twitter, Yahoo,Zynga等一些互联网公司

SOPA的制定主要是为了遏止网络盗版,就是当网站内有盗版内容、或是有助长盗版风气的内容,该网站可以合法的被版 权所有 人监控、审查,甚至版权所有者可以透过程序、让该网站在美国境内遭到封锁。此外,搜索引擎也有义务需要配合政府将相关链接排除在搜索结果之外。以一般 Web 2.0 网站而言,由使用者产生的大量内容(User Generated Content),像Facebook的动态讯息、相簿内容等也都必须受到监控、审查,也可能会成为网站被封锁的原因。在这样模糊的定义下,几乎所有我们 日常生活当中用到的网站都会受到波及,这也是遭遇互联网公司反对的主要原因。

SOPA法在美国互联网届引起轩然大波。是否这个时代全面收紧对互联网的控制是大势所趋呢?

image

image

posted @ 2012-01-19 10:19 leslie_ 阅读(60) 评论(0) 编辑

      XNA是微软推出的一个基于DirectX的.net平台游戏开发框架,用来开发XBOX和Windows桌面的游戏,后被移植到了silverlight和windows phone上。内部是一个game loop,不同于事件驱动模式,但也不难掌握,性能方面还是不错的。下面就用XNA创建一个简单的局域网游戏。

网络连接无外乎就是那么几个阶段,连接-发送、接收数据-断开。

用到的几个主要的类是:

        using Microsoft.Xna.Framework.Net;
//网络连接会话类
NetworkSession networkSession;
//发送packet的类
PacketWriter packetWriter = new PacketWriter();
//读取packet的类
PacketReader packetReader = new PacketReader();

游戏开始需要有一个人来创建一个session,然后有人来加入到你创建的游戏当中来。

创建游戏session:

            // 创建一个session,最大人数2
networkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 1, 2);
networkSession.AllowHostMigration = true;
networkSession.AllowJoinInProgress = false;
//绑定join和left时间
networkSession.GamerJoined += GamerJoined;
networkSession.GamerLeft += GamerLeft;

void GamerJoined(object sender, GamerJoinedEventArgs e)
{
//tag是一个object类型的容器,用来保存用户自定义的信息。
e.Gamer.Tag = new entity;
}

void GamerLeft(object sender, GamerLeftEventArgs e)
{
networkSession.Dispose();
networkSession = null;
}

加入游戏:

            // 查找是否存在游戏session
AvailableNetworkSessionCollection sessions =
NetworkSession.Find(NetworkSessionType.SystemLink, 1, null);
if (sessions.Count > 0)
{
//如果session存在,就加入到这个session中
networkSession = NetworkSession.Join(sessions[0]);
//绑定join和left事件,同上
networkSession.GamerJoined += GamerJoined;
networkSession.GamerLeft += GamerLeft;
}

加入到游戏session之后就开是玩家之间进行数据的传输和接收了。

        protected void UpdateLocalPlayer(GameTime gameTime)
{
// 获取本地玩家
LocalNetworkGamer localGamer = networkSession.LocalGamers[0];

// 获取上面绑定到tag中的entity
entity sprite = (entity )localGamer.Tag;

// 写入数据到packetWriter中
packetWriter.Write((int)1);
packetWriter.Write(Vector.Zero);

// 发送数据
localGamer.SendData(packetWriter, SendDataOptions.InOrder);

}
        protected void ProcessIncomingData(GameTime gameTime)
{
// 获取玩家
LocalNetworkGamer localGamer = networkSession.LocalGamers[0];

//是否有数据可读取
while (localGamer.IsDataAvailable)
{
//接收数据
NetworkGamer sender;
localGamer.ReceiveData(packetReader, out sender);

//忽略到自己发送的数据
if (!sender.IsLocal)
{
//读取数据到packetReader中
MessageType messageType = (int)packetReader.ReadInt32();
}
foreach (NetworkGamer gamer in networkSession.AllGamers)
{
if (!gamer.IsLocal)
{
//获取绑定到tag中的实体
entity _e= ((entity)gamer.Tag);

//读取packetReader中的数据
Vector2 Pos = packetReader.ReadVector2();
}

//add some game logic here
}
}
}

在接收到数据之后就可以在Update方法中写游戏逻辑,最后Draw方法也很简单。

            // 循环所有游戏玩家
foreach (NetworkGamer gamer in networkSession.AllGamers)
{
//调用entity中的draw方法
entity sprite = ((entity)gamer.Tag);
sprite.Draw(gameTime, spriteBatch);
}

代码都很简单,一看就懂,感兴趣的朋友可以自己去找些资料来看。






posted @ 2011-12-21 20:25 leslie_ 阅读(683) 评论(1) 编辑

最近的一个项目中在数据交互用的是ado.net,今天在做了一些修改之后跑出了两个错误,分别是:
1、Invalid attempt to Read when reader is closed
2、There is already an open DataReader associated with this Connection which must be closed first.

乍看都是和数据库的连接有关的问题,第一个说数据库的连接已经关闭,另一个说数据库的连接已经打开。

看代码分析了一下,发现错误原因是来自一个修饰符,static 。

静态数据成员只存储一处,供所有对象共用。静态数据成员的值对每个对象都是一样,但它的值是可以更新的。只要对静态数据成员的值更新一次,保证所有对象存取更新后的相同的值,这样可以提高时间效率。

产生错误的地方主要有两个地方:

            using (MySqlDataReader dr1 = MySqlHelper.getData(id1))
            {
                 
                   using(MySqlDataReader dr2 =  MySqlHelper.getData2(id2))
                    {

                    }

             }
            using (MySqlDataReader dr1 = MySqlHelper.getData(id1))
            {
                 
                   using(object obj =  MySqlHelper.ExecuteScalar(id2))
                    {

                    }

             }

原因是在MySqlHelper中将连接字符串定义成了static。

    public static MySqlConnection getConnection()
    {
        MySqlConnection connection = new MySqlConnection(connString);
       
        return connection;
    }

1中由于嵌套内的datareader关闭了连接,影响了外面的datareader。会返回Invalid attempt to Read when reader is closed这个错误。

2中由于外层的datareader一直处在连接打开中,重新打开会报错,There is already an open DataReader associated with this Connection which must be closed first.

原因我想是:静态数据成员只存储一处,供所有对象共用。

修改了一下MySqlHelper的连接,

using(MySqlConnection conn = new MySqlConnection(connString))

恢复正常!



posted @ 2011-07-02 19:40 leslie_ 阅读(135) 评论(0) 编辑
摘要: Ruminations on the Psychology and Aesthetics of CodingBy Charles PetzoldA Talk Delivered at the NYC .NET Developer’s Group,October 20, 2005Abstract:Visual Studio can be one of the programmer's best friends, but over the years it has become increasingly pushy, domineering, and suffering from unse阅读全文
posted @ 2011-03-31 09:33 leslie_ 阅读(77) 评论(0) 编辑

      上个月做了一个关于人人网api数据的小东西。发现人人网的api做的不是一般的烂啊,前几天又看了一下,似乎更新了文档,又开放了些接口。现在就说一个在其中遇到的问题和大家分享一下。

问题是,在取用户数据的时候要求传一个session_key,这个session_key是根据access_token的值得到的,而且有过期时间。(具体方法点这里)。

access_token格式是这样的:

 "access_token": "127089|5.a24b8385f555445d3898cae50b65f3ef.86400.1295031600-222209506",

 "expires_in": 88913//过期时间

}
      根据人人网OAuth2.0的要求是,必须通过access_token才能得到session_key,但在实际应用过程中发现access_token中红色的部分就是session_key。那事情就简单了。
解决问题的方法是我们只要模拟人人网的登录就能够得到传说中新的session_key了。

先通过Firebug检测一下这个登录过程如下图:

 

Fiddler:

POST的表单内容:

整个过程经历了1次POST和3次GET的webrequest,所以我们只要模拟这些操作就可以刷新我们的session_key了。据体的方法在下面,自己应该能看懂。

1 /// <summary>
2 /// 刷新access_token
3 /// </summary>
4 /// <returns></returns>
5 public static string RefreshToken()
6 {
7 HttpWebRequest httpRequest = null;
8 HttpWebResponse httpResponse = null;
9 string gethost = string.Empty;
10 string Cookiesstr = string.Empty;
11 CookieContainer cookie = new CookieContainer();
12
13 //第一次POST
14 try
15 {
16 byte[] data = Encoding.UTF8.GetBytes("email={你的邮箱}&password={你的密码}&domain=renren.com&origURL=http%3A%2F%2Fgraph.renren.com%2Foauth%2Fauthorize%3Fclient_id%3Dd743d5f993e34a30ac933d972b1e88df%26response_type%3Dtoken%26redirect_uri%3Dhttp%3A%2F%2Fgraph.renren.com%2Foauth%2Flogin_success.html%26pp%3D1%26https%3Dhttps%3A%2F%2Fgraph.renren.com");
17
18 Uri code = new Uri("http://passport.renren.com/RL.do?P3P=1");
19
20 httpRequest = (HttpWebRequest)WebRequest.Create(code);
21 httpRequest.Method = "POST";
22 httpRequest.ContentType = "application/x-www-form-urlencoded";
23 httpRequest.ContentLength = data.Length;
24 httpRequest.KeepAlive = true;
25 httpRequest.AllowAutoRedirect = false;
26 httpRequest.CookieContainer = cookie;
27 using (Stream newStream = httpRequest.GetRequestStream())
28 newStream.Write(data, 0, data.Length);
29
30 httpResponse = (HttpWebResponse)httpRequest.GetResponse();
31 httpResponse.Cookies = httpRequest.CookieContainer.GetCookies(httpRequest.RequestUri);
32 CookieCollection cook = httpResponse.Cookies;
33 Cookiesstr = httpRequest.CookieContainer.GetCookieHeader(httpRequest.RequestUri);
34
35 string backstr = string.Empty;
36 using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8))
37 backstr = sr.ReadToEnd();
38 string[] substr = backstr.Split(new char[] { '"' });
39 gethost = substr[1];
40
41 httpRequest.Abort();
42 httpResponse.Close();
43 }
44 catch (Exception)
45 { }
46
47 //第一次GET
48
49 try
50 {
51 string ss;
52 httpRequest = (HttpWebRequest)WebRequest.Create(gethost);
53 httpRequest.Method = "GET";
54 httpRequest.KeepAlive = true;
55 httpRequest.Headers.Add("Cookie:" + Cookiesstr);
56 httpRequest.CookieContainer = cookie;
57 httpRequest.AllowAutoRedirect = false;
58 httpResponse = (HttpWebResponse)httpRequest.GetResponse();
59 Cookiesstr = httpRequest.CookieContainer.GetCookieHeader(httpRequest.RequestUri);
60 using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
61 ss = sr.ReadToEnd();
62 string[] substr = ss.Split(new char[] { '"' });
63 gethost = substr[1];
64 httpRequest.Abort();
65 httpResponse.Close();
66 }
67 catch (Exception)
68 { }
69
70 //第二次GET
71 try
72 {
73 string ss;
74 httpRequest = (HttpWebRequest)WebRequest.Create(gethost);
75 httpRequest.Method = "GET";
76 httpRequest.KeepAlive = true;
77 httpRequest.Headers.Add("Cookie:" + Cookiesstr);
78 httpRequest.CookieContainer = cookie;
79 httpRequest.AllowAutoRedirect = false;
80 httpResponse = (HttpWebResponse)httpRequest.GetResponse();
81 Cookiesstr = httpRequest.CookieContainer.GetCookieHeader(httpRequest.RequestUri);
82 using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
83 ss = sr.ReadToEnd();
84 string[] substr = ss.Split(new char[] { '"' });
85 gethost = substr[1];
86 httpRequest.Abort();
87 httpResponse.Close();
88 }
89 catch (Exception)
90 { }
91
92 //第三次GET
93 try
94 {
95 string ss;
96 httpRequest = (HttpWebRequest)WebRequest.Create(gethost);
97 httpRequest.Method = "GET";
98 httpRequest.KeepAlive = true;
99 httpRequest.Headers.Add("Cookie:" + Cookiesstr);
100 httpRequest.CookieContainer = cookie;
101 httpRequest.AllowAutoRedirect = false;
102 httpResponse = (HttpWebResponse)httpRequest.GetResponse();
103 Cookiesstr = httpRequest.CookieContainer.GetCookieHeader(httpRequest.RequestUri);
104 using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
105 ss = sr.ReadToEnd();
106 string[] substr = ss.Split(new char[] { '"' });
107 gethost = substr[1];
108 httpRequest.Abort();
109 httpResponse.Close();
110 }
111 catch (Exception)
112 { }
113
114 return gethost.Substring(71, 61);
115 }

     希望对大家有所帮助。

posted @ 2011-03-25 17:01 leslie_ 阅读(3110) 评论(9) 编辑
摘要: 1阅读全文
posted @ 2010-10-04 15:37 leslie_ 阅读(146) 评论(0) 编辑
摘要: 。阅读全文
posted @ 2010-10-02 08:16 leslie_ 阅读(242) 评论(0) 编辑
C#
摘要: k阅读全文
posted @ 2010-09-30 10:36 leslie_ 阅读(84) 评论(0) 编辑
摘要: 一起开始学习之旅吧~~先报个到!!阅读全文
posted @ 2010-09-29 21:24 leslie_ 阅读(30) 评论(0) 编辑
仅列出标题