Esempio n. 1
0
 public EventSubscription SetupCallback(NotificationReceivedDelegate eventAction)
 {
     OnMessageReceived += eventAction;
     return(this);
 }
Esempio n. 2
0
        public void Start(int port, int messageSize, NotificationReceivedDelegate received)
        {
            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the
            // host running the application.
            var localEndPoint = new IPEndPoint(IPAddress.Any, port);

            // Create a TCP/IP socket.
            listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and
            // listen for incoming connections.
            listener.Bind(localEndPoint);
            listener.Listen(10);

            try
            {

                // Start listening for connections.
                while (!cancellationTokenSource.IsCancellationRequested)
                {
                    // Program is suspended while waiting for an incoming connection.
                    handler = listener.Accept();
                    handler.ReceiveTimeout = 3000;
                    handler.ReceiveBufferSize = messageSize;

                    // An incoming connection needs to be processed.
                    while (!cancellationTokenSource.IsCancellationRequested)
                    {
                        var buffer = new List<byte>();
                        bool disconnected = false;

                        while (buffer.Count < messageSize)
                        {
                            var bytes = new byte[messageSize];
                            var bytesRec = handler.Receive(bytes, 0, bytes.Length, SocketFlags.None);

                            for (int i = 0; i < bytesRec; i++)
                                buffer.Add(bytes[i]);

                            if (bytesRec <= 0)
                            {
                                disconnected = true;
                                break;
                            }
                        }

                        if (disconnected)
                            break;

                        var recdBuffer = buffer;

                        Int16 msgLength = 0;

                        //See if we have the msg length
                        if (recdBuffer.Count >= 45)

                        {
                            var lenBytes = recdBuffer.GetRange(43, 2).ToArray();

                            var payloadLen = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(lenBytes.ToArray(), 0));

                            msgLength = (Int16)((Int16)payloadLen + (Int16)45);

                            //See if we have the full msg
                            if (recdBuffer.Count >= msgLength)
                            {

                                var payload = System.Text.Encoding.UTF8.GetString(recdBuffer.GetRange(45, payloadLen).ToArray());

                                var identifierBytes = recdBuffer.GetRange(1, 4);
                                var tokenBytes = recdBuffer.GetRange(11, 32);

                                var identifier = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(identifierBytes.ToArray(), 0));
                                var token = BitConverter.ToString(tokenBytes.ToArray()).Replace("-", "");

                                recdBuffer.RemoveRange(0, msgLength);

                                //	System.Threading.Thread.Sleep(rnd.Next(10, 100));

                                bool hadError = false;

                                foreach (var f in ResponseFilters)
                                {
                                    if (f.IsMatch(identifier, token, payload))
                                    {
                                        var b2 = new byte[6];
                                        b2[0] = 0x01;
                                        b2[1] = BitConverter.GetBytes((short)f.Status)[0];

                                        Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(identifier)), 0, b2, 2, 4);

                                        //stream.Write(b2, 0, b2.Length);
                                        hadError = true;
                                        handler.Send(b2);

                                        received(false, identifier, token, payload);

                                        break;
                                    }
                                }

                                if (hadError)
                                    break;

                                //Process the full msg
                                received(true, identifier, token, payload);
                            }

                        }
                        else
                        {
                            var b2 = new byte[6];
                            b2[0] = 0x01;
                            b2[1] = BitConverter.GetBytes((short)ApnsResponseStatus.InvalidPayloadSize)[0];
                            handler.Send(b2);
                            break;
                        }
                    }

                    try { handler.Shutdown(SocketShutdown.Both); }
                    catch { }

                    try { handler.Close(); }
                    catch { }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            waitFinished.Set();
        }