예제 #1
0
파일: Network.cs 프로젝트: ifzz/FDK
        static bool ReceiveEx(IntPtr socket, Timeout timeout, void* data, int size)
        {
            var set = fd_set.Create(socket);
            var buffer = (byte*)data;
            for (; size > 0; )
            {
                var t = timeout.ToTime();
                var status = WinAPI.select(1, &set, null, null, &t);
                if (status != 1)
                    return false;

                status = WinAPI.recv(socket, buffer, size, 0);
                if (status <= 0)
                    return false;

                buffer += status;
                size -= status;
            }

            return true;
        }
예제 #2
0
파일: StClient.cs 프로젝트: ifzz/FDK
        unsafe void Receive(MemoryBuffer data, ref Timeout timeout)
        {
            data.Reset();
            var set = fd_set.Create(this.socket.Handle);
            var empty = fd_set.Null;

            // receiving package size and any additional data

            var length = data.Capacity;
            var buffer = (byte*)data.Data;

            var total = 0;
            for (; total < 4;)
            {
                var interval = timeout.ToTime();

                var status = WinAPI.select(1, &set, null, null, &interval);
                if (status == 0)
                    throw new TimeoutException("Timeout of receiving has been reached.");

                status = WinAPI.recv(this.socket.Handle, buffer, length, 0);
                if (status <= 0)
                    throw new DisconnectedException();

                buffer += status;
                total += status;
                length -= status;
            }
            data.Construct(total);
            var size = data.ReadInt32();
            data.Construct(sizeof(int) + size);

            length = size - total + sizeof(int); // remaining data length

            if (length < 0) // we read more than expected
                throw new DisconnectedException();

            if (length == 0) // we read all data
                return;

            buffer = (byte*)data.Data;
            buffer += total;
            for (; length > 0; )
            {
                var interval = timeout.ToTime();

                var status = WinAPI.select(1, &set, null, null, &interval);
                if (status == 0)
                    throw new TimeoutException("Timeout of receiving has been reached.");

                status = WinAPI.recv(this.socket.Handle, buffer, length, 0);
                if (status <= 0)
                    throw new DisconnectedException();

                buffer += status;
                length -= status;
            }
        }
예제 #3
0
파일: Network.cs 프로젝트: ifzz/FDK
        public static bool ReceiveEx(IntPtr socket, Timeout timeout, MemoryBuffer data)
        {
            var set = fd_set.Create(socket);

            data.Reset();

            var length = data.Capacity;
            var buffer = (byte*)data.Data;

            var total = 0;
            for (; total < 4; )
            {
                var interval = timeout.ToTime();

                var status = WinAPI.select(1, &set, null, null, &interval);
                if (status == 0)
                    return false;

                status = WinAPI.recv(socket, buffer, length, 0);
                if (status <= 0)
                    return false;

                buffer += status;
                total += status;
                length -= status;
            }
            data.Construct(total);
            var size = data.ReadInt32();
            data.Construct(sizeof(int) + size);

            length = size - total + sizeof(int); // remaining data length

            if (length < 0) // we read more than expected
                return false;

            if (length == 0) // we read all data
                 return true;
 
            buffer = (byte*)data.Data;
            buffer += total;
            for (; length > 0; )
            {
                var interval = timeout.ToTime();

                var status = WinAPI.select(1, &set, null, null, &interval);
                if (status == 0)
                    return false;

                status = WinAPI.recv(socket, buffer, length, 0);
                if (status <= 0)
                    return false;

                buffer += status;
                length -= status;
            }

            return true;
        }
예제 #4
0
파일: StClient.cs 프로젝트: ifzz/FDK
        unsafe void Send(MemoryBuffer data, ref Timeout timeout)
        {
            var length = data.Size;
            var buffer = (Byte*)data.Data;
            var set = fd_set.Create(this.socket.Handle);

            for (; length > 0; )
            {
                var interval = timeout.ToTime();

                var status = WinAPI.select(1, null, &set, null, &interval);
                if (status == 0)
                    throw new TimeoutException("Timeout of sending has been reached.");

                status = WinAPI.send(this.socket.Handle, buffer, length, 0);
                if (status <= 0)
                    throw new DisconnectedException();

                buffer += status;
                length -= status;
            }
        }