예제 #1
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);
                    }
                }
            }
예제 #2
0
            public void ReadCallback(PacketData data)
            {
                try
                {
                    string[] messages = data.Messages;
                    foreach (string message in messages)
                    {
                        int commandEndIndex = message.IndexOf(' ');
                        string command, argument;
                        if (commandEndIndex > 0)
                        {
                            command = message.Substring(0, commandEndIndex);
                            argument = message.Substring(commandEndIndex + 1);
                        }
                        else
                        {
                            command = message;
                            argument = string.Empty;
                        }
                        //Form.AppendToLogTextBox(message, Color.Red);

                        if (Delegators.ContainsKey(command))
                        {
                            MessageDelegate action = Delegators[command];
                            action(argument);
                        }
                        else
                        {
                            Form.AppendToLogTextBox(message);
                        }
                    }
                }
                catch (Exception e)
                {
                    Form.AppendToLogTextBox(e.ToString());
                }
            }
예제 #3
0
        void AsyncReadCallback(IAsyncResult ar)
        {
            //handler.Log("ReceiveCallback");

            try
            {
                // Read data from the remote device.
                int bytesRead = _socket.EndReceive(ar);
                if (bytesRead == 0)
                {
                    Disconnect();
                }
                else
                {
                    PacketData data = new PacketData(readBuffer, bytesRead);

                    // Process message here
                    handler.ReadCallback(data);

                    ReadNext();
                }
            }
            catch (Exception e)
            {
                handler.Log(e.ToString());
                Disconnect();
            }
        }
예제 #4
0
 public void ReadCallback(PacketData message)
 {
     Console.WriteLine("Received message: {0}", message);
 }
예제 #5
0
        void AsyncReadCallback(IAsyncResult ar)
        {
            try
            {
                IPEndPoint remoteEP = null;
                Byte[] bytes = client.EndReceive(ar, ref remoteEP);
                PacketData data = new PacketData(bytes, bytes.Length);

                handler.ReadCallback(data);

                ReadNext();
            }
            catch (NullReferenceException)
            {
                Disconnect();
            }
            catch (Exception ex)
            {
                handler.Log(ex.ToString());
                Disconnect();
            }
        }