Exemplo n.º 1
0
        public object Cmd(ProtoCommand cmd, string uri, long start, long len, string file = "")
        {
            if (file == "")
            {
                file = uri;
            }
            var state = new AgentState(Config, Fs)
            {
                FileName = file,
                Gram     = new ProtoGram(cmd, start, len, uri)
            };

            try
            {
                var ipHostInfo = Dns.GetHostEntry(Config.Host);
                var ipAddress  = ipHostInfo.AddressList
                                 .First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
                var remoteEp = new IPEndPoint(ipAddress, Config.Port);
                var client   = new Socket(ipAddress.AddressFamily,
                                          SocketType.Stream, ProtocolType.Tcp);
                state.WorkSocket = client;
                client.BeginConnect(remoteEp, ConnectCallback, state);
                ConnectDone.WaitOne();
                Send(state);
                SendDone.WaitOne();
                Receive(state);
                AllDone.WaitOne();
                if (state.Gram.Status == ProtoStatus.Success)
                {
                    return(Complete(state));
                }
                else
                {
                    throw new ApplicationException(state.Url);
                }
            }
            catch (Exception e)
            {
                Abort(state, e);
                return(null);
            }
        }
Exemplo n.º 2
0
 private void Send(AgentState state)
 {
     state.Send();
     state.WorkSocket.BeginSend(state.Buffer, 0, state.BufferLen, 0,
                                SendCallback, state);
 }