Example #1
0
        void sendDataMessage(DataMessage me, bool resend, bool twice, bool block)
        {
            lock (conn.clientControl.getSynlock())
            {
                long startTime = DateTime.Now.Ticks * 100;//c#没有Java的nano秒,只能用ticks*100来模拟
                long t1        = DateTime.Now.Millisecond;
                conn.clientControl.onSendDataPacket(conn);

                int timeId = conn.clientControl.getCurrentTimeId();

                me.create(timeId);

                SendRecord record_current = conn.clientControl.getSendRecord(timeId);
                if (!resend)
                {
                    //第一次发,修改当前时间记录
                    me.setFirstSendTimeId(timeId);
                    me.setFirstSendTime(DateTime.Now.Millisecond);
                    record_current.addSended_First(me.getData().Length);
                    record_current.addSended(me.getData().Length);
                }
                else
                {
                    //重发,修改第一次发送时间记录
                    SendRecord record = conn.clientControl.getSendRecord(me.getFirstSendTimeId());
                    record.addResended(me.getData().Length);
                    record_current.addSended(me.getData().Length);
                }

                try
                {
                    sendSum++;
                    sum++;
                    unAckMax++;

                    long t = DateTime.Now.Millisecond;
                    send(me.getDatagramPacket());

                    if (twice)
                    {
                        send(me.getDatagramPacket());//发两次
                    }
                    if (block)
                    {
                        conn.clientControl.sendSleep(startTime, me.getData().Length);
                    }
                    TrafficEvent tevent = new TrafficEvent("", (long)Math.Floor(ran.NextDouble() * 1000000000D), me.getData().Length, TrafficEvent.type_uploadTraffic);
                    Route.fireEvent(tevent);
                }
                catch (Exception e)
                {
                    MLog.info(e.Message);
                }
            }
        }
        public SendRecord getSendRecord(int timeId)
        {
            SendRecord record = null;

            lock (syn_timeid)
            {
                record = sendRecordTable[timeId];
                if (record == null)
                {
                    record = new SendRecord();
                    record.setTimeId(timeId);
                    sendRecordTable.Add(timeId, record);
                }
            }
            return(record);
        }
Example #3
0
        public void onReceivePacket(DatagramPacket dp)
        {
            DataMessage me;

            if (dp != null)
            {
                if (conn.isConnected())
                {
                    int ver   = MessageCheck.checkVer(dp);
                    int sType = MessageCheck.checkSType(dp);
                    if (ver == RUDPConfig.protocal_ver)
                    {
                        conn.live();
                        if (sType == MessageType.sType_DataMessage)
                        {
                            me = new DataMessage(dp);
                            int        timeId = me.getTimeId();
                            SendRecord record = conn.clientControl.sendRecordTable_remote[timeId];
                            if (record == null)
                            {
                                record = new SendRecord();
                                record.setTimeId(timeId);
                                conn.clientControl.sendRecordTable_remote.Add(timeId, record);
                            }
                            record.addSended(me.getData().Length);

                            if (timeId > currentRemoteTimeId)
                            {
                                currentRemoteTimeId = timeId;
                            }

                            int sequence = me.getSequence();

                            conn.sender.sendAckDelay(me.getSequence());
                            if (sequence > lastRead)
                            {
                                lock (availOb)
                                {
                                    receiveTable.Add(sequence, me);
                                    if (receiveTable.ContainsKey(lastRead + 1))
                                    {
                                        Monitor.Pulse(availOb);   //availOb.notify();//不知对不对
                                    }
                                }
                            }
                        }
                        else if (sType == MessageType.sType_AckListMessage)
                        {
                            AckListMessage alm       = new AckListMessage(dp);
                            int            lastRead3 = alm.getLastRead();
                            if (lastRead3 > lastRead2)
                            {
                                lastRead2 = lastRead3;
                            }
                            List <int> ackList = alm.getAckList();

                            for (int i = 0; i < ackList.Count; i++)
                            {
                                int sequence = ackList[i];
                                conn.sender.removeSended_Ack(sequence);
                            }
                            SendRecord rc1 = conn.clientControl.getSendRecord(alm.getR1());
                            if (rc1 != null)
                            {
                                if (alm.getS1() > rc1.getAckedSize())
                                {
                                    rc1.setAckedSize(alm.getS1());
                                }
                            }

                            SendRecord rc2 = conn.clientControl.getSendRecord(alm.getR2());
                            if (rc2 != null)
                            {
                                if (alm.getS2() > rc2.getAckedSize())
                                {
                                    rc2.setAckedSize(alm.getS2());
                                }
                            }

                            SendRecord rc3 = conn.clientControl.getSendRecord(alm.getR3());
                            if (rc3 != null)
                            {
                                if (alm.getS3() > rc3.getAckedSize())
                                {
                                    rc3.setAckedSize(alm.getS3());
                                }
                            }

                            if (checkWin())
                            {
                                conn.sender.play();
                            }
                        }
                        else if (sType == MessageType.sType_CloseMessage_Stream)
                        {
                            CloseMessage_Stream cm = new CloseMessage_Stream(dp);
                            reveivedClose = true;
                            int n = cm.getCloseOffset();
                            closeStream_Remote(n);
                        }
                        else if (sType == MessageType.sType_CloseMessage_Conn)
                        {
                            CloseMessage_Conn cm2 = new CloseMessage_Conn(dp);
                            conn.close_remote();
                        }
                        else
                        {
                            ////#MLog.println("未处理数据包 "+sType);
                        }
                    }
                }
            }
        }