[原创]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.公共代码
6.消息类(转发命令)
6.1 消息基类.所有命令的基类
6.2 Client to Server命令
6.3 Server to Client命令
6.4 Client to Client(Peer to Peer P2P)命令
6.5 定义用户的动作
扫一扫加作者微信
续: [原创]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
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
{
//消息基类
}
/// 消息基类,抽象类
/// </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
/// <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
/// <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
/// <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
}
/// 用户动作
/// </summary>
public enum UserAction
{
Login,
Logout
.... more action
}
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网