public void Add(PNode <T> x, T obj) { x.obj = obj; x.index = nr_ent; x.taken = false; nr_ent++; node_buffer.Add(x); }
public void Init() { efifo_rcv.Init(TCP_MAX_DEPTH_NUM); for (int i = 0; i < TCP_MAX_DEPTH_NUM; i++) { tyNode nnode = new tyNode(TCP_MAX_DATA_LEN); PNode <tyNode> pnode = new PNode <tyNode>(); nnode.pnode = pnode; epool_rcv.Add(pnode, nnode); } }
public void Update_TextBox(string text, int op) { PNode <tyShowOp> pnode = epool_show.Get(); tyShowOp show_node = pnode.obj; show_node.op = op; show_node.text = text; efifo_str_2_show.Input(show_node); event_txt_update.Set(); }
public void Put(PNode <T> free_node) { lock (epool_lock) { PNode <T> p = node_buffer[free_node.index]; Dbg.Assert(p.taken == true, "###pool put error1"); p.taken = false; nr_got--; DbgAddLog(-1, nr_got); if (nr_got < 0) { DbgDumpLog(); } Dbg.Assert(nr_got >= 0, "###pool put error2"); } }
public COM() { efifo_raw_2_str.Init(tyRcvNode.RCV_NODE_NUM); //eFIFO能管理8K个元素 for (int i = 0; i < tyRcvNode.RCV_NODE_NUM; i++) //每个元素8K大小,一共64MB,如果收的比做得快,那只能丢失了 { //第一次收到10个,不满攒住,第二次收到4096,则会溢出! tyRcvNode rnode = new tyRcvNode(tyRcvNode.RCV_CACHE_SIZE + COM_BUFFER_SIZE_MAX); PNode <tyRcvNode> pnode = new PNode <tyRcvNode>(); rnode.pnode = pnode; epool_rcv.Add(pnode, rnode); //Dbg.WriteLine("Add node:{0} to ePool", rnode.GetHashCode()); } efifo_str_2_show.Init(tyShowOp.SHOW_NODE_NUM); //上面采用eFIFO搬运 for (int i = 0; i < tyShowOp.SHOW_NODE_NUM; i++) { tyShowOp snode = new tyShowOp(); PNode <tyShowOp> pnode = new PNode <tyShowOp>(); snode.pnode = pnode; epool_show.Add(pnode, snode); //Dbg.WriteLine("Add node:{0} to ePool", rnode.GetHashCode()); } timer_AutoSnd = new System.Timers.Timer(); //实例化Timer类,设置间隔时间为1000毫秒 timer_AutoSnd.Elapsed += new System.Timers.ElapsedEventHandler(timer_AutoSnd_Tick); //到达时间的时候执行事件 timer_AutoSnd.AutoReset = true; //设置是执行一次(false)还是一直执行(true) timer_AutoSnd.Enabled = false; //是否执行System.Timers.Timer.Elapsed事件 timer_AutoSnd.Interval = cfg.auto_send_inverval_100ms * 100; timer_RcvFlush = new System.Timers.Timer(); timer_RcvFlush.Elapsed += new System.Timers.ElapsedEventHandler(timer_RcvFlush_Tick); timer_RcvFlush.AutoReset = false; timer_RcvFlush.Enabled = false; timer_RcvFlush.Interval = 500; }
public void ThreadEntry_ServerRcv() //线程入口 { Enter_MessageQueue(is_server, false, "ThreadEntry_ServerRcv run"); while (true) { while (true) { IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(str_ip), port); //本地端地址 Listener = new TcpListener(localEP); //建立监听类,并绑定到指定的端地址 try { Listener.Start(); //开始监听 } catch (Exception ex) { Listener.Stop(); Listener = null; Enter_MessageQueue(is_server, false, "Error local IP:" + localEP.ToString() + "setup. " + ex.Message); Thread.Sleep(500); continue; } Enter_MessageQueue(is_server, false, "IP setup ok!"); DateTime listener_pending_mark; DateTime listener_pending_timeout; listener_pending_mark = DateTime.Now; while (true) { if (Listener.Pending() == false) { //为了避免每次都被tcpListener.AcceptTcpClient()阻塞线程,添加了此判断, //no connection requests have arrived //当没有连接请求时,什么也不做,有了请求再执行到tcpListener.AcceptTcpClient() listener_pending_timeout = DateTime.Now; TimeSpan ts = listener_pending_timeout - listener_pending_mark; int second_pass = ts.Minutes * 60 + ts.Seconds; if (second_pass >= 5) { Listener.Stop(); Listener = null; Enter_MessageQueue(is_server, false, "Can't find any client"); Thread.Sleep(500); break; } } else { remoteClient = Listener.AcceptTcpClient(); //等待连接(阻塞) network_stream_server = remoteClient.GetStream(); br_server_read_from_client = new BinaryReader(network_stream_server); bw_server_write_to_client = new BinaryWriter(network_stream_server); is_active = true; break; } } if (is_active == true) { break; } } Enter_MessageQueue(is_server, false, "Tcp is active!"); while (true) //循坏 接收/发送 数据 { /********************接收数据部分 Start******************/ int rcv_length; PNode <tyNode> pnode = epool_rcv.Get(); tyNode nnode = pnode.obj; try { //接收客户端发送的数据 rcv_length = br_server_read_from_client.Read(nnode.buffer, 0, TCP_MAX_DATA_LEN); } catch (Exception ex) { Enter_MessageQueue(is_server, false, "Client lost, Read fail!" + ex.Message); is_active = false; break; } #if SUPPORT_SHOW_LEN Dbg.WriteLine("Servre rcv:{0}", rcv_length); #endif #if SUPPORT_SHOW_DATA Func.DumpBuffer(efifo_rcv.Peek(), rcv_length); #endif if (rcv_length == 0) { epool_rcv.Put(pnode); Enter_MessageQueue(is_server, false, "Client lost, Read error!"); is_active = false; break; } else { nnode.length = rcv_length; efifo_rcv.Input(nnode); } /********************接收数据部分 End********************/ } } //TcpMessage(is_server, false, "ThreadEntry_ServerRcv end"); }
public void ThreadEntry_ClientRcv() { Enter_MessageQueue(is_server, false, "ThreadEntry_ClientRcv run"); while (true) { IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(str_ip), port); //远程服务器端地址; remoteServer = new TcpClient(); try { remoteServer.Connect(remoteEP); //调用connect方法连接远端服务器; } catch (Exception ex) { remoteServer.Close(); remoteServer = null; Enter_MessageQueue(is_server, false, "Can't connect to server:" + str_ip + ", " + ex.Message); Thread.Sleep(500); continue; } is_active = true; network_stream_client = remoteServer.GetStream(); bw_client_read_from_server = new BinaryReader(network_stream_client); bw_client_write_to_server = new BinaryWriter(network_stream_client); Enter_MessageQueue(is_server, false, "Connect to server successfully:" + str_ip); Dbg.WriteLine("I'm using {0}.", remoteServer.Client.LocalEndPoint); //打印自己使用的端地址 while (true) { /********************接收数据部分 Start******************/ int rcv_length; PNode <tyNode> pnode = epool_rcv.Get(); tyNode nnode = pnode.obj; try { //接受服务器发送过来的消息,注意Client已经把数据处理成字符串了! rcv_length = bw_client_read_from_server.Read(nnode.buffer, 0, TCP_MAX_DATA_LEN); } catch (Exception ex) { epool_rcv.Put(pnode); Enter_MessageQueue(is_server, false, "Server lost, Read fail!" + ex.Message); is_active = false; break; } #if SUPPORT_SHOW_LEN Dbg.WriteLine("Client rcv:{0}", rcv_length); #endif #if SUPPORT_SHOW_DATA Func.DumpBuffer(efifo_rcv.Peek(), rcv_length); #endif if (rcv_length == 0) { epool_rcv.Put(pnode); Enter_MessageQueue(is_server, false, "Server lost, Read error!"); is_active = false; break; } else { nnode.length = rcv_length; efifo_rcv.Input(nnode); } /********************接收数据部分 End********************/ } } //TcpMessage(is_server, false, "ThreadEntry_ClientRcv end"); }
void ISR_COM_DataRec(object sender, SerialDataReceivedEventArgs e) //串口接受函数 { rcv_recving = true; last_rcv_data_time = DateTime.Now; timer_RcvFlush.Stop(); timer_RcvFlush.Enabled = false;//timer重新计时 timer_RcvFlush.Enabled = true; if ((COM_Op.com_is_closing == true) || (serialport.IsOpen == false) || (rcv_flushing == true)) { rcv_recving = false; return; } event_recv.Set(); //无论有没有资源,都唤醒recv线程去取FIFO //如果FIFO已经满了,最后一个current_rnode会一直接一直接,然后突破了buffer的长度 if (efifo_raw_2_str.is_full == true) { Dbg.WriteLine("###1.COM:{0} recv fifo is full:{1}, data miss!!!", serialport.IsOpen, efifo_raw_2_str.GetValidNum()); UpdateMissData(); rcv_recving = false; return; } if (current_rnode == null) { PNode <tyRcvNode> pnode = epool_rcv.Get(); if (pnode == null) { Dbg.WriteLine("###COM:{0} recv pool is full:{1}({2}), data miss!!!", serialport.IsOpen, epool_rcv.nr_got, epool_rcv.nr_ent); UpdateMissData(); rcv_recving = false; return; } else { current_rnode = pnode.obj; } current_rnode = pnode.obj; current_rnode.length = 0; //把长度清零,避免长度越界 } int com_recv_buff_length = serialport.Read(current_rnode.buffer, current_rnode.length, serialport.ReadBufferSize); if (com_recv_buff_length > 0) { bbbb = DateTime.Now; current_rnode.length += com_recv_buff_length; #if false current_rnode.log_len[current_rnode.log_cnt % 128] = com_recv_buff_length; current_rnode.log_sz[current_rnode.log_cnt % 128] = current_rnode.length; current_rnode.log_cnt++; #endif //当缓存较多的时候,优先提高平滑性(收得太慢/显示得太慢) if ((epool_rcv.nr_got < epool_rcv.nr_ent / 128) && //64 (epool_show.nr_got < epool_show.nr_ent / 128)) { handle_data_thresdhold = 0; } else if ((epool_rcv.nr_got < epool_rcv.nr_ent / 64) && //128 (epool_show.nr_got < epool_show.nr_ent / 64)) { handle_data_thresdhold = 1024; } else if ((epool_rcv.nr_got < epool_rcv.nr_ent / 32) || //256 (epool_show.nr_got < epool_show.nr_ent / 32)) { handle_data_thresdhold = tyRcvNode.RCV_CACHE_SIZE; } if (current_rnode.length >= handle_data_thresdhold) { if (efifo_raw_2_str.is_full == true) { Dbg.WriteLine("###2.COM:{0} recv fifo is full:{1}, data miss!!!", serialport.IsOpen, efifo_raw_2_str.GetValidNum()); record.miss_data += (uint)current_rnode.length; rcv_recving = false; return; } efifo_raw_2_str.Input(current_rnode); current_rnode = null; } else { rcv_recving = false; return; } event_recv.Set(); #if false Dbg.Write("RECA[{0}]: in:{1}-{2} out:{3}-{4}", com_recv_buff_length, com_recv_fifo_top, com_recv_fifo_buttom, fp_out_top, fp_out_buttom); for (int v = 0; v < com_recv_buff_length; v++) { Dbg.Write(" {0:X}", rcv_fifo.buffer[v]); } Dbg.Write("\r\n"); #endif rcv_recving = false; } }