예제 #1
0
        public int Recieve(byte[] buffer, int offset, int readByteCount)
        {
            Debug.Assert(InteralSocket != IntPtr.Zero);
            if ((readByteCount + offset) > buffer.Length)
            {
                throw new ArgumentException("Length cannot be larger than the buffer", "readByteCount");
            }

            int         ret = SocketsApi.ReadBytes(InteralSocket, buffer, offset, readByteCount);
            SocketError err = SocketsApi.GetLastError();

            if (ret == SocketsApi.SOCKET_ERROR && err != SocketError.Success)
            {
                Connected = false;
                Debug.WriteLine("Recieve Error: " + err);
                Close();
            }
            else if (ret == 0 && readByteCount != 0)
            {
                Connected = false;
                Debug.WriteLine("Socket closed gracefully");
                Close();
            }
            Total += ret;
            Debug.WriteLine("Total read: " + Total);

            if (ret == -1)
            {
                return(0);
            }
            return(ret);
        }
예제 #2
0
        public static int ReadBytes(IntPtr socket, byte[] buffer, int offset, int byteCount)
        {
            //Debug.Assert(buffer.Length >= offset + byteCount);

            Debug.Assert(buffer.Length >= (offset + byteCount));

            Debug.WriteLine("Read: buf=" + buffer.Length + " offset=" + offset + " count=" + byteCount);
            // System.Threading.Thread.Sleep(500);
            var bufferHandle = Microsoft.Phone.InteropServices.GCHandle.Alloc(buffer, GCHandleType.Pinned);

            Debug.WriteLine("Reading " + byteCount + " from " + socket.ToInt32());
            int ret;

            m_netSocketInterface.ReadBytes(socket, new IntPtr(bufferHandle.AddrOfPinnedObject().ToInt32() + offset), byteCount, out ret);
            Debug.WriteLine(" --> Read " + ret);
            // System.Threading.Thread.Sleep(500);
            if (ret == SocketsApi.SOCKET_ERROR)
            {
                // Debug.WriteLine("There has been an error! ------------");
                Debug.WriteLine("Err: " + SocketsApi.GetLastError());
            }
            bufferHandle.Free();
            //  Debug.WriteLine("Handle Free");
            return(ret);
        }
예제 #3
0
        public int Send(byte[] buffer, int offset, int byteCount)
        {
            if (InteralSocket == IntPtr.Zero)
            {
                return(-1);
            }

            //Debug.Assert(InteralSocket != IntPtr.Zero);
            if ((byteCount + offset) > buffer.Length)
            {
                throw new ArgumentException("Length cannot be larger than the buffer", "byteCount");
            }

            int         ret = SocketsApi.SendBytes(InteralSocket, buffer, offset, byteCount, 0);
            SocketError err = SocketsApi.GetLastError();

            if (ret == SocketsApi.SOCKET_ERROR)
            {
                Connected = false;
                Debug.WriteLine("Send Error: " + err);
                Close();
            }
            if (ret == -1)
            {
                return(0);
            }
            return(ret);
        }