예제 #1
0
        public void Connect <T>(IPAddress address,
                                int port,
                                AccessToData <T> accessToData,
                                GetBytes <T> bytesGetter,
                                StoreAction <T> store,
                                ActionWithData <T> action,
                                StopCondition <T> closeConnectionCondition) where T : class
        {
            IPEndPoint ipEndPoint = new IPEndPoint(address, port);
            Socket     sender     = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            sender.Connect(ipEndPoint);
            byte[] buffer = new byte[config.BufferSize];
            while (true)
            {
                T data = accessToData();
                if (closeConnectionCondition(data))
                {
                    onCloseConnection();
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                    return;
                }
                byte[] dataBytes = bytesGetter(data);
                int    bytesRest = dataBytes.Length;
                while (bytesRest > 0)
                {
                    int bytesSent = sender.Send(dataBytes);
                    dataBytes  = dataBytes.Skip(bytesSent).ToArray();
                    bytesRest -= bytesSent;
                }
                while (sender.Available > 0)
                {
                    int byteRecived = sender.Receive(buffer);
                    data = store(buffer, byteRecived, data);
                }
                data = action(data);
            }
        }
 public void SetUp()
 {
     _statServices = new StatServices();
     _webClient    = new WebClient();
     _accessToData = new AccessToData();
 }
 public StatServices()
 {
     dataAccess = new AccessToData();
 }