void client_DataOutPut(long key, ConClient conClient, byte[] Data) { if (DataInput != null) { DataInput(key, Data); } }
void client_ExpOUtPut(ConClient conClient, string Message) { SessionObj session = null; if (ConnUserList.ContainsKey(conClient.Key)) { session = ConnUserList[conClient.Key]; session.IsConnect = false; } LLOG(string.Format("客户连接断开 {0}:{1}", conClient.Host + ":" + conClient.Port, conClient.Key), ActionType.None); if (SessionDisconnect != null) { SessionDisconnect(conClient.Key, string.Format("客户连接断开 {0}:{1}", conClient.Host + ":" + conClient.Port, conClient.Key)); } }
void client_Conn(ConClient conClient, bool conn) { if (conClient != null && conn) { LLOG(string.Format("连接到:{0} ---->OK", conClient.Sock.Sock.RemoteEndPoint.ToString()), ActionType.None); if (ConnUserList.ContainsKey(conClient.Key)) { lock (lockObj) { if (!ConnUserList[conClient.Key].IsConnect) { conClient.DataOutPut += new DataOutPutHandlerFiler(client_DataOutPut); conClient.ExpOUtPut += new ExpOutPutHandlerFiler(client_ExpOUtPut); var session = ConnUserList[conClient.Key]; session.IsConnect = true; session.Client = conClient; conClient.Sock.StartRead(); if (ConnectToMe != null) { ConnectToMe(conClient.Key); } } else { conClient.Sock.Close(); } } } else { conClient.Sock.Close(); } } else { LLOG(string.Format("无法连接到指定地址端口 {0}:{1}", conClient.Host, conClient.Port), ActionType.None); conClient.Sock.Close(); } }
/// <summary> /// 连接到指定的端口上 /// </summary> /// <param name="o"></param> private async void RunConnToMe(string host, int port, long id) { await Task.Run(() => { try { int maxport = port + 1 + ResetConnt; //最大端口等于提供的端口+尝试次数 for (int i = port + 1; i < maxport; i++) //循环连接的端口 { ConClient client = new ConClient(host, i, BufferPoolLength); IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); if (BindPort >= 60000) { BindPort = 1000; } try { client.Sock.Sock.Bind(endpoint); client.Key = id; client.Conn += new ConnectionsHandlerFiler(client_Conn); client.ConnTo(); LLOG(string.Format("开始尝试本地端口{0} 连接到 {1}:{2}", BindPort - 1, host, i), ActionType.None); } catch (SocketException e) { LLOG("错误无法绑定本地端口:" + e.Message, ActionType.Error); } } } catch (Exception ex) { LLOG("代码出错#1:" + ex.Message, ActionType.Error); } finally { RunQueueList(); } }); }