public void AsyncAccept(IAsyncResult Ar) { try { Socket socket = Server_Socket.EndAccept(Ar); int Index = Get_Connect_Index(); if (Index < 0) { socket.Close(); FDebug.Log("警告:连接已满"); } else { Connect connect = connects[Index]; connect.Init(socket); FDebug.Log("与{0}建立连接", connect.GetAddress()); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } Server_Socket.BeginAccept(AsyncAccept, null); } catch (Exception e) { FDebug.Log("AsyncAccept失败 :" + e.Message); } }
public bool CheckRights(SystemRights netRights) { if (netRights == SystemRights.root || netRights == SystemRights.Admin || netRights == Rights) { return(true); } FDebug.Log("没有访问权限"); return(false); }
public void Close() { if (!State_IsUSE) { return; } FDebug.Log("断开连接:" + GetAddress()); Connect_Socket.Close(); State_IsUSE = false; }
/// <summary> /// 数据传送 /// </summary> /// <param name="bs"></param> private void DataTransfer(byte[] bs, Connect connect) { /// ///待完善 FDebug.Log("接收完成共{0}byte数据 From:{1}", bs.Length, connect.GetAddress()); SocketCall?.Invoke(bs); //数据解密 // IPC.GetInstance().InComing_DATA(bs, connect); }
/// <summary> /// 对从网络缓冲区拿到的数据得处理函数 /// </summary> /// <param name="connect">连接类的实例</param> private void PossingData(Connect connect) { if (connect.buffCount < sizeof(Int32)) { connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } else { Int32 MessageLen = BitConverter.ToInt32(connect.Buffers, 0);//取包头获取单个数据包长度 if (connect.buffCount < sizeof(Int32) + MessageLen && connect.buffCount < Connect.BUFFERS_SIZE) { connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } else if (connect.buffCount < sizeof(Int32) + MessageLen && Connect.BUFFERS_SIZE < sizeof(Int32) + MessageLen) { try { MemoryStream stream = new MemoryStream(); stream.Write(connect.Buffers, sizeof(Int32), connect.buffCount - sizeof(Int32)); connect.MessageLenght = MessageLen; connect.BufferStream = stream; connect.RestBuffCount(); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.StreamBuffLen(), SocketFlags.None, SubReceiveCb, connect); } catch (Exception e) { FDebug.Log(e.Message + " :分包接收"); } } else { try { byte[] data = new byte[MessageLen]; Array.Copy(connect.Buffers, sizeof(Int32), data, 0, MessageLen); DataTransfer(data, connect); connect.buffCount -= (MessageLen + sizeof(Int32)); if (connect.buffCount > 0) { Array.Copy(connect.Buffers, sizeof(Int32) + MessageLen, connect.Buffers, 0, connect.buffCount); PossingData(connect); } else { connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } } catch (Exception e) { FDebug.Log(e.Message + " pd"); } } } }
private void PossingData() { if (buffCount < sizeof(Int32)) { socket.BeginReceive(Buffers, buffCount, BuffRemain(), SocketFlags.None, AsyncReceiveCb, this); } else { Int32 MessageLen = BitConverter.ToInt32(Buffers, 0);//取包头获取单个数据包长度 if (buffCount < sizeof(Int32) + MessageLen && buffCount < BUFFERS_SIZE) { socket.BeginReceive(Buffers, buffCount, BuffRemain(), SocketFlags.None, AsyncReceiveCb, this); } else if (buffCount < sizeof(Int32) + MessageLen && BUFFERS_SIZE < sizeof(Int32) + MessageLen) { try { MemoryStream stream = new MemoryStream(); stream.Write(Buffers, sizeof(Int32), buffCount - sizeof(Int32)); MessageLenght = MessageLen; BufferStream = stream; RestBuffCount(); socket.BeginReceive(Buffers, buffCount, StreamBuffLen(), SocketFlags.None, SubReceiveCb, this); } catch (Exception e) { FDebug.Log(e.Message + " :分包接收"); } } else { try { byte[] data = new byte[MessageLen]; Array.Copy(Buffers, sizeof(Int32), data, 0, MessageLen); DataTransfer(data); buffCount -= (MessageLen + sizeof(Int32)); if (buffCount > 0) { Array.Copy(Buffers, sizeof(Int32) + MessageLen, Buffers, 0, buffCount); PossingData(); } else { socket.BeginReceive(Buffers, buffCount, BuffRemain(), SocketFlags.None, AsyncReceiveCb, this); } } catch (Exception e) { FDebug.Log(e.Message + " pd"); } } } }
public int MStreamDispose() { try { BufferStream.Close(); BufferStream.Dispose(); MessageLenght = 0; return(1); }catch (Exception e) { FDebug.Log(e.Message); return(-1); } }
public void CheckCommand() { string Commend = Console.ReadLine(); var function = Fursion_CSharpTools.CSharpTools.GetMethodDo <ServiceCommand>(Commend); if (function != null) { function.Invoke(this, null); } else { FDebug.Log(@"Not found this ""{0}"" command", Commend); } }
public void Connect(string HOST, int PORT) { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress iP = IPAddress.Parse(HOST); try { socket.Connect(iP, PORT); socket.BeginReceive(Buffers, 0, BUFFERS_SIZE, SocketFlags.None, AsyncReceiveCb, null); } catch (SocketException se) { FDebug.Log(se.Message); } }
/// <summary> /// 自动获取IP地址的启动函数 /// </summary> /// <param name="Port"></param> /// <param name="callBack"></param> /// <returns></returns> public int StarServer(int Port, SocketCallBack callBack) { Server_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connects = new Connect[MAX_CONNECT_NUMBERS]; IPEndPoint iPEnd = new IPEndPoint(IPAddress.Any, Port); if (callBack != null) { SocketCall += callBack; } Server_Socket.Bind(iPEnd); Server_Socket.Listen(10); Server_Socket.BeginAccept(AsyncAccept, null); FDebug.Log(iPEnd.Address.ToString()); return(1); }
/// <summary> /// 自定义端口的启动函数 /// </summary> /// <param name="IP"></param> /// <param name="Port"></param> /// <param name="callBack"></param> public void StarServer(string IP, int Port, SocketCallBack callBack) { FDebug.Log("Service starting..."); Server_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connects = new Connect[MAX_CONNECT_NUMBERS]; IPEndPoint iPEnd = new IPEndPoint(IPAddress.Parse(IP), Port); if (callBack != null) { SocketCall += callBack; } Server_Socket.Bind(iPEnd); Server_Socket.Listen(10); Server_Socket.BeginAccept(AsyncAccept, null); FDebug.Log("ServiceAddressIp:{0}", iPEnd.Address.ToString()); FDebug.Log("Service started Successfully!"); }
/// <summary> /// 创建连接实例 /// </summary> /// <param name="SQLConnectionStatement"></param> /// <returns></returns> public static MySqlConnection ConnectSQL(string SQLConnectionStatement) { FDebug.Log("连接数据库中..."); MySqlConnection SqlConnect = new MySqlConnection(SQLConnectionStatement); try { SqlConnect.Open(); FDebug.Log("成功连接到数据库"); return(SqlConnect); } catch (MySqlException SqlEx) { FDebug.Log("连接数据库失败: {0}", SqlEx.Message); return(SqlConnect); } }
/// <summary> /// 增量接收 /// </summary> /// <param name="ar"></param> private void SubReceiveCb(IAsyncResult ar) { Connect connect = (Connect)ar.AsyncState; try { int count = connect.Connect_Socket.EndReceive(ar); connect.buffCount = count; if (count < 0) { connect.Close(); } else if (count == 0) { connect.MStreamDispose(); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } else { connect.BufferStream.Write(connect.Buffers, 0, connect.buffCount); if (connect.MessageLenght == connect.BufferStream.Length) { var bs = connect.BufferStream.ToArray(); DataTransfer(bs, connect); connect.MStreamDispose(); connect.RestBuffCount(); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } else if (connect.MessageLenght > connect.BufferStream.Length) { connect.RestBuffCount(); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.StreamBuffLen(), SocketFlags.None, SubReceiveCb, connect); } else { FDebug.Log("MS < s.lenght"); } } } catch (Exception e) { FDebug.Log(e.Message + "SubReceiveCb err"); } }
/// <summary> /// 增量接收 /// </summary> /// <param name="ar"></param> private void SubReceiveCb(IAsyncResult ar) { try { int count = socket.EndReceive(ar); FDebug.Log("收到{0}个byte数据", count); buffCount = count; if (count < 0) { Close(); } else if (count == 0) { MStreamDispose(); socket.BeginReceive(Buffers, buffCount, BuffRemain(), SocketFlags.None, AsyncReceiveCb, this); } else { BufferStream.Write(Buffers, 0, buffCount); if (MessageLenght == BufferStream.Length) { var bs = BufferStream.ToArray(); DataTransfer(bs); MStreamDispose(); RestBuffCount(); socket.BeginReceive(Buffers, buffCount, BuffRemain(), SocketFlags.None, AsyncReceiveCb, this); } else if (MessageLenght > BufferStream.Length) { socket.BeginReceive(Buffers, buffCount, StreamBuffLen(), SocketFlags.None, SubReceiveCb, this); } else { FDebug.Log("MS < s.lenght"); } } } catch (Exception e) { FDebug.Log(e.Message + "Sub err"); } }
private void AsyncReceiveCb(IAsyncResult Ar) { try { int count = socket.EndReceive(Ar); FDebug.Log("接收到" + count + ":{0}个字节的数据", count); if (count < 0) { //有待修改 Close(); } buffCount += count; PossingData(); } catch (Exception e) { FDebug.Log(" :连接异常 已经断开"); FDebug.Log(e.Message + " From: ServerMain.AsyncReceiveCb"); Close(); } }
/// <summary> /// 异步接收回调 /// </summary> /// <param name="Ar"></param> private void AsyncReceiveCb(IAsyncResult Ar) { Connect connect = (Connect)Ar.AsyncState; try { int count = connect.Connect_Socket.EndReceive(Ar); if (count < 0) { //有待修改 connect.Close(); } connect.buffCount += count; PossingData(connect); } catch (Exception e) { FDebug.Log(connect.GetAddress() + " :连接异常 已经断开"); FDebug.Log(e.Message + " from: ServerMain.AsyncReceiveCb"); connect.Close(); } }
string IJobTaskGet <string> .Execute(object obj) { try { MailMessage mailMsg = new MailMessage(); mailMsg.From = new MailAddress("*****@*****.**", "fursion"); mailMsg.To.Add(new MailAddress(Addressee)); //mailMsg.CC.Add("抄送人地址"); //mailMsg.Bcc.Add("密送人地址"); //可选,设置回信地址 mailMsg.ReplyToList.Add("*****@*****.**"); // 邮件主题 mailMsg.Subject = Subject; // 邮件正文内容 //mailMsg.Body = MailText; mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(MailText, null, MediaTypeNames.Text.Html)); mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(MailHtml, null, MediaTypeNames.Text.Html)); // 添加附件 if (System.IO.File.Exists(File)) { Attachment data = new Attachment(File, MediaTypeNames.Application.Octet); mailMsg.Attachments.Add(data); } //邮件推送的SMTP地址和端口 SmtpClient smtpClient = new SmtpClient("mail.fursion.cn", 25); //smtpClient.EnableSsl = true; // 使用SMTP用户名和密码进行验证 System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("*****@*****.**", "dj970619"); smtpClient.Credentials = credentials; smtpClient.Send(mailMsg); return("send successfuly"); } catch (Exception ex) { FDebug.Log(ex.ToString()); return(ex.Message); } }
public void CallBack(object obj) { FDebug.Log("回调 {0} 线程ID:{1}", i * i, Thread.CurrentThread.ManagedThreadId); }
public void Execute(object obj) { FDebug.Log("jobTask test"); }
public static void SocketCall(byte[] bs) { FDebug.Log("收到信号"); }
public void CallBack(object obj) { FDebug.Log(((Task)obj).Status.ToString()); }
public int Execute(object obj) { FDebug.Log("执行任务"); return(Thread.CurrentThread.ManagedThreadId * i); }
public void Execute(object obj) { FDebug.Log("{0} 线程ID:{1}", i, Thread.CurrentThread.ManagedThreadId); }