Exemplo n.º 1
0
        //データの受信
        //パラメータ cs CS.SERVER を設定した場合、buf[CS.SERVER]を処理対象とし、クライアント側に送信する
        bool RecvBuf(CS cs, ILife iLife)
        {
            SockTcp sock = Proxy.Sock(cs);

            if (sock == null)//サーバ側未接続
            {
                return(true);
            }

            var len = sock.Length();

            if (len == 0)
            {
                return(true);
            }
            var b = sock.Recv(len, Proxy.OptionTimeout, iLife);

            if (b == null)
            {
                return(false);
            }
            _oneObj.Body[cs].Add(b);
            _lastRecvServer = DateTime.Now.Ticks;
            return(true);
        }
Exemplo n.º 2
0
 void Pipe()
 {
     _sockTcp = SockServer.CreateConnection(_kernel, _ip, _listenPort, null, this);
     if (_sockTcp != null)
     {
         while (_life)
         {
             var len = _sockTcp.Length();
             if (len > 0)
             {
                 const int tout = 3; //受信バイト数がわかっているので、ここでのタイムアウト値はあまり意味が無い
                 var       b    = _sockTcp.Recv(len, tout, this);
                 if (b != null)
                 {
                     _buffer = Bytes.Create(_buffer, b);
                 }
             }
             if (_sockTcp.Length() == 0 && _sockTcp.SockState != SockState.Connect)
             {
                 break;
             }
         }
         _sockTcp.Close();
     }
     IsRecv = false;
 }
Exemplo n.º 3
0
        //受信(無効な場合 return null)
        public static OneRemoteData Recv(SockTcp sockTcp, ILife iLife)
        {
            if (sockTcp != null)
            {
                //Ver5.8.6
                var sec = 10;                          //最初はタイムアウト値を最小に設定する
                var b   = sockTcp.Recv(1, sec, iLife); //REMOTE_DATA_KINDの受信
                if (b != null && b.Length == 1)
                {
                    var kind = (RemoteDataKind)b[0];

                    //これ以降は、データが到着しているはずなので、タイムアウト値を上げて待機する
                    //timeout = 3000;
                    //Ver5.8.6
                    sec = 10;
                    Thread.Sleep(1);
                    b = sockTcp.Recv(4, sec, iLife);//データサイズの受信

                    if (b != null && b.Length == 4)
                    {
                        var len = BitConverter.ToInt32(b, 0);
                        if (len == 0)
                        {
                            return(new OneRemoteData(kind, ""));//データ本体はサイズ0
                        }
                        //Ver5.8.6
                        b = new byte[0];
                        while (iLife.IsLife())
                        {
                            Thread.Sleep(1);
                            var buf = sockTcp.Recv(len, sec, iLife);//データ本体の受信
                            if (buf == null)
                            {
                                return(null);
                            }
                            b = Bytes.Create(b, buf);
                            if (b.Length == len)
                            {
                                return(new OneRemoteData(kind, Encoding.GetEncoding(932).GetString(b)));
                            }
                        }
                    }
                }
            }
            return(null);
        }
Exemplo n.º 4
0
        public void OneServerを継承したEchoServer_TCP版_を使用して接続する()
        {
            const string addr    = "127.0.0.1";
            const int    port    = 9999;
            const int    timeout = 300;
            Ip           ip      = null;

            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex) {
                Assert.Fail(ex.Message);
            }
            var oneBind = new OneBind(ip, ProtocolKind.Tcp);
            var conf    = TestUtil.CreateConf("OptionSample");

            conf.Set("port", port);
            conf.Set("multiple", 10);
            conf.Set("acl", new Dat(new CtrlType[0]));
            conf.Set("enableAcl", 1);
            conf.Set("timeOut", timeout);

            var echoServer = new EchoServer(conf, oneBind);

            echoServer.Start();

            //TCPクライアント

            const int max = 10000;
            var       buf = new byte[max];

            buf[8] = 100; //CheckData
            for (int i = 0; i < 3; i++)
            {
                var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

                sockTcp.Send(buf);

                while (sockTcp.Length() == 0)
                {
                    Thread.Sleep(2);
                }

                var len = sockTcp.Length();
                if (0 < len)
                {
                    var b = sockTcp.Recv(len, timeout, this);
                    Assert.That(b[8], Is.EqualTo(buf[8]));//CheckData
                }
                Assert.That(max, Is.EqualTo(len));

                sockTcp.Close();
            }

            echoServer.Dispose();
        }
Exemplo n.º 5
0
        //�t�@�C����M�i�o�C�i���j
        private int RecvBinary(SockTcp sockTcp, String fileName)
        {
            var sb = new StringBuilder();

            sb.Append(string.Format("RecvBinary({0}) ", fileName));

            var fs = new FileStream(fileName, FileMode.Create);
            var bw = new BinaryWriter(fs);

            fs.Seek(0, SeekOrigin.Begin);

            var       size    = 0;
            const int timeout = 3000;

            while (IsLife())
            {
                int len = sockTcp.Length();
                if (len < 0)
                {
                    break;
                }
                if (len == 0)
                {
                    if (sockTcp.SockState != Bjd.sock.SockState.Connect)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                    continue;
                }
                byte[] buf = sockTcp.Recv(len, timeout, this);
                if (buf.Length != len)
                {
                    throw new IOException("buf.length!=len");
                }
                bw.Write(buf, 0, buf.Length);

                //�g���[�X�\��
                sb.Append(string.Format("Binary={0}byte ", len));
                size += len;
            }
            bw.Flush();
            bw.Close();
            fs.Close();

            //noEncode = true; //�o�C�i���ł��鎖���������Ă���
            //Trace(TraceKind.Send, Encoding.ASCII.GetBytes(sb.ToString()), true); //�g���[�X�\��

            return(size);
        }
Exemplo n.º 6
0
        public void Echoサーバにsendで送信てtcpQueueのlength分ずつRecvする()
        {
            const string addr = "127.0.0.1";
            const int    port = 9981;

            var echoServer = new EchoServer(addr, port);

            echoServer.Start();

            const int timeout = 100;
            Ip        ip      = null;

            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex) {
                Assert.Fail(ex.Message);
            }
            var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

            const int max  = 1000;
            const int loop = 3;
            var       tmp  = new byte[max];

            for (var i = 0; i < max; i++)
            {
                tmp[i] = (byte)i;
            }

            int recvCount = 0;

            for (var i = 0; i < loop; i++)
            {
                var len = sockTcp.Send(tmp);
                Assert.That(len, Is.EqualTo(tmp.Length));

                Thread.Sleep(10);

                var b = sockTcp.Recv(len, timeout, this);
                recvCount += b.Length;
                for (int m = 0; m < max; m += 10)
                {
                    Assert.That(b[m], Is.EqualTo(tmp[m])); //送信したデータと受信したデータが同一かどうかのテスト
                }
            }
            Assert.That(loop * max, Is.EqualTo(recvCount)); //送信したデータ数と受信したデータ数が一致するかどうかのテスト

            sockTcp.Close();
            echoServer.Stop();
        }
Exemplo n.º 7
0
 private void Tcp(SockTcp sockTcp)
 {
     while (IsLife() && sockTcp.SockState == Bjd.sock.SockState.Connect)
     {
         Thread.Sleep(0); //これが無いと、別スレッドでlifeをfalseにできない
         var len = sockTcp.Length();
         if (0 < len)
         {
             const int timeout = 10;
             var       buf     = sockTcp.Recv(len, timeout, this);
             sockTcp.Send(buf);
             break; //echoしたらセッションを閉じる
         }
     }
 }
Exemplo n.º 8
0
        //通常はこれを使用する
        public bool Recv(SockTcp sockTcp, int sec, Logger logger, ILife iLife)
        {
            var dtLast = DateTime.Now; //受信が20秒無かった場合は、処理を中断する

            while (iLife.IsLife())
            {
                if (dtLast.AddSeconds(sec) < DateTime.Now)
                {
                    return(false); //タイムアウト
                }
                var len = sockTcp.Length();
                if (len == 0)
                {
                    continue;
                }
                var buf = sockTcp.Recv(len, sec, iLife);
                if (buf == null)
                {
                    return(false); //切断された
                }
                dtLast = DateTime.Now;

                var recvStatus = Append(buf);

                if (recvStatus == RecvStatus.Limit)
                {
                    //サイズ制限
                    if (logger != null)
                    {
                        logger.Set(LogKind.Secure, sockTcp, 7, string.Format("Limit:{0}KByte", _sizeLimit));
                    }
                    sockTcp.AsciiSend("552 Requested mail action aborted: exceeded storage allocation");
                    return(false);
                }
                if (recvStatus == RecvStatus.Finish)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 9
0
        //.行までを受信する
        byte [] RecvData()
        {
            var dt   = DateTime.Now;
            var line = new byte[0];

            while (_iLife.IsLife())
            {
                var now = DateTime.Now;
                if (dt.AddSeconds(_sec) < now)
                {
                    return(null); //タイムアウト
                }
                var len = _sockTcp.Length();
                if (len == 0)
                {
                    continue;
                }
                var buf = _sockTcp.Recv(len, _sec, _iLife);
                if (buf == null)
                {
                    return(null); //切断された
                }
                dt = now;

                var tmp = new byte[line.Length + buf.Length];
                Buffer.BlockCopy(line, 0, tmp, 0, line.Length);
                Buffer.BlockCopy(buf, 0, tmp, line.Length, buf.Length);
                line = tmp;
                if (line.Length >= 3)
                {
                    if (line[line.Length - 1] == '\n' && line[line.Length - 2] == '\r' && line[line.Length - 3] == '.')
                    {
                        return(line);
                    }
                }
            }
            return(null);
        }