Example #1
0
        public Socket AcceptSocket()
        {
            if (Server == null)
            {
                throw new InvalidOperationException("Not listening");
            }

            IntPtr client;
            string addr;
            int    remote_port;
            int    ret = SocketsApi.Accept(Server.InteralSocket, out client, out addr, out remote_port);

            // TODO WSAError
            if (ret != 0)
            {
                throw new InteropException("Unable to accept client, Error: " + ret);
            }

            Socket retSocket = new Socket(client);

            retSocket.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(addr), remote_port);
            //retSocket.IPAddress = addr;
            // TODO Remote Port, IPEndPoint
            return(retSocket);
        }
        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);
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
0
 public void Close()
 {
     if (InteralSocket != IntPtr.Zero)
     {
         int ret = SocketsApi.Close(InteralSocket);
         InteralSocket = IntPtr.Zero;
         Debug.WriteLine("Closed: " + ret);
         Connected = false;
     }
 }
Example #6
0
        public void Start()
        {
            IntPtr socket;
            int    ret = SocketsApi.Listen(Port, out socket);

            // TODO WSAError
            if (ret != 0)
            {
                throw new InteropException("Unable to listen, Error: " + ret);
            }
            Server         = new Socket(socket);
            Server.IsBound = true;
        }
Example #7
0
        public void Connect(string host, int port)
        {
            IntPtr socket;

            int ret = SocketsApi.Connect(host, port, out socket);

            if (ret != 0)
            {
                throw new InteropException("Could not connect socket");
            }

            _socket = new Socket(socket);
        }