[原创]C# UDP穿越NAT,UDP打洞,UDP Hole Punching源代码(2)

  [原创]C# UDP穿越NAT,UDP打洞,UDP Hole Punching源代码(2)

[原创]C# UDP穿越NAT,UDP打洞,UDP Hole Punching源代码(2)



续: [原创]C# UDP穿越NAT,UDP打洞,UDP Hole Punching源代码(1)


本帖包括公共代码及用于转发命令代码


5.公共代码

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
using System.Collections;

namespace vjsdn.net.library
{
   /// <summary>
   /// 显示跟踪消息的事件委托
   /// </summary>
   public delegate void WriteLogHandle(string msg);
   
   /// <summary>
   /// 刷新在线用户的事件委托
   /// </summary>
   public delegate void UserChangedHandle(UserCollection users);
   
   public class Globals
   {
      /// <summary>
      /// 服务器侦听端口
      /// </summary>
      public const int SERVER_PORT = 21134;
      
      /// <summary>
      /// 本地侦听端口
      /// </summary>
      public const int LOCAL_PORT = 19786;
   }
   
   /// <summary>
   /// User 的摘要说明。
   /// </summary>
   [Serializable]
   public class User
   {
      protected string _userName;
      protected IPEndPoint _netPoint;
      protected bool _conntected;
      
      public User(string UserName, IPEndPoint NetPoint)
      {
         this._userName = UserName;
         this._netPoint = NetPoint;
      }
      
      public string UserName
      {
         get { return _userName; }
      }
      
      public IPEndPoint NetPoint
      {
         get { return _netPoint; }
         set { _netPoint = value; }
      }
      
      public bool IsConnected //打洞标记
      {
         get { return _conntected; }
         set { _conntected = value; }
      }
      
      public string FullName { get { return this.ToString(); } }
      
      public override string ToString()
      {
         return _userName + "- [" + _netPoint.Address.ToString() + ":" + _netPoint.Port.ToString() + "] " + (_conntected ? "Y" : "N");
      }
   }
   
   /// <summary>
   /// 在线用户列表
   /// </summary>
   [Serializable]
   public class UserCollection : CollectionBase
   {
      public void Add(User user)
      {
         InnerList.Add(user);
      }
      
      public void Remove(User user)
      {
         InnerList.Remove(user);
      }
      
      public User this[int index]
      {
         get { return (User)InnerList[index]; }
      }
      
      public User Find(string userName)
      {
         foreach (User user in this)
         {
            if (string.Compare(userName, user.UserName, true) == 0)
            {
               return user;
            }
         }
         return null;
      }
   }
   
   /// <summary>
   /// 序列化反序列化对象
   /// </summary>
   public class ObjectSerializer
   {
      public static byte[] Serialize(object obj)
      {
         BinaryFormatter binaryF = new BinaryFormatter();
         MemoryStream ms = new MemoryStream(1024 * 10);
         binaryF.Serialize(ms, obj);
         ms.Seek(0, SeekOrigin.Begin);
         byte[] buffer = new byte[(int)ms.Length];
         ms.Read(buffer, 0, buffer.Length);
         ms.Close();
         return buffer;
      }
      
      public static object Deserialize(byte[] buffer)
      {
         BinaryFormatter binaryF = new BinaryFormatter();
         MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length, false);
         object obj = binaryF.Deserialize(ms);
         ms.Close();
         return obj;
      }
   }
}

来源:C/S框架网(www.csframework.com) QQ:1980854898



6.消息类(转发命令)

6.1 消息基类.所有命令的基类
/// <summary>
/// 消息基类,抽象类
/// </summary>
[Serializable]
public abstract class MessageBase
{
   //消息基类
}



6.2 Client to Server命令
#region 客户端发送到服务器的消息

/// <summary>
/// 客户端发送到服务器的消息基类
/// </summary>
[Serializable]
public abstract class C2S_MessageBase : MessageBase
{
   private string _fromUserName;
   
   protected C2S_MessageBase(string fromUserName)
   {
      _fromUserName = fromUserName;
   }
   
   public string FromUserName
   {
      get { return _fromUserName; }
   }
}

/// <summary>
/// 用户登录消息
/// </summary>
[Serializable]
public class C2S_LoginMessage : C2S_MessageBase
{
   private string _password;
   
   public C2S_LoginMessage(string userName, string password)
   : base(userName)
   {
      this._password = password;
   }
   
   public string Password
   {
      get { return _password; }
   }
}

/// <summary>
/// 用户登出消息
/// </summary>
[Serializable]
public class C2S_LogoutMessage : C2S_MessageBase
{
   
   public C2S_LogoutMessage(string userName)
   : base(userName)
   { }
}

/// <summary>
/// 请求用户列表消息
/// </summary>
[Serializable]
public class C2S_GetUsersMessage : C2S_MessageBase
{
   public C2S_GetUsersMessage(string userName)
   : base(userName)
   { }
}

/// <summary>
/// 请求Purch Hole消息
/// </summary>
[Serializable]
public class C2S_HolePunchingRequestMessage : C2S_MessageBase
{
   protected string toUserName;
   
   public C2S_HolePunchingRequestMessage(string fromUserName, string toUserName)
   : base(fromUserName)
   {
      this.toUserName = toUserName;
   }
   
   public string ToUserName
   {
      get { return this.toUserName; }
   }
}

#endregion

来源:C/S框架网(www.csframework.com) QQ:1980854898


6.3 Server to Client命令
#region 服务器发送到客户端消息

/// <summary>
/// 服务器发送到客户端消息基类
/// </summary>
[Serializable]
public abstract class S2C_MessageBase : MessageBase
{
}

/// <summary>
/// 请求用户列表应答消息
/// </summary>
[Serializable]
public class S2C_UserListMessage : S2C_MessageBase
{
   private UserCollection userList;
   
   public S2C_UserListMessage(UserCollection users)
   {
      this.userList = users;
   }
   
   public UserCollection UserList
   {
      get { return userList; }
   }
}

/// <summary>
/// 转发请求Purch Hole消息
/// </summary>
[Serializable]
public class S2C_HolePunchingMessage : S2C_MessageBase
{
   protected System.Net.IPEndPoint _remotePoint;
   
   public S2C_HolePunchingMessage(System.Net.IPEndPoint point)
   {
      this._remotePoint = point;
   }
   
   public System.Net.IPEndPoint RemotePoint
   {
      get { return _remotePoint; }
   }
}

/// <summary>
/// 服务器通知所有在线用户,
/// </summary>
[Serializable]
public class S2C_UserAction : S2C_MessageBase
{
   protected User _User;
   protected UserAction _Action;
   
   public S2C_UserAction(User user, UserAction action)
   {
      _User = user;
      _Action = action;
   }
   
   public User User
   {
      get { return _User; }
   }
   
   public UserAction Action
   {
      get { return _Action; }
   }
}

#endregion

来源:C/S框架网(www.csframework.com) QQ:1980854898


6.4 Client to Client(Peer to Peer P2P)命令

#region 点对点消息

/// <summary>
/// 点对点消息基类
/// </summary>
[Serializable]
public abstract class P2P_MessageBase : MessageBase
{
   //
}

/// <summary>
/// 聊天消息
/// </summary>
[Serializable]
public class P2P_TalkMessage : P2P_MessageBase
{
   private string message;
   
   public P2P_TalkMessage(string msg)
   {
      message = msg;
   }
   
   public string Message
   {
      get { return message; }
   }
}

/// <summary>
/// UDP打洞测试消息
/// </summary>
[Serializable]
public class P2P_HolePunchingTestMessage : P2P_MessageBase
{
   private string _UserName;
   
   public P2P_HolePunchingTestMessage(string userName)
   {
      _UserName = userName;
   }
   
   public string UserName
   {
      get { return _UserName; }
   }
}

/// <summary>
/// 收到消息的回复确认
/// 如A与B想建立通话通道,些命令由B发出确认打洞成功
/// </summary>
[Serializable]
public class P2P_HolePunchingResponse : P2P_MessageBase
{
   private string _UserName;
   
   public P2P_HolePunchingResponse(string userName)
   {
      _UserName = userName;
   }
   
   public string UserName
   {
      get { return _UserName; }
   }
}

#endregion

来源:C/S框架网(www.csframework.com) QQ:1980854898



6.5 定义用户的动作

/// <summary>
/// 用户动作
/// </summary>
public enum UserAction
{
   Login,
   Logout

.... more action
}




C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
发表评论

评论内容
昵称:
关联文章

[]C# UDP穿越NAT,UDP,UDP Hole Punching源代码(2)
[]C# UDP穿越NAT,UDP,UDP Hole Punching源代码(1)
UDP(UDP Hole Punching)原理
C#实现UDP穿透NAT(UDP)完整版()
C++实现的NAT技术(C++ NAT Hole Puching)
[] Asp.Net三层体系结构应用实例(2)源代码
[]C#键盘勾子(Hook),屏蔽键盘活动.(源代码下载)
:CodeHighlighter源代码格式化,代码缩进,关键词高亮着色(C#源码)
ERP系统开发平台|基于C#.NET造的C/S系统快速开发框架
[]C#仿QQ聊天程序(点对点无服务端, P2P)
[]无线监控系统之二-------C#实现(续)
.NET快速开发框架|C/S框架网
[]C# Access 模糊查询SQL语句
C#.Net B/S简单框架结构示例[]
C#.Net局域网版本自动升级解决方案()
使用DOC2CHM工具制作CHM帮助文件图解()
C#源代码安全缺陷与提高源代码质量解决方案
C#多文档界面(MDI)系统框架 (C/S框架网!)
.Net后端框架|WebApi服务端开发框架|C/S框架网作品
C#开发框架|C#开源框架|C/S框架网