コード例 #1
0
        internal static SubscriberClient Connect(IPEndPoint endPoint, string name, string topic, Type type, Action <object> action, Action <IPEndPoint> disconnected)
        {
            Log.Debug(Tag, "Connecting to " + endPoint);
            try
            {
                var serialiser = new ProtoSerialiser();
                var tcpClient  = new TcpClient {
                    NoDelay = true, LingerState = { Enabled = true, LingerTime = 0 }
                };
                tcpClient.Connect(endPoint);
                var networkStream = tcpClient.GetStream();

                // handshake
                serialiser.Serialise(networkStream, new Header {
                    Topic = topic, Type = type.FullName, Name = name
                });
                var ack = serialiser.Deserialize <string>(networkStream);

                if (ack != "OK")
                {
                    return(null);
                }

                Log.Debug(Tag, "Subscribing started.");

                return(new SubscriberClient(endPoint, type, action, disconnected, tcpClient, networkStream, serialiser));
            }
            catch (InvalidOperationException)
            {
                Log.Debug(Tag, "Connect: InvalidOperationException");
            }
            catch (SocketException e)
            {
                Log.Debug(Tag, "Connect: SocketException: " + e.Message + " [SocketErrorCode:" + e.SocketErrorCode + "]");
            }
            catch (ProtoSerialiserException)
            {
                Log.Error(Tag, "Connect: ProtoSerialiserException");
            }
            catch (IOException e)
            {
                Log.Debug(Tag, "Connect: IOException: " + e.Message);
            }

            Log.Debug(Tag, "Error connecting to " + endPoint);

            return(null);
        }
コード例 #2
0
        private SubscriberClient(IPEndPoint endPoint, Type type, Action <object> action, Action <IPEndPoint> disconnected, TcpClient tcpClient, NetworkStream networkStream, ProtoSerialiser serialiser)
        {
            _endPoint      = endPoint;
            _type          = type;
            _action        = action;
            _disconnected  = disconnected;
            _tcpClient     = tcpClient;
            _networkStream = networkStream;
            _serialiser    = serialiser;

            _consumerThread = new Thread(Consume)
            {
                IsBackground = true
            };
            _consumerThread.Start();
        }