private void CreateAsyncSocket(Socket socket, JobProcessor jobProcessor, IPacketAnalyzer packetAnalyzer) { this.asyncSocket = this.CreateAsyncSocket(socket, packetAnalyzer); this.crypto = ((packetAnalyzer == null) ? null : packetAnalyzer.CryptoTransform); if (jobProcessor == null) { this.asyncSocket.PacketReceive += delegate(object sender, EventArgs <ArraySegment <byte> > e) { this.OnPacketReceive(e); }; this.asyncSocket.SocketException += delegate(object sender, EventArgs <Exception> e) { this.OnExceptionOccur(e); }; this.asyncSocket.SocketClose += delegate(object sender, EventArgs e) { this.active = false; this.OnDisconnected(e); }; return; } this.asyncSocket.PacketReceive += delegate(object sender, EventArgs <ArraySegment <byte> > e) { jobProcessor.Enqueue(Job.Create <EventArgs <ArraySegment <byte> > >(new Action <EventArgs <ArraySegment <byte> > >(this.OnPacketReceive), e)); }; this.asyncSocket.SocketException += delegate(object sender, EventArgs <Exception> e) { jobProcessor.Enqueue(Job.Create <EventArgs <Exception> >(new Action <EventArgs <Exception> >(this.OnExceptionOccur), e)); }; this.asyncSocket.SocketClose += delegate(object sender, EventArgs e) { this.active = false; jobProcessor.Enqueue(Job.Create <EventArgs>(new Action <EventArgs>(this.OnDisconnected), e)); }; }
private void Analyze() { analyzeResults.Items.Clear(); var analyzers = new IPacketAnalyzer[] { new PascalStringPacketAnalyzer(), new ByteCountPacketAnalyzer(), new ConstantsPacketAnalyzer(), new VariableLengthStringAnalyzer(), new ContentPacketAnalyzer() }; var data = GetBytes(); foreach (var analyzer in analyzers) { var results = analyzer.Analyze(data); if (results != null) { foreach (var result in results) { analyzeResults.Items.Add(result); } } } }
private void EndConnect(IAsyncResult ar) { object[] array = (object[])ar.AsyncState; Socket socket = (Socket)array[0]; JobProcessor jobProcessor = (JobProcessor)array[1]; IPacketAnalyzer packetAnalyzer = (IPacketAnalyzer)array[2]; if (jobProcessor != null && !jobProcessor.IsInThread()) { jobProcessor.Enqueue(Job.Create <IAsyncResult>(new Action <IAsyncResult>(this.EndConnect), ar)); return; } try { socket.EndConnect(ar); this.CreateAsyncSocket(socket, jobProcessor, packetAnalyzer); this.OnConnectionSucceed(EventArgs.Empty); this.asyncSocket.Activate(); } catch (Exception value) { this.active = false; this.OnConnectionFail(new EventArgs <Exception>(value)); } }
internal void Activate(Socket connectedSocket, JobProcessor jobProcessor, IPacketAnalyzer packetAnalyzer) { try { this.active = true; this.CreateAsyncSocket(connectedSocket, jobProcessor, packetAnalyzer); this.LocalEndPoint = (connectedSocket.LocalEndPoint as IPEndPoint); this.RemoteEndPoint = (connectedSocket.RemoteEndPoint as IPEndPoint); this.Handle = connectedSocket.Handle; this.OnConnectionSucceed(EventArgs.Empty); this.asyncSocket.Activate(); } catch (Exception value) { this.active = false; this.OnConnectionFail(new EventArgs <Exception>(value)); } }
public void Connect(JobProcessor jobProcessor, IPEndPoint remoteEndPoint, IPacketAnalyzer packetAnalyzer) { if (this.active) { throw new InvalidOperationException("Can't use on connected instance"); } this.active = true; this.RemoteEndPoint = remoteEndPoint; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.BeginConnect(remoteEndPoint, new AsyncCallback(this.EndConnect), new object[] { socket, jobProcessor, packetAnalyzer }); this.LocalEndPoint = (socket.LocalEndPoint as IPEndPoint); this.Handle = socket.Handle; }
public AsyncSocket2(Socket socket, IPacketAnalyzer packetAnalyzer) { if (socket == null) { throw new ArgumentNullException("socket"); } if (packetAnalyzer == null) { throw new ArgumentNullException("packetAnalyzer"); } if (!socket.Connected) { throw new ArgumentException("Can't activate on closed socket.", "socket"); } if (socket.SocketType != SocketType.Stream || socket.AddressFamily != AddressFamily.InterNetwork || socket.ProtocolType != ProtocolType.Tcp) { throw new ArgumentException("Only TCP/IPv4 socket available.", "socket"); } LingerOption lingerOption = socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger) as LingerOption; if (lingerOption == null || lingerOption.Enabled) { throw new ArgumentException("Linger option must be disabled.", "socket"); } if (socket.UseOnlyOverlappedIO) { throw new ArgumentException("Socket must be bind on completion port.", "socket"); } this.socket = socket; this.packetAnalyzer = packetAnalyzer; this.sendQueue = new ConcurrentQueue <ArraySegment <byte> >(); IPEndPoint ipendPoint = (IPEndPoint)socket.RemoteEndPoint; this.RemoteAddress = ipendPoint.Address.GetAddressBytes(); this.RemotePort = ipendPoint.Port; this.receiveArg = AsyncSocket2.receiveArgsPool.Pop(); this.receiveArg.UserToken = this; this.sendArg = AsyncSocket2.sendArgsPool.Pop(); this.sendSize = 0; this.sendArg.UserToken = this; }
protected override IAsyncSocket CreateAsyncSocket(Socket socket, IPacketAnalyzer packetAnalyzer) { return(new AsyncSocket2(socket, packetAnalyzer)); }
protected virtual IAsyncSocket CreateAsyncSocket(Socket socket, IPacketAnalyzer packetAnalyzer) { return(new AsyncTcpWorker(socket, packetAnalyzer)); }
public void Connect(JobProcessor jobProcessor, string hostNameOrAddress, int port, IPacketAnalyzer packetAnalyzer) { IPAddress ipAddress; if (IPAddress.TryParse(hostNameOrAddress, out ipAddress)) { this.Connect(jobProcessor, ipAddress, port, packetAnalyzer); return; } if (this.active) { throw new InvalidOperationException("Can't use on connected instance"); } this.active = true; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.BeginConnect(hostNameOrAddress, port, new AsyncCallback(this.EndConnect), new object[] { socket, jobProcessor, packetAnalyzer }); }
public void Connect(JobProcessor jobProcessor, IPAddress ipAddress, int port, IPacketAnalyzer packetAnalyzer) { this.Connect(jobProcessor, new IPEndPoint(ipAddress, port), packetAnalyzer); }
public void Set(IPacketAnalyzer an) { packetAnalyzer = an; }