void client_DataOutPut(string key, ConClient conClient, byte[] Data) { if (ClientDataIn != null) { ClientDataIn(key, conClient, Data); } }
void client_ClientDiscon(ConClient client, string message) { try { this.BeginInvoke(new EventHandler((a, b) => { this.richTextBox1.AppendText(client.Host + ":" + client.Port + "-" + client.Key + " ->" + message + "\r\n"); })); if (client.UserToken != null) { UserInfo user = client.UserToken as UserInfo; if (user != null) { UserList.Remove(user); UpdateUserListView(); } client.UserToken = null; } if (!client.IsProxy) { client.Sock.Close(); } } catch { } }
void client_Conn(ConClient conClient, bool conn) { if (conClient != null && conn) { LogOut.LogIn(string.Format("连接到:{0} ---->OK", conClient.Sock.Sock.RemoteEndPoint.ToString()), ActionType.Message); if (!ConnUserList.ContainsKey(conClient.Key)) { if (ConnUserList.TryAdd(conClient.Key, conClient)) { conClient.Sock.StartRead(); if (ClientConnToMe != null) { ClientConnToMe(conClient); } } } else { conClient.Sock.Close(); } } else { LogOut.LogIn(string.Format("无法连接到指定地址端口 {0}:{1}", conClient.Host, conClient.Port), ActionType.Message); conClient.Sock.Close(); } }
/// <summary> /// 连接到指定的端口上 /// </summary> /// <param name="o"></param> private void RunConnToMe(object o) { try { string[] iphost = o.ToString().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (iphost.Length == 3) { int port; if (int.TryParse(iphost[1], out port)) { int maxport = port + ResetConnt; //最大端口等于提供的端口+尝试次数 for (int i = port + 1; i < maxport; i++) //循环连接的端口 { ConClient client = new ConClient(iphost[0], i); IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); if (BindPort >= 60000) { BindPort = 1000; } try { client.Sock.Sock.Bind(endpoint); client.Key = iphost[2]; client.Conn += new ConnectionsHandlerFiler(client_Conn); client.DataOutPut += new DataOutPutHandlerFiler(client_DataOutPut); client.ExpOUtPut += new ExpOutPutHandlerFiler(client_ExpOUtPut); client.ConnTo(); LogOut.LogIn(string.Format("开始尝试本地端口{0} 连接到 {1}:{2}", BindPort - 1, iphost[0], i), ActionType.Message); } catch (SocketException e) { LogOut.LogIn("错误无法绑定本地端口:" + e.Message, ActionType.Error); } } } } } catch (Exception ex) { LogOut.LogIn("代码出错#1:" + ex.Message, ActionType.Error); } finally { RunQueueList(); } }
void client_ExpOUtPut(ConClient conClient, string Message) { if (ConnUserList.ContainsKey(conClient.Key)) { ConnUserList.TryRemove(conClient.Key, out conClient); } LogOut.LogIn(string.Format("客户连接断开 {0}:{1}", conClient.Host + ":" + conClient.Port, conClient.Key), ActionType.Message); if (ClientDiscon != null) { ClientDiscon(conClient, string.Format("客户连接断开 {0}:{1}", conClient.Host + ":" + conClient.Port, conClient.Key)); } }
void client_Conn(ConClient conClient, bool conn) { if (conClient != null && conn) { LogOut.LogIn(string.Format("连接到:{0} ---->OK", conClient.Sock.Sock.RemoteEndPoint.ToString()),ActionType.Message); if (!ConnUserList.ContainsKey(conClient.Key)) { if (ConnUserList.TryAdd(conClient.Key, conClient)) { conClient.Sock.StartRead(); if (ClientConnToMe != null) ClientConnToMe(conClient); } } else { conClient.Sock.Close(); } } else { LogOut.LogIn(string.Format("无法连接到指定地址端口 {0}:{1}", conClient.Host, conClient.Port), ActionType.Message); conClient.Sock.Close(); } }
/// <summary> /// 数据包处理 /// </summary> /// <param name="data"></param> private void BufferIn(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int length; if (read.ReadInt32(out length) && length == read.Length) { int cmd; if (read.ReadInt32(out cmd)) { PCMD pcmd = (PCMD)cmd; switch (pcmd) { case PCMD.SET: //准备就绪 BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLMASK); Mainclient.Send(tmp.Finish()); break; case PCMD.ALLUSER: //获取全部用户列表 try { int count; if (read.ReadInt32(out count)) { for (int i = 0; i < count; i++) { string usermask; if (read.ReadString(out usermask)) { UserMaskList.Enqueue(usermask); } } RunQueueList(); } } catch (ArgumentOutOfRangeException) { } break; case PCMD.NOWCONN: //立刻连接到指定IP端口 string host; string key; if (read.ReadString(out host) && read.ReadString(out key)) { host = host + ":" + key; SocketClient client = new SocketClient(); Tp: IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定端口 if (BindPort >= 60000) { BindPort = 1000; } try { client.Sock.Bind(endpoint); //如果无法绑定那么重新选个端口 } catch { goto Tp; } if (client.Connect(this.Host, RegIpPort)) //连接到注册端口 { BufferFormat tmpX = new BufferFormat(100); tmpX.AddItem(Key); tmpX.AddItem(BindPort); client.Send(tmpX.Finish()); System.Threading.Thread.Sleep(50); BufferFormatV2 tmpX2 = new BufferFormatV2((int)PCMD.LEFTCONN); tmpX2.AddItem(key); Mainclient.Send(tmpX2.Finish()); client.Close(); System.Threading.Thread.Sleep(50); System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host); } } break; case PCMD.LEFTCONN: string host2; string key2; if (read.ReadString(out host2) && read.ReadString(out key2)) { host2 = host2 + ":" + key2; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host2); } break; case PCMD.GETALLUSER: { int count; if (read.ReadInt32(out count)) { AllUser = new List <string>(); for (int i = 0; i < count; i++) { string var; if (read.ReadString(out var)) { AllUser.Add(var); } else { break; } } if (GetAllUserList != null) { GetAllUserList(AllUser); } } } break; case PCMD.ProxyData: { string keys; byte[] buff; if (read.ReadString(out keys) && read.ReadByteArray(out buff)) { if (ProxyList.ContainsKey(keys)) { client_DataOutPut(keys, ProxyList[keys], buff); } else { ConClient client = new ConClient(keys); if (ProxyList.TryAdd(client.Key, client)) { client_DataOutPut(keys, client, buff); } } } } break; } } } }
void client_ClientDataIn(string key,ConClient client, byte[] data) { SetViewText("Revc:"+ Encoding.UTF8.GetString(data)); }
static void client_ClientConnToMe(ConClient client) { Console.WriteLine(client.Host + ":" + client.Port + "-" + client.Key + " 连接"); }
static void client_ClientDataIn(ConClient client, byte[] data) { Console.WriteLine(Encoding.UTF8.GetString(data)); }
void client_ExpOUtPut(ConClient conClient, string Message) { if(ConnUserList.ContainsKey(conClient.Key)) ConnUserList.TryRemove(conClient.Key, out conClient); LogOut.LogIn(string.Format("客户连接断开 {0}:{1}", conClient.Host+":"+conClient.Port, conClient.Key),ActionType.Message); if (ClientDiscon != null) ClientDiscon(conClient, string.Format("客户连接断开 {0}:{1}", conClient.Host + ":" + conClient.Port, conClient.Key)); }
static void client_ClientDiscon(ConClient client, string message) { Console.WriteLine(client.Host + ":" + client.Port + "-" + client.Key + " ->" + message); }
void client_ClientConnToMe(ConClient client) { SetViewText(client.Host + ":" + client.Port + "-" + client.Key + " 连接"); }
void client_ClientDataIn(string key,ConClient client, byte[] data) { try { UserInfo user = client.UserToken as UserInfo; if (user == null && client.IsProxy) { if (UserList == null) UserList = new List<UserInfo>(); user = new UserInfo(); user.Client = client; user.MainClient = MClient; user.Stream = new ZYSocket.share.ZYNetRingBufferPoolV2(1073741824); client.UserToken = user; UserList.Add(user); UpdateUserListView(); this.BeginInvoke(new EventHandler((a, b) => { this.richTextBox1.AppendText(client.Host + ":" + client.Port + "-" + client.Key + " 连接"); })); } if (user != null) { user.Stream.Write(data); byte[] datax; while (user.Stream.Read(out datax)) { if (user.IsSuccess) { DataOn(user, datax); } else { SuccessData(user, datax); } } } else { if (!client.IsProxy) { client.Sock.Close(); } } } catch (Exception er) { this.BeginInvoke(new EventHandler((a, b) => { this.richTextBox1.AppendText(er.ToString() + "\r\n"); })); } }
/// <summary> /// 连接到指定的端口上 /// </summary> /// <param name="o"></param> private void RunConnToMe(object o) { try { string[] iphost = o.ToString().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (iphost.Length == 3) { int port; if (int.TryParse(iphost[1], out port)) { int maxport = port + ResetConnt; //最大端口等于提供的端口+尝试次数 for (int i = port + 1; i < maxport; i++) //循环连接的端口 { ConClient client = new ConClient(iphost[0], i); IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); if (BindPort >= 60000) BindPort = 1000; try { client.Sock.Sock.Bind(endpoint); client.Key = iphost[2]; client.Conn += new ConnectionsHandlerFiler(client_Conn); client.DataOutPut += new DataOutPutHandlerFiler(client_DataOutPut); client.ExpOUtPut += new ExpOutPutHandlerFiler(client_ExpOUtPut); client.ConnTo(); LogOut.LogIn(string.Format("开始尝试本地端口{0} 连接到 {1}:{2}", BindPort - 1, iphost[0], i),ActionType.Message); } catch (SocketException e) { LogOut.LogIn("错误无法绑定本地端口:" + e.Message, ActionType.Error); } } } } } catch (Exception ex) { LogOut.LogIn("代码出错#1:"+ex.Message,ActionType.Error); } finally { RunQueueList(); } }
/// <summary> /// 数据包处理 /// </summary> /// <param name="data"></param> private void BufferIn(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int length; if (read.ReadInt32(out length) && length == read.Length) { int cmd; if (read.ReadInt32(out cmd)) { PCMD pcmd = (PCMD)cmd; switch (pcmd) { case PCMD.SET: //准备就绪 BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLMASK); Mainclient.Send(tmp.Finish()); break; case PCMD.ALLUSER: //获取全部用户列表 try { int count; if (read.ReadInt32(out count)) { for (int i = 0; i < count; i++) { string usermask; if (read.ReadString(out usermask)) { UserMaskList.Enqueue(usermask); } } RunQueueList(); } } catch (ArgumentOutOfRangeException) { } break; case PCMD.NOWCONN: //立刻连接到指定IP端口 string host; string key; if (read.ReadString(out host) && read.ReadString(out key)) { host = host + ":" + key; SocketClient client = new SocketClient(); Tp: IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定端口 if (BindPort >= 60000) BindPort = 1000; try { client.Sock.Bind(endpoint); //如果无法绑定那么重新选个端口 } catch { goto Tp; } if (client.Connect(this.Host, RegIpPort)) //连接到注册端口 { BufferFormat tmpX = new BufferFormat(100); tmpX.AddItem(Key); tmpX.AddItem(BindPort); client.Send(tmpX.Finish()); System.Threading.Thread.Sleep(50); BufferFormatV2 tmpX2 = new BufferFormatV2((int)PCMD.LEFTCONN); tmpX2.AddItem(key); Mainclient.Send(tmpX2.Finish()); client.Close(); System.Threading.Thread.Sleep(50); System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host); } } break; case PCMD.LEFTCONN: string host2; string key2; if (read.ReadString(out host2) && read.ReadString(out key2)) { host2 = host2 + ":" + key2; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host2); } break; case PCMD.GETALLUSER: { int count; if (read.ReadInt32(out count)) { AllUser = new List<string>(); for (int i = 0; i < count; i++) { string var; if (read.ReadString(out var)) { AllUser.Add(var); } else break; } if (GetAllUserList != null) GetAllUserList(AllUser); } } break; case PCMD.ProxyData: { string keys; byte[] buff; if (read.ReadString(out keys) && read.ReadByteArray(out buff)) { if (ProxyList.ContainsKey(keys)) { client_DataOutPut(keys, ProxyList[keys], buff); } else { ConClient client = new ConClient(keys); if (ProxyList.TryAdd(client.Key, client)) { client_DataOutPut(keys, client, buff); } } } } break; } } } }
void client_ClientConnToMe(ConClient client) { if (UserList == null) UserList = new List<UserInfo>(); UserInfo user = new UserInfo(); user.Client = client; user.MainClient = MClient; user.Stream = new ZYSocket.share.ZYNetRingBufferPoolV2(1073741824); client.UserToken = user; UserList.Add(user); UpdateUserListView(); this.BeginInvoke(new EventHandler((a, b) => { this.richTextBox1.AppendText(client.Host + ":" + client.Port + "-" + client.Key + " 连接"); })); }
void client_DataOutPut(string key,ConClient conClient, byte[] Data) { if (ClientDataIn != null) ClientDataIn(key,conClient, Data); }
void client_ClientDiscon(ConClient client, string message) { SetViewText(client.Host + ":" + client.Port + "-" + client.Key + " ->" + message); }