您的位置:首页 > 其它

ConnectionInfo类(NetworkComms 2.3.1源码了解和学习)

2015-03-03 00:00 615 查看
networkComms.net2.3.1开源版本,基于gpl V3协议。因为不能公开3.x版本的源码,所以基于此版本进行学习。3.X版本进行了诸多改进和Bug修复,使用方法上两者相差不大。 /*请注意使用以下代码,需遵循GplV3协议*/

/// <summary>
/// 连接状态枚举类 /// </summary>
public enum ConnectionState { /// <summary>
/// 未定义 是连接的初始状态. /// </summary>
Undefined, /// <summary>
///连接创建中 /// </summary>
Establishing, /// <summary>
/// 连接已经创建完成. /// </summary>
Established, /// <summary>
/// 连接已经关闭. /// </summary>
Shutdown } /// <summary>
/// 连接信息类 包含与连接相关的配置信息 /// </summary>
[ProtoContract] public class ConnectionInfo : IEquatable<ConnectionInfo> { /// <summary>
/// 连接类型 /// </summary>
[ProtoMember(1)] public ConnectionType ConnectionType { get; internal set; } /// <summary>
/// 网络ID. /// </summary>
[ProtoMember(2)] string NetworkIdentifierStr; [ProtoMember(3)] string localEndPointIPStr; //Only set on serialise
[ProtoMember(4)] int localEndPointPort; //Only set on serialise

bool hashCodeCacheSet = false; int hashCodeCache; /// <summary>
/// True if the <see cref="RemoteEndPoint"/> is connectable. /// </summary>
[ProtoMember(5)] public bool IsConnectable { get; private set; } /// <summary>
///连接创建时间 /// </summary>
public DateTime ConnectionCreationTime { get; protected set; } /// <summary>
///是否为服务器端 /// </summary>
public bool ServerSide { get; internal set; } /// <summary>
/// 连接创建完成时间 /// </summary>
public DateTime ConnectionEstablishedTime { get; private set; } /// <summary>
/// 本地IP端点 /// </summary>
public IPEndPoint LocalEndPoint { get; private set; } /// <summary>
/// 远端IP端点 /// </summary>
public IPEndPoint RemoteEndPoint { get; private set; } /// <summary>
/// 连接目前的状态 /// </summary>
public ConnectionState ConnectionState { get; private set; } /// <summary>
/// Returns the networkIdentifier of this peer as a ShortGuid. If the NetworkIdentifier has not yet been set returns ShortGuid.Empty. /// </summary>
public ShortGuid NetworkIdentifier { get { if (NetworkIdentifierStr == null || NetworkIdentifierStr == "") return ShortGuid.Empty; else return new ShortGuid(NetworkIdentifierStr); } } DateTime lastTrafficTime; object internalLocker = new object(); /// <summary>
/// 最近更新时间 如果有数据包进出 此时间会被更新 /// </summary>
public DateTime LastTrafficTime { get { lock (internalLocker) return lastTrafficTime; } protected set { lock (internalLocker) lastTrafficTime = value; } } /// <summary>
/// Private constructor required for deserialisation. /// </summary>
#if iOS || ANDROID
public ConnectionInfo() { } #else
private ConnectionInfo() { } #endif

/// <summary>
/// 根据给定的端点,创建一个新的连接信息对象 /// </summary>
public ConnectionInfo(IPEndPoint remoteEndPoint) { this.RemoteEndPoint = remoteEndPoint; this.ConnectionCreationTime = DateTime.Now; } //根据给定的IP地址和端口创建连接信息对象
public ConnectionInfo(string remoteIPAddress, int remotePort) { IPAddress ipAddress; if (!IPAddress.TryParse(remoteIPAddress, out ipAddress)) throw new ArgumentException("Provided remoteIPAddress string was not succesfully parsed.", "remoteIPAddress"); this.RemoteEndPoint = new IPEndPoint(ipAddress, remotePort); this.ConnectionCreationTime = DateTime.Now; } public ConnectionInfo(ConnectionType connectionType, ShortGuid localNetworkIdentifier, IPEndPoint localEndPoint, bool isConnectable) { this.ConnectionType = connectionType; this.NetworkIdentifierStr = localNetworkIdentifier.ToString(); this.LocalEndPoint = localEndPoint; this.IsConnectable = isConnectable; } internal ConnectionInfo(bool serverSide, ConnectionType connectionType, IPEndPoint remoteEndPoint) { this.ServerSide = serverSide; this.ConnectionType = connectionType; this.RemoteEndPoint = remoteEndPoint; this.ConnectionCreationTime = DateTime.Now; } internal ConnectionInfo(bool serverSide, ConnectionType connectionType, IPEndPoint remoteEndPoint, IPEndPoint localEndPoint) { this.ServerSide = serverSide; this.ConnectionType = connectionType; this.RemoteEndPoint = remoteEndPoint; this.LocalEndPoint = localEndPoint; this.ConnectionCreationTime = DateTime.Now; } [ProtoBeforeSerialization] private void OnSerialise() { lock (internalLocker) { localEndPointIPStr = LocalEndPoint.Address.ToString(); localEndPointPort = LocalEndPoint.Port; } } [ProtoAfterDeserialization] private void OnDeserialise() { IPAddress ipAddress; if (!IPAddress.TryParse(localEndPointIPStr, out ipAddress)) throw new ArgumentException("Failed to parse IPAddress from localEndPointIPStr", "localEndPointIPStr"); LocalEndPoint = new IPEndPoint(ipAddress, localEndPointPort); } /// <summary>
/// 标记连接创建中 /// </summary>
internal void NoteStartConnectionEstablish() { lock(internalLocker) { if (ConnectionState == ConnectionState.Shutdown) throw new ConnectionSetupException("Unable to mark as establishing as connection has already shutdown."); if (ConnectionState == ConnectionState.Establishing) throw new ConnectionSetupException("Connection already marked as establishing"); else ConnectionState = ConnectionState.Establishing; } } /// <summary>
/// 标记连接创建完成 /// </summary>
internal void NoteCompleteConnectionEstablish() { lock (internalLocker) { if (ConnectionState == ConnectionState.Shutdown) throw new ConnectionSetupException("Unable to mark as established as connection has already shutdown."); if (!(ConnectionState == ConnectionState.Establishing)) throw new ConnectionSetupException("Connection should be marked as establishing before calling CompleteConnectionEstablish"); if (ConnectionState == ConnectionState.Established) throw new ConnectionSetupException("Connection already marked as establised."); ConnectionState = ConnectionState.Established; ConnectionEstablishedTime = DateTime.Now; if (NetworkIdentifier == ShortGuid.Empty) throw new ConnectionSetupException("Unable to set connection established until networkIdentifier has been set."); } } /// <summary>
/// 标记连接关闭 /// </summary>
internal void NoteConnectionShutdown() { lock (internalLocker) ConnectionState = ConnectionState.Shutdown; } /// <summary>
/// 更新连接状态 /// </summary>
/// <param name="localEndPoint"></param>
internal void UpdateLocalEndPointInfo(IPEndPoint localEndPoint) { lock (internalLocker) { hashCodeCacheSet = false; this.LocalEndPoint = localEndPoint; } }

//在英文网站上购买 九折折扣代码: NCDN_PRCLW

//淘宝正版销售 http://shop115882994.taobao.com/ 推广期间 八折优惠

/// 在连接握手完成后,我们更新相关的信息
internal void UpdateInfoAfterRemoteHandshake(ConnectionInfo handshakeInfo, IPEndPoint remoteEndPoint) { lock (internalLocker) { NetworkIdentifierStr = handshakeInfo.NetworkIdentifier.ToString(); RemoteEndPoint = remoteEndPoint; LocalEndPoint.Address = handshakeInfo.LocalEndPoint.Address; IsConnectable = handshakeInfo.IsConnectable; } } /// <summary>
///更新最近传输时间 /// </summary>
internal void UpdateLastTrafficTime() { lock (internalLocker) lastTrafficTime = DateTime.Now; } /// <summary>
/// 替换网络ID /// </summary>
/// <param name="networkIdentifier">The new networkIdentifier for this connectionInfo</param>
public void ResetNetworkIdentifer(ShortGuid networkIdentifier) { NetworkIdentifierStr = networkIdentifier.ToString(); }  //重新设定连接状态
internal void ResetConnectionInfo() { lock (internalLocker) { ConnectionState = ConnectionState.Undefined; } } public override bool Equals(object obj) { lock (internalLocker) { var other = obj as ConnectionInfo; if (((object)other) == null) return false; else
return this == other; } } public bool Equals(ConnectionInfo other) { lock (internalLocker) return this == other; } public static bool operator ==(ConnectionInfo left, ConnectionInfo right) { if (((object)left) == ((object)right)) return true; else if (((object)left) == null || ((object)right) == null) return false; else { if (left.RemoteEndPoint != null && right.RemoteEndPoint != null) return (left.NetworkIdentifier.ToString() == right.NetworkIdentifier.ToString() && left.RemoteEndPoint.Equals(right.RemoteEndPoint)); else
return (left.NetworkIdentifier.ToString() == right.NetworkIdentifier.ToString()); } } /// <summary>
/// Returns !left.Equals(right) /// </summary>
/// <param name="left">Left connectionInfo</param>
/// <param name="right">Right connectionInfo</param>
/// <returns>True if both are different, otherwise false</returns>
public static bool operator !=(ConnectionInfo left, ConnectionInfo right) { return !(left == right); } /// <summary>
/// Returns NetworkIdentifier.GetHashCode() ^ RemoteEndPoint.GetHashCode(); /// </summary>
/// <returns>The hashcode for this connection info</returns>
public override int GetHashCode() { lock (internalLocker) { if (!hashCodeCacheSet) { if (RemoteEndPoint != null) hashCodeCache = NetworkIdentifier.GetHashCode() ^ RemoteEndPoint.GetHashCode(); else hashCodeCache = NetworkIdentifier.GetHashCode(); hashCodeCacheSet = true; } return hashCodeCache; } } /// <summary>
/// Returns a string containing suitable information about this connection /// </summary>
/// <returns>A string containing suitable information about this connection</returns>
public override string ToString() { string returnString = "[" + ConnectionType.ToString() + "] "; if (ConnectionState == ConnectionState.Established) returnString += LocalEndPoint.Address + ":" + LocalEndPoint.Port.ToString() + " -> " + RemoteEndPoint.Address + ":" + RemoteEndPoint.Port.ToString() + " (" + NetworkIdentifier + ")"; else { if (RemoteEndPoint != null && LocalEndPoint != null) returnString += LocalEndPoint.Address + ":" + LocalEndPoint.Port.ToString() + " -> " + RemoteEndPoint.Address + ":" + RemoteEndPoint.Port.ToString(); else if (RemoteEndPoint != null) returnString += "Local -> " + RemoteEndPoint.Address + ":" + RemoteEndPoint.Port.ToString(); else if (LocalEndPoint != null) returnString += LocalEndPoint.Address + ":" + LocalEndPoint.Port.ToString() + " " + (IsConnectable ? "Connectable" : "NotConnectable"); } return returnString.Trim(); } } } http://www.cnblogs.com/networkcomms http://www.networkcoms.cn 编辑


来自英国剑桥的c#网络通讯框架  开源版本: networkcomms2.3.1  可以进入此页面下载 networkcomms网络通讯框架学习

【开源下载】基于TCP网络通信的即时聊天系统(IM系统)(c#源码)

[源码下载]Demo2.模拟简单登陆-效果图 基于networkcomms2.3.1

[源码下载]Demo1 客户端从服务器获取信息(基于networkcomms2.3.1)

【开源下载】基于TCP网络通信的自动升级程序c#源码

【模板下载】分享我所使用的数据库框架

【模板下载】innosetup 制作.net安装包的模板

【模板下载】分享我所使用的数据库框架
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐