/********************************************************** * * 推送发送报告处理模块 * *********************************************************/ private void SendstatusThreadStart() { while (true) { reportbuffblockEvent.WaitOne(); if (SendStatusEvent != null) { SendStatusEventArgs e = null; lock (Synreport) { if (reportbuff.Count > 0) { e = reportbuff.Dequeue(); } else { reportbuffblockEvent.Reset(); } } if (e != null) { SendStatusEvent(e); } } else { reportbuffblockEvent.Reset(); } } }
///<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); } }
protected void ics_SendStatusEvent(SendStatusEventArgs e) { lock (Synreport) { if (SendStatusEvent != null) { reportbuff.Enqueue(e); reportbuffblockEvent.Set(); } } }
///<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); } }
/********************************************************** * * 超时验证 * *********************************************************/ //超时验证线程 private void sendouttimeThreadStart() { while (true) { lock (Synsendinglist) { var outlist = from p in sendinglist where p.Value.outtime < DateTime.Now select p; foreach (var v in outlist) { sendinglist.Remove(v.Key); SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_TIMEOUT, v.Value); SendStatusEvent(e); } } Thread.Sleep(30000); } }
/********************************************************** * * 超时验证 * *********************************************************/ //超时验证线程 private void sendouttimeThreadStart() { while (true) { lock (Synsendinglist) { var outlist = from p in sendinglist where p.Value.outtime < DateTime.Now select p; foreach ( var v in outlist) { sendinglist.Remove(v.Key); SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_TIMEOUT, v.Value); SendStatusEvent(e); } } Thread.Sleep(30000); } }
//解析 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)); } } } }
void cc_SendStatusEvent(SendStatusEventArgs e) { if (this.InvokeRequired) { InvokeSendStatusEvent ivo = new InvokeSendStatusEvent(this.cc_SendStatusEvent); this.Invoke(ivo, e); } else { switch (e.Status) { case SENDSTATUS.PACKET_RECEIVED: switch (((testpacket)e.Packet.data).type) { case PacketType.sdata: sendreports[((testpacket)e.Packet.data).recvServerid].SendOK(((testpacket)e.Packet.data).type, e.Packet.packetsize, e.Packet.sendtime,e.Packet.sendfinishtime,e.Packet.outtime); if (((testpacket)e.Packet.data).isEnd) { double runtime = (DateTime.Now - tasklist[((testpacket)e.Packet.data).taskid]).TotalMilliseconds; tasklist.Remove(((testpacket)e.Packet.data).taskid); sendreports[((testpacket)e.Packet.data).recvServerid].TaskDone(((testpacket)e.Packet.data).type, runtime); } break; case PacketType.mdata: sendreports[((testpacket)e.Packet.data).recvServerid].SendOK(((testpacket)e.Packet.data).type, e.Packet.packetsize, e.Packet.sendtime, e.Packet.sendfinishtime, e.Packet.outtime); if (((testpacket)e.Packet.data).isEnd) { double runtime = (DateTime.Now - tasklist[((testpacket)e.Packet.data).taskid]).TotalMilliseconds; tasklist.Remove(((testpacket)e.Packet.data).taskid); sendreports[((testpacket)e.Packet.data).recvServerid].TaskDone(((testpacket)e.Packet.data).type, runtime); } break; case PacketType.ldata: sendreports[((testpacket)e.Packet.data).recvServerid].SendOK(((testpacket)e.Packet.data).type, e.Packet.packetsize, e.Packet.sendtime, e.Packet.sendfinishtime, e.Packet.outtime); if (((testpacket)e.Packet.data).isEnd) { double runtime = (DateTime.Now - tasklist[((testpacket)e.Packet.data).taskid]).TotalMilliseconds; tasklist.Remove(((testpacket)e.Packet.data).taskid); sendreports[((testpacket)e.Packet.data).recvServerid].TaskDone(((testpacket)e.Packet.data).type, runtime); } break; case PacketType.text: break; case PacketType.file: break; default: break; } break; case SENDSTATUS.PACKET_SERVERDOWN: textBox9.AppendText(e.Msg); //textBox9.AppendText(":" + e.Packet.ToString()); textBox9.AppendText(((char)10).ToString()); break; case SENDSTATUS.PACKET_APPDOWN: textBox9.AppendText(e.Msg); //textBox9.AppendText(":" + e.Packet.ToString()); textBox9.AppendText(((char)10).ToString()); break; case SENDSTATUS.PACKET_REJECT: break; case SENDSTATUS.PACKET_ERR: textBox9.AppendText(e.Msg); textBox9.AppendText(((char)10).ToString()); break; case SENDSTATUS.PACKET_TIMEOUT: textBox9.AppendText(e.Msg + "/n"); break; case SENDSTATUS.PACKET_SERVERUNKOWN: break; case SENDSTATUS.PACKET_CANNOTCROSSAPP: break; default: break; } } }
//解析 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)); } } } }