Exemplo n.º 1
0
        void ProcessCapturedPacket(PacketDotNet.Packet packet)
        {
            if (!(packet is PacketDotNet.EthernetPacket)) return;
            var eth = (PacketDotNet.EthernetPacket)packet;

            byte[] bytes = eth.Bytes;
            if (!Utility.IsPspPacket(bytes, Utility.HEADER_OFFSET))
            {
                // PSPのパケットではないのでスルー
                return;
            }
            string src = eth.SourceHwAddress.ToString();
            string dest = eth.DestinationHwAddress.ToString();

            // サーバーから送られてきた他プレイヤーのPSPパケットか判別
            if (RemoteMacAddresses.ContainsKey(src))
            {
                //string log = String.Format("PSP Packet: サーバーから自PSPへ転送するパケットなのでスルーします: {0} -> {1}", src, desc);
                //AppendToLogTextBox(log, Color.DeepPink);
                return;
            }

            bool packetToRemotePSP = true;
            TraficStatistics myStats;
            lock (MyMacAddresses)
            {
                if (MyMacAddresses.ContainsKey(src))
                {
                    myStats = MyMacAddresses[src];
                }
                else
                {
                    myStats = new TraficStatistics();
                    MyMacAddresses[src] = myStats;
                }

                // 手元に送信先MACアドレスのPSPがあるか判別
                if (MyMacAddresses.ContainsKey(dest))
                {
                    packetToRemotePSP = false;
                    // not need tunneling
                    //string log = String.Format("PSP Packet: Local trafic from {0} -> {1}", src, desc);
                    //AppendToLogTextBox(log, Color.Purple);
                }
            }

            if (packetToRemotePSP && TunnelIsLinked)
            {
                TraficStatistics remoteStats;
                lock (RemoteMacAddresses)
                {
                    if (RemoteMacAddresses.ContainsKey(dest))
                    {
                        remoteStats = RemoteMacAddresses[dest];
                    }
                    else
                    {
                        remoteStats = new TraficStatistics();
                        RemoteMacAddresses[dest] = remoteStats;
                    }
                }

                //string log = String.Format("PSP Packet: {0} -> {1} Size: {2}", src, desc, bytes.Length);
                if (CurrentOperationMode == OperationMode.PlayRoomMaster || CurrentOperationMode == OperationMode.PlayRoomParticipant)
                {
                    myStats.lastModified = System.DateTime.Now.Ticks;
                    myStats.currentOutBytes += bytes.Length;
                    myStats.totalOutBytes += bytes.Length;

                    remoteStats.lastModified = System.DateTime.Now.Ticks;
                    remoteStats.currentOutBytes += bytes.Length;
                    remoteStats.totalOutBytes += bytes.Length;

                    if (Utility.HEADER_OFFSET == 0)
                    {
                        //AppendToLogTextBox(System.DateTime.Now.Ticks.ToString(), Color.Black);
                        ArenaTunnelClient.Send(bytes.ToArray());
                    }
                    //else
                    //{
                    //    //long timestamp = Environment.TickCount;
                    //    long timestamp = System.DateTime.Now.Ticks;
                    //    AppendToLogTextBox(timestamp.ToString());
                    //    byte[] ticks = BitConverter.GetBytes(timestamp);
                    //    ArenaTunnelClient.Send(ticks.Concat(bytes).ToArray());
                    //}
                }
            }
        }
Exemplo n.º 2
0
            public void ReadCallback(PacketData data)
            {
                byte[] packet = data.RawBytes;
                if (Utility.IsPspPacket(packet, Utility.HEADER_OFFSET))
                {
                    byte[] buff = new byte[6];
                    Array.Copy(packet, Utility.HEADER_OFFSET, buff, 0, 6);
                    string dest = new PhysicalAddress(buff).ToString();
                    Array.Copy(packet, Utility.HEADER_OFFSET + 6, buff, 0, 6);
                    string src = new PhysicalAddress(buff).ToString();

                    TraficStatistics remoteStats;
                    // 再度パケットキャプチャされた時にサーバーへ再送しないために記録
                    lock (Form.RemoteMacAddresses)
                    {
                        //long ticks = Utility.HEADER_OFFSET == 0 ? System.DateTime.Now.Ticks : BitConverter.ToInt64(packet, 0);
                        if (Form.RemoteMacAddresses.ContainsKey(src))
                        {
                            remoteStats = Form.RemoteMacAddresses[src];
                            //if (lastTicks > ticks) // 古いパケットはスルー
                            //    return;
                        }
                        else
                        {
                            remoteStats = new TraficStatistics();
                            Form.RemoteMacAddresses[src] = remoteStats;
                        }
                    }

                    //PhysicalAddress destMac = new PhysicalAddress(packet.Skip(Utility.HEADER_OFFSET).Take(6).ToArray());
                    //string log = String.Format("Packet To My PSP: {0} -> {1} Size={2}", src, destMac, packet.Length);
                    //Form.AppendToLogTextBox(log, Color.Black);

                    if (Form.CurrentPcapDevice != null && Form.PacketCapturing)// Form.CurrentPcapDevice.Started)
                    {
                        remoteStats.lastModified = System.DateTime.Now.Ticks;
                        remoteStats.currentInBytes += packet.Length;
                        remoteStats.totalInBytes += packet.Length;

                        TraficStatistics myStats = null;
                        lock (Form.MyMacAddresses)
                            if (Form.MyMacAddresses.ContainsKey(dest))
                            {
                                myStats = Form.MyMacAddresses[dest];
                                //dest = MAC_BROADCAST_ADDRESS;
                                //packet[0] = 0xFF;
                                //packet[1] = 0xFF;
                                //packet[2] = 0xFF;
                                //packet[3] = 0xFF;
                                //packet[4] = 0xFF;
                                //packet[5] = 0xFF;
                            }
                            else if (dest != MAC_BROADCAST_ADDRESS)
                            {
                                myStats = new TraficStatistics();
                                Form.MyMacAddresses[dest] = myStats;
                            }

                        if (myStats == null) // dest == 'FFFFFFFFFFFF"
                        {
                            lock (Form.RemoteMacAddresses)
                            {
                                if (Form.RemoteMacAddresses.ContainsKey(dest))
                                {
                                    myStats = Form.RemoteMacAddresses[dest];
                                }
                                else
                                {
                                    myStats = new TraficStatistics();
                                    Form.RemoteMacAddresses[dest] = myStats;
                                }
                            }
                        }

                        myStats.lastModified = System.DateTime.Now.Ticks;
                        myStats.currentInBytes += packet.Length;
                        myStats.totalInBytes += packet.Length;

                        if (Utility.HEADER_OFFSET == 0)
                            Form.CurrentPcapDevice.SendPacket(packet);
                        else
                            Form.CurrentPcapDevice.SendPacket(packet.Skip(Utility.HEADER_OFFSET).ToArray());
                    }
                }
                else
                {
                    string routerTunnelPort = data.Messages[0];
                    //Form.AppendToLogTextBox("UDP : Informed my tunnel port " + routerTunnelPort, Color.Black);
                    int port;
                    if (Int32.TryParse(routerTunnelPort, out port))
                    {
                        Form.ArenaSessionClient.Send(Protocol1Constants.COMMAND_INFORM_TUNNEL_UDP_PORT + " " + port);
                    }
                }
            }