HandleResult OnReceive(HPSocketCS.TcpServer sender, IntPtr connId, byte[] bytes) { // 数据到达了 try { // 获取附加数据 ClientInfo clientInfo = extra.Get(connId); if (clientInfo != null) { // clientInfo 就是accept里传入的附加数据了 AddMsg(string.Format(" > [{0},OnReceive] -> {1}:{2} ({3} bytes)", clientInfo.ConnId, clientInfo.IpAddress, clientInfo.Port, bytes.Length)); } else { AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, bytes.Length)); } if (server.Send(connId, bytes, bytes.Length)) { return(HandleResult.Ok); } return(HandleResult.Error); } catch (Exception) { return(HandleResult.Ignore); } }
HandleResult OnPointerDataReceive(HPSocketCS.TcpServer sender, IntPtr connId, IntPtr pData, int length) { // 数据到达了 try { // 可以通过下面的方法转换到byte[] // byte[] bytes = new byte[length]; // Marshal.Copy(pData, bytes, 0, length); // 获取附加数据 ClientInfo clientInfo = extra.Get(connId); if (clientInfo != null) { // clientInfo 就是accept里传入的附加数据了 AddMsg(string.Format(" > [{0},OnReceive] -> {1}:{2} ({3} bytes)", clientInfo.ConnId, clientInfo.IpAddress, clientInfo.Port, length)); } else { AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, length)); } if (server.Send(connId, pData, length)) { return(HandleResult.Ok); } return(HandleResult.Error); } catch (Exception) { return(HandleResult.Ignore); } }
HandleResult OnAccept(HPSocketCS.TcpServer sender, IntPtr connId, IntPtr pClient) { // 客户进入了 // 获取客户端ip和端口 string ip = string.Empty; ushort port = 0; if (server.GetRemoteAddress(connId, ref ip, ref port)) { AddMsg(string.Format(" > [{0},OnAccept] -> PASS({1}:{2})", connId, ip.ToString(), port)); } else { AddMsg(string.Format(" > [{0},OnAccept] -> Server_GetClientAddress() Error", connId)); } // 设置附加数据 ClientInfo clientInfo = new ClientInfo(); clientInfo.ConnId = connId; clientInfo.IpAddress = ip; clientInfo.Port = port; if (extra.Set(connId, clientInfo) == false) { AddMsg(string.Format(" > [{0},OnAccept] -> SetConnectionExtra fail", connId)); } return(HandleResult.Ok); }
public TcpServerEx() { m_tcpserver = new HPSocketCS.TcpServer(); m_LockObject = new object(); m_Queue = new Queue <SocketEvent>(); m_TempQueue = new Queue <SocketEvent>(); }
HandleResult OnShutdown(HPSocketCS.TcpServer sender) { // 服务关闭了 AddMsg(" > [OnShutdown]"); return(HandleResult.Ok); }
HandleResult OnSend(HPSocketCS.TcpServer sender, IntPtr connId, byte[] bytes) { // 服务器发数据了 AddMsg(string.Format(" > [{0},OnSend] -> ({1} bytes)", connId, bytes.Length)); return(HandleResult.Ok); }
public static event ShowMsg OnMessage;//消息来了的委托事件 /// <summary> /// 构造函数,要求有参数 /// </summary> /// <param name="ip"></param> /// <param name="port">端口号</param> /// <param name="desc">服务器描述</param> public TcpServerManager(string ip, ushort port, string desc) { this.ip = ip; this.port = port; this.desc = desc; server = new HPSocketCS.TcpServer(); extra = new HPSocketCS.Extra <ClientInfo>(); server.OnAccept += new HPSocketCS.TcpServerEvent.OnAcceptEventHandler(server_OnAccept); server.OnSend += new TcpServerEvent.OnSendEventHandler(server_OnSend); server.OnReceive += new HPSocketCS.TcpServerEvent.OnReceiveEventHandler(server_OnReceive); server.OnClose += new TcpServerEvent.OnCloseEventHandler(server_OnClose); server.OnShutdown += new TcpServerEvent.OnShutdownEventHandler(server_OnShutdown); StartServer(); }
HandleResult OnClose(HPSocketCS.TcpServer sender, IntPtr connId, SocketOperation enOperation, int errorCode) { if (errorCode == 0) { AddMsg(string.Format(" > [{0},OnClose]", connId)); } else { AddMsg(string.Format(" > [{0},OnError] -> OP:{1},CODE:{2}", connId, enOperation, errorCode)); } if (extra.Remove(connId) == false) { AddMsg(string.Format(" > [{0},OnClose] -> SetConnectionExtra({0}, null) fail", connId)); } return(HandleResult.Ok); }
public BasicServer() { server = new HPSocketCS.TcpServer(); try { // 设置服务器事件 server.OnPrepareListen += new TcpServerEvent.OnPrepareListenEventHandler(OnPrepareListen); server.OnAccept += new TcpServerEvent.OnAcceptEventHandler(OnAccept); server.OnSend += new TcpServerEvent.OnSendEventHandler(OnSend); server.OnReceive += new TcpServerEvent.OnReceiveEventHandler(OnReceive); server.OnClose += new TcpServerEvent.OnCloseEventHandler(OnClose); server.OnError += new TcpServerEvent.OnErrorEventHandler(OnError); server.OnShutdown += new TcpServerEvent.OnShutdownEventHandler(OnShutdown); } catch (Exception ex) { Output.LogDefault(ex.Message); } }
public BaseDTU(DTUEntity dtu, IntPtr connId, TcpServer server) { this.dtu = dtu; this.connId = connId; this.server = server; }
HandleResult OnPrepareListen(HPSocketCS.TcpServer sender, IntPtr soListen) { // 监听事件到达了,一般没什么用吧? return(HandleResult.Ok); }
/// <summary> /// 处理单个数据 /// 这个数据可以使任何的,比如登陆的,心跳的 ,还是正常的数据包都行 /// </summary> /// <param name="data">要求为 ClientInfo</param> public void ProcessSingleData(object data) { ClientInfo clientInfo = data as ClientInfo; HPSocketCS.TcpServer server = clientInfo.server; byte[] bytes = clientInfo.Data; IntPtr connId = clientInfo.ConnId; if (clientInfo != null) { if (bytes.Length == 14 && bytes[0] == 170) { //14位长,并且第一位是170,表示是登陆指令,解析登陆id String logid = BaseDTU.ParseLogin(bytes); if (logid != "") { try { //合法的id,首先发送心跳包 if (server.Send(clientInfo.ConnId, heartbeat, heartbeat.Length)) { //发送心跳包成功,根据过滤的设置显示发送成功的消息 if (clientInfo.ConnId.ToInt32() % showFilterNumber == 0) { AddMsg(string.Format(" 发送了心跳包 {0}", clientInfo.ConnId)); } //判断此server的dtu字典中是否存在这个logid if (clientInfo.serverManager.dtuDic.ContainsKey(logid)) { //已存在key,说明已经初始化了 try { //心跳完成,准备轮训 clientInfo.serverManager.dtuDic[logid].server = server; clientInfo.serverManager.dtuDic[logid].connId = connId; basetimermgr.AddDTU(clientInfo.serverManager.dtuDic[logid]); if (clientInfo.ConnId.ToInt32() % showFilterNumber == 0) { AddMsg(string.Format(clientInfo.serverManager.desc + " > [客户端] -> {0}开始轮询", (int)connId)); } } catch (Exception e) { MessageBox.Show(e.Message); } } else { //不存在key 未被初始化 if (server.Disconnect(connId, true)) { //正常断开非法登录客户端 } AddMsg(string.Format(" > [客户端] -> {0}断开 id : {1}", (int)connId, logid)); } } } catch (Exception e) { MessageBox.Show("发送出错" + e.Message); } } else { //非法id,断开 if (server.Disconnect(connId, true)) { //正常断开非法登录客户端 } } } else { //不是登录包 if (bytes == heartbeat) { //收到的是心跳包 } else { try { //收到的是正常任务包 BaseDTU basedtu = clientInfo.serverManager.dtuDic.Values.Where(item => item.connId == connId).FirstOrDefault(); if (basedtu != null) { //把正常数据包丢给dtu处理 basedtu.ReceivedCmd(bytes); } } catch (Exception e) { server.Disconnect(connId); } } } } else { AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, bytes.Length)); } }