查看: 3106|回复: 3
打印 上一主题 下一主题

[其他] 服务器开发第二章 连接对象定义

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-6-30 16:00:12 |只看该作者 |倒序浏览

包含成员socket 连接客户端的socket对象
message 此连接当前接收消息对象
messageCache 接收消息缓存 在第三季中因为考虑到客户端发送的消息一般不会很大 所以可有可无 没有实际意义 此对象是考虑粘包的情况下 进行消息缓存 以便解析

包含方法
write 用于给此连接发送消息
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;

  7. namespace GameServer.NetModel
  8. {
  9.     class Session
  10.     {
  11.         public Socket socket { get; set; }
  12.         public byte[] message { get; set; }
  13. <pre class="brush:csharp;auto-links:false;"><span class="Apple-tab-span" style="white-space:pre">        </span>public List<byte> messageCache { get; set; }</pre>
  14.         public Session() {
  15.             message = new byte[1024 * 4];
  16.         }
  17.         public void write(int type,int area,int command,Object message) {
  18.             SocketModel model = new SocketModel(type, area, command, null);
  19.             if (message != null)
  20.             {
  21.                model.Message= coding.Coding<Object>.encode(message);
  22.             }
  23.             int length = 16;
  24.             if (model.Message != null)
  25.             {
  26.                 length += model.Message.Length;
  27.             }
  28.             ByteArray arr = new ByteArray();
  29.             arr.WriteInt(length);
  30.             arr.WriteInt(model.Type);
  31.             arr.WriteInt(model.Area);
  32.             arr.WriteInt(model.Command);
  33.             
  34.             if (model.Message != null)
  35.             {
  36.                 length += model.Message.Length;
  37.                 arr.WriteInt(model.Message.Length);
  38.                 arr.WriteUTFBytes(model.Message);
  39.             }
  40.             else {
  41.                 arr.WriteInt(0);
  42.             }
  43.             socket.Send(arr.Buffer);
  44.         }
  45.     }
  46. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

115

主题

3

听众

5676

积分

高级设计师

Rank: 6Rank: 6

纳金币
7268
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-6-30 17:26:34 |只看该作者
THanks for sharing !
回复

使用道具 举报

wucnj    

1

主题

1

听众

3160

积分

中级设计师

Rank: 5Rank: 5

纳金币
1065
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-7-1 10:45:18 |只看该作者
感谢分享!!!
回复

使用道具 举报

0

主题

1

听众

823

积分

初级设计师

Rank: 3Rank: 3

纳金币
80
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2014-7-1 12:45:01 |只看该作者
感谢分享!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-2 14:08 , Processed in 0.086873 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部