/********************************************************** * * 接收队列处理模块 * *********************************************************/ //推送数据 private void ReceivedThreadStart() { while (true) { recvbuffblockEvent.WaitOne(); if (ReceivedEvent != null) { icPacket packet = null; lock (Synrecv) { if (recvbuff.Count > 0) { packet = recvbuff.Dequeue(); } else { recvbuffblockEvent.Reset(); } } if ((packet != null) && (packet.data != null)) { ReceivedobjEventArgs e = new ReceivedobjEventArgs(packet.data); e.Appid = packet.Appid; e.Serverid = packet.Serverid; ReOriginData(e); ReceivedEvent(e); } } else { recvbuffblockEvent.Reset(); } } }
/********************************************************** * * 发送队列处理模块 * *********************************************************/ //Socket取数据 icPacket ics_GetDataEvent(EventArgs e) { sendbuffblockEvent.WaitOne(); icPacket result = null; lock (Synsend) { if (sendbuff.Count > 0) { result = sendbuff.Dequeue(); } if (sendbuff.Count == 0) { if (!sendbuffblocked) { sendbuffblocked = true; sendbuffblockEvent.Reset(); } } else { if (sendbuffblocked) { sendbuffblocked = false; sendbuffblockEvent.Set(); } } } return(result); }
///<summary>异步发送数据,提交后立即返回,数据包在缓存中等待发送</summary> ///<param name="data">数据包</param> public void Send(PacketManaged data) { UInt16 serverid = localserverid; UInt16 Appid = (UInt16)APPID.Authorization; if ((serverid > 0) && (Appid > 0)) { if (isAppOnline(serverid, Appid)) { lock (Synsend) { //封装寄出地址 data.Sserverid = localserverid; data.SAppid = app.id; data.checktime.Add(DateTime.Now); if (data.UnSerializedData != null) //对数据序列化 { DateTime date1 = DateTime.Now; MemoryStream stream = new MemoryStream(); sformatter.Serialize(stream, data.UnSerializedData); data.SerializedData = stream.GetBuffer(); data.UnSerializedData = null; data.datasize = data.SerializedData.Length; } // icPacket p = new icPacket(); p.Serverid = serverid; p.Appid = (APPID)Appid; p.dataid = Dataid; p.data = data; sendbuff.Enqueue(p); if (sendbuffblocked) { sendbuffblocked = false; sendbuffblockEvent.Set(); } } } else { sendingpacket status = new sendingpacket(); status.data = data; status.packetsize = 0; status.sendtime = DateTime.Now; status.outtime = DateTime.Now; SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_APPDOWN, status); ics_SendStatusEvent(e); } } else { sendingpacket status = new sendingpacket(); status.data = data; status.packetsize = 0; status.sendtime = DateTime.Now; status.outtime = DateTime.Now; SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_SERVERUNKOWN, status); ics_SendStatusEvent(e); } }
/********************************************************** * * 发送模块 * *********************************************************/ //发送线程 private void sendThreadStart() { MD5 md5Pro = new MD5CryptoServiceProvider(); while (true) { try { checkconnect(); sendblockEvent.WaitOne(); if (GetDataEvent != null) { icPacket packet = GetDataEvent(new EventArgs()); if (packet != null) { UInt16 Serverid = packet.Serverid; UInt16 Appid = (UInt16)packet.Appid; UInt32 Connectid = packet.Connectid; UInt32 dataid = packet.dataid; object data = packet.data; MemoryStream stream = new MemoryStream(); sformatter.Serialize(stream, data); byte[] d = stream.GetBuffer(); byte[] md5 = md5Pro.ComputeHash(d); byte[] p = icParse.makeDataPacket(md5, d, serverid, (UInt16)app.id, Serverid, Appid, dataid); UInt32 packetsize = (UInt32)p.Length; lock (Synsendinglist) { sendingpacket sp = new sendingpacket(); sp.data = packet.data; sp.packetsize = packetsize; sp.sendtime = DateTime.Now; double t = (sp.packetsize / MinNetSpeed); if (t < MinTimeOut) { t = MinTimeOut; } sp.outtime = sp.sendtime.AddSeconds(t); sendinglist.Add(dataid, sp); } SendPakcet(p); sendinglist[dataid].sendfinishtime = DateTime.Now; } } } catch (Exception e) { if (ExceptionEvent != null) { ExceptionEvent(e); } } } }
///<summary>异步发送数据,提交后立即返回,数据包在缓存中等待发送</summary> ///<param name="data">数据包</param> public void Send(PacketManaged data) { UInt16 serverid = data.Sserverid; UInt16 Appid = (UInt16)data.SAppid; if ((serverid > 0) && (Appid > 0)) { if (isAppOnline(serverid, (ushort)Appid)) { lock (Synsend) { data.checktime.Add(DateTime.Now); icPacket p = new icPacket(); p.Serverid = serverid; p.Appid = (APPID)Appid; p.dataid = Dataid; p.data = data; sendbuff.Enqueue(p); if (sendbuffblocked) { sendbuffblocked = false; sendbuffblockEvent.Set(); } } } else { sendingpacket status = new sendingpacket(); status.data = data; status.packetsize = 0; status.sendtime = DateTime.Now; status.outtime = DateTime.Now; SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_APPDOWN, status); ics_SendStatusEvent(e); } } else { sendingpacket status = new sendingpacket(); status.data = data; status.packetsize = 0; status.sendtime = DateTime.Now; status.outtime = DateTime.Now; SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_SERVERUNKOWN, status); ics_SendStatusEvent(e); } }
///<summary>异步发送数据,提交后立即返回,数据包在缓存中等待发送</summary> ///<param name="data">数据包</param> public void Send(string stationid, object data) { UInt16 serverid = StationIdtoServerid(stationid); UInt16 Appid = (UInt16)app.id; if ((serverid > 0) && (Appid > 0)) { if (isAppOnline(serverid, Appid)) { lock (Synsend) { icPacket p = new icPacket(); p.Serverid = serverid; p.Appid = (APPID)Appid; p.dataid = Dataid; p.data = data; sendbuff.Enqueue(p); if (sendbuffblocked) { sendbuffblocked = false; sendbuffblockEvent.Set(); } } } else { sendingpacket status = new sendingpacket(); status.data = data; status.packetsize = 0; status.sendtime = DateTime.Now; status.outtime = DateTime.Now; SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_APPDOWN, status); ics_SendStatusEvent(e); } } else { sendingpacket status = new sendingpacket(); status.data = data; status.packetsize = 0; status.sendtime = DateTime.Now; status.outtime = DateTime.Now; SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_SERVERUNKOWN, status); ics_SendStatusEvent(e); } }
/// <summary> /// 主动从接受缓存读取数据。对多线程安全。当ReceivedEvent未设置时启用,如果没数据,或设置了ReceivedEvent,将自动阻塞线程 /// </summary> /// <returns>接受到的数据包</returns> public ReceivedobjEventArgs GetData() { getrecvbuffblockEvent.WaitOne(); if (ReceivedEvent == null) { icPacket packet = null; lock (Synrecv) { if (recvbuff.Count > 0) { packet = recvbuff.Dequeue(); } else { getrecvbuffblockEvent.Reset(); } } if ((packet != null) && (packet.data != null)) { ReceivedobjEventArgs e = new ReceivedobjEventArgs(packet.data); e.Appid = packet.Appid; e.Serverid = packet.Serverid; ReOriginData(e); return(e); } else { return(null); } } else { getrecvbuffblockEvent.Reset(); return(null); } }
public ReceivedEventArgs(icPacket packet) { this.packet = packet; }
//解析 private void ParseData(byte[] buff, UInt32 buffindex, UInt32 PacketLength) { int command = (buff[buffindex] + (buff[buffindex + 1] << 8)); if (command == (int)COMMAND_ID.COMMAND_KEEP_ALIVE_RESPONSE) { } else if (command == (int)COMMAND_ID.COMMAND_REG_RESPONSE) { byte Regresult = buff[buffindex + 2]; if (Regresult == 1) { Reged = true; Reging = false; sendblockEvent.Set(); } else { Reged = false; Reging = false; sendblockEvent.Reset(); Exception e = new Exception("应用注册失败,同类应用可能已经在运行。"); if (ExceptionEvent != null) ExceptionEvent(e); } } else if (command == (int)COMMAND_ID.COMMAND_PACKET_SERVERDOWN) { if (SendStatusEvent != null) { UInt16 serverid = BitConverter.ToUInt16(buff, (int)buffindex + 2); UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 4); lock (Synsendinglist) { if (sendinglist.ContainsKey(dataid)) { SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_SERVERDOWN, sendinglist[dataid]); sendinglist.Remove(dataid); SendStatusEvent(e); } } } } else if (command == (int)COMMAND_ID.COMMAND_PACKET_APPDOWN) { if (SendStatusEvent != null) { UInt16 serverid = BitConverter.ToUInt16(buff, (int)buffindex + 2); UInt16 sourceappid = BitConverter.ToUInt16(buff, (int)buffindex + 4); UInt16 destappid = BitConverter.ToUInt16(buff, (int)buffindex + 6); UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 8); lock (Synsendinglist) { if (sendinglist.ContainsKey(dataid)) { SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_APPDOWN, sendinglist[dataid]); sendinglist.Remove(dataid); SendStatusEvent(e); } } } } else if (command == (int)COMMAND_ID.COMMAND_PACKET_ERR) { if (SendStatusEvent != null) { UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14); lock (Synsendinglist) { if (sendinglist.ContainsKey(dataid)) { SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_ERR, sendinglist[dataid]); sendinglist.Remove(dataid); SendStatusEvent(e); } } } } else if (command == (int)COMMAND_ID.COMMAND_PACKET_RECEIVED) { if(SendStatusEvent != null) { UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14); lock (Synsendinglist) { if (sendinglist.ContainsKey(dataid)) { sendinglist[dataid].outtime = DateTime.Now; SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_RECEIVED, sendinglist[dataid]); sendinglist.Remove(dataid); SendStatusEvent(e); } } } } else if (command == (int)COMMAND_ID.COMMAND_QUERY_SERVER_RESPONSE) { if(updateClientEvent != null) { char[] trimChars = {(char)0 }; List<ServerClass> servers = new List<ServerClass>(); int servercount = BitConverter.ToUInt16(buff, (int)buffindex + 2); UInt32 pos = buffindex + 4; for(int i = 0; i<servercount; ++i) { ushort serverid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2; string stationid = Encoding.Default.GetString(buff, (int)pos, 33).Trim(trimChars); pos = pos + 33; byte online = buff[pos]; ++pos; ServerClass s = new ServerClass(); s.m_Serverid = serverid; s.m_szStationID = stationid; s.isonline = (online == 1); servers.Add(s); } ClienUpdateEventArgs e = new ClienUpdateEventArgs(CLIENTUPDATE.updateServer); e.data = servers; updateClientEvent(e); } } else if ((command == (int)COMMAND_ID.COMMAND_QUERY_APP_RESPONSE) || (command == (int)COMMAND_ID.COMMAND_SYN_APP)) { if (updateClientEvent != null) { List<AppObj> applist = new List<AppObj>(); UInt32 pos = buffindex + 2; ushort serverid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2; ushort appcount = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2; for (int i = 0; i < appcount; ++i) { ushort appid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2; byte isadd = buff[pos]; ++pos; AppObj a = new AppObj(); a.m_Serverid = serverid; a.app = (APPID)appid; a.isonline = (isadd == 1); applist.Add(a); } ClienUpdateEventArgs e = new ClienUpdateEventArgs(CLIENTUPDATE.updateApp); e.data = applist; updateClientEvent(e); } } //接受到数据包 else if (command == (int)COMMAND_ID.COMMAND_PACKET) { if (ReceivedEvent != null) { const int CommnadSize = 2; //命令大小 const int ConnectSize = 12; //连接大小 const int DataidSize = 4; //数据块编号大小 const int DataLengthSize = 4; //数据块长度大小 const int DataOffsetSize = 4; //数据块偏移大小 const int MD5Length = 16; const int PacketHead = CommnadSize + ConnectSize + DataidSize + DataLengthSize + DataOffsetSize; bool datacorrect = true; UInt16 SourServerid = BitConverter.ToUInt16(buff, (int)buffindex + 2); UInt16 DestServerid = BitConverter.ToUInt16(buff, (int)buffindex + 4); UInt16 SourAppid = BitConverter.ToUInt16(buff, (int)buffindex + 6); UInt16 DestAppid = BitConverter.ToUInt16(buff, (int)buffindex + 8); UInt32 connid = BitConverter.ToUInt32(buff, (int)buffindex + 10); UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14); byte[] smd5 = new byte[MD5Length]; Array.Copy(buff, buffindex + PacketHead, smd5, 0, MD5Length); byte[] dmd5 = md5Pro.ComputeHash(buff, (int)buffindex + PacketHead + MD5Length, (int)PacketLength - PacketHead - MD5Length); for (int i = 0; i < MD5Length; i++) { if (smd5[i] != dmd5[i]) datacorrect = false; } //验证数据正确性 if (datacorrect) { icPacket packet = new icPacket(); object data = null; MemoryStream stream = new MemoryStream(buff, (int)buffindex + PacketHead + MD5Length, (int)PacketLength - PacketHead - MD5Length); data = rformatter.Deserialize(stream); packet.data = data; packet.Serverid = SourServerid; packet.Appid = (APPID)SourAppid; packet.dataid = dataid; ReceivedEventArgs e = new ReceivedEventArgs(packet); ReceivedEvent(e); socket.Send(icParse.makePacketReceived(DestServerid, DestAppid, SourServerid, SourAppid, 0, dataid)); } else { socket.Send(icParse.makePacketErr(DestServerid, DestAppid, SourServerid, SourAppid, 0, dataid)); } } } }
//解析 private void ParseData(byte[] buff, UInt32 buffindex, UInt32 PacketLength) { int command = (buff[buffindex] + (buff[buffindex + 1] << 8)); if (command == (int)COMMAND_ID.COMMAND_KEEP_ALIVE_RESPONSE) { } else if (command == (int)COMMAND_ID.COMMAND_REG_RESPONSE) { byte Regresult = buff[buffindex + 2]; if (Regresult == 1) { Reged = true; Reging = false; sendblockEvent.Set(); } else { Reged = false; Reging = false; sendblockEvent.Reset(); Exception e = new Exception("应用注册失败,同类应用可能已经在运行。"); if (ExceptionEvent != null) { ExceptionEvent(e); } } } else if (command == (int)COMMAND_ID.COMMAND_PACKET_SERVERDOWN) { if (SendStatusEvent != null) { UInt16 serverid = BitConverter.ToUInt16(buff, (int)buffindex + 2); UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 4); lock (Synsendinglist) { if (sendinglist.ContainsKey(dataid)) { SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_SERVERDOWN, sendinglist[dataid]); sendinglist.Remove(dataid); SendStatusEvent(e); } } } } else if (command == (int)COMMAND_ID.COMMAND_PACKET_APPDOWN) { if (SendStatusEvent != null) { UInt16 serverid = BitConverter.ToUInt16(buff, (int)buffindex + 2); UInt16 sourceappid = BitConverter.ToUInt16(buff, (int)buffindex + 4); UInt16 destappid = BitConverter.ToUInt16(buff, (int)buffindex + 6); UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 8); lock (Synsendinglist) { if (sendinglist.ContainsKey(dataid)) { SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_APPDOWN, sendinglist[dataid]); sendinglist.Remove(dataid); SendStatusEvent(e); } } } } else if (command == (int)COMMAND_ID.COMMAND_PACKET_ERR) { if (SendStatusEvent != null) { UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14); lock (Synsendinglist) { if (sendinglist.ContainsKey(dataid)) { SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_ERR, sendinglist[dataid]); sendinglist.Remove(dataid); SendStatusEvent(e); } } } } else if (command == (int)COMMAND_ID.COMMAND_PACKET_RECEIVED) { if (SendStatusEvent != null) { UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14); lock (Synsendinglist) { if (sendinglist.ContainsKey(dataid)) { sendinglist[dataid].outtime = DateTime.Now; SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_RECEIVED, sendinglist[dataid]); sendinglist.Remove(dataid); SendStatusEvent(e); } } } } else if (command == (int)COMMAND_ID.COMMAND_QUERY_SERVER_RESPONSE) { if (updateClientEvent != null) { char[] trimChars = { (char)0 }; List <ServerClass> servers = new List <ServerClass>(); int servercount = BitConverter.ToUInt16(buff, (int)buffindex + 2); UInt32 pos = buffindex + 4; for (int i = 0; i < servercount; ++i) { ushort serverid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2; string stationid = Encoding.Default.GetString(buff, (int)pos, 33).Trim(trimChars); pos = pos + 33; byte online = buff[pos]; ++pos; ServerClass s = new ServerClass(); s.m_Serverid = serverid; s.m_szStationID = stationid; s.isonline = (online == 1); servers.Add(s); } ClienUpdateEventArgs e = new ClienUpdateEventArgs(CLIENTUPDATE.updateServer); e.data = servers; updateClientEvent(e); } } else if ((command == (int)COMMAND_ID.COMMAND_QUERY_APP_RESPONSE) || (command == (int)COMMAND_ID.COMMAND_SYN_APP)) { if (updateClientEvent != null) { List <AppObj> applist = new List <AppObj>(); UInt32 pos = buffindex + 2; ushort serverid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2; ushort appcount = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2; for (int i = 0; i < appcount; ++i) { ushort appid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2; byte isadd = buff[pos]; ++pos; AppObj a = new AppObj(); a.m_Serverid = serverid; a.app = (APPID)appid; a.isonline = (isadd == 1); applist.Add(a); } ClienUpdateEventArgs e = new ClienUpdateEventArgs(CLIENTUPDATE.updateApp); e.data = applist; updateClientEvent(e); } } //接受到数据包 else if (command == (int)COMMAND_ID.COMMAND_PACKET) { if (ReceivedEvent != null) { const int CommnadSize = 2; //命令大小 const int ConnectSize = 12; //连接大小 const int DataidSize = 4; //数据块编号大小 const int DataLengthSize = 4; //数据块长度大小 const int DataOffsetSize = 4; //数据块偏移大小 const int MD5Length = 16; const int PacketHead = CommnadSize + ConnectSize + DataidSize + DataLengthSize + DataOffsetSize; bool datacorrect = true; UInt16 SourServerid = BitConverter.ToUInt16(buff, (int)buffindex + 2); UInt16 DestServerid = BitConverter.ToUInt16(buff, (int)buffindex + 4); UInt16 SourAppid = BitConverter.ToUInt16(buff, (int)buffindex + 6); UInt16 DestAppid = BitConverter.ToUInt16(buff, (int)buffindex + 8); UInt32 connid = BitConverter.ToUInt32(buff, (int)buffindex + 10); UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14); byte[] smd5 = new byte[MD5Length]; Array.Copy(buff, buffindex + PacketHead, smd5, 0, MD5Length); byte[] dmd5 = md5Pro.ComputeHash(buff, (int)buffindex + PacketHead + MD5Length, (int)PacketLength - PacketHead - MD5Length); for (int i = 0; i < MD5Length; i++) { if (smd5[i] != dmd5[i]) { datacorrect = false; } } //验证数据正确性 if (datacorrect) { icPacket packet = new icPacket(); object data = null; MemoryStream stream = new MemoryStream(buff, (int)buffindex + PacketHead + MD5Length, (int)PacketLength - PacketHead - MD5Length); data = rformatter.Deserialize(stream); packet.data = data; packet.Serverid = SourServerid; packet.Appid = (APPID)SourAppid; packet.dataid = dataid; ReceivedEventArgs e = new ReceivedEventArgs(packet); ReceivedEvent(e); socket.Send(icParse.makePacketReceived(DestServerid, DestAppid, SourServerid, SourAppid, 0, dataid)); } else { socket.Send(icParse.makePacketErr(DestServerid, DestAppid, SourServerid, SourAppid, 0, dataid)); } } } }