CSADDR_INFO.cs :  » Business-Application » 32feet.NET » InTheHand » Net » Sockets » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » 32feet.NET 
32feet.NET » InTheHand » Net » Sockets » CSADDR_INFO.cs
// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Net.Sockets.CSADDR_INFO
// 
// Copyright (c) 2003-2007 In The Hand Ltd, All rights reserved.
// This source code is licensed under the In The Hand Community License - see License.txt

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using InTheHand.Net.Bluetooth;
#if V1
using InTheHand.Runtime.InteropServices;
#endif

namespace InTheHand.Net.Sockets{
    [StructLayout(LayoutKind.Sequential, Size=24)]
    internal struct CSADDR_INFO
    {
        internal IntPtr localAddr;
        internal int localSize;
        internal IntPtr remoteAddr;
        internal int remoteSize;
        internal System.Net.Sockets.SocketType iSocketType;
        internal System.Net.Sockets.ProtocolType iProtocol;

        public CSADDR_INFO(BluetoothAddress local, BluetoothAddress remote, System.Net.Sockets.SocketType type, System.Net.Sockets.ProtocolType protocol)
        {
            //ensure zeros
            localAddr = IntPtr.Zero;
            localSize = 0;
            remoteAddr = IntPtr.Zero;
            remoteSize = 0;

            iSocketType = type;
            iProtocol = protocol;
            

            if (local != null)
            {
#if V1
                //have to use AllocHGlobal substitute
                localAddr = Marshal32.AllocHGlobal(40);
                Marshal.Copy(local.ToByteArray(), 0, new IntPtr(localAddr.ToInt32() + 8), 6);
#else
                localAddr = Marshal.AllocHGlobal(40);
                Marshal.WriteInt64(localAddr, 8, local.ToInt64());
#endif
                Marshal.WriteInt16(localAddr, 0, 32);                
                localSize = 40;
            }
            if (remote != null)
            {
#if V1
                remoteAddr = Marshal32.AllocHGlobal(40);
                Marshal.Copy(remote.ToByteArray(), 0, new IntPtr(remoteAddr.ToInt32() + 8), 6);
#else
                remoteAddr = Marshal.AllocHGlobal(40);
                Marshal.WriteInt64(remoteAddr, 8, remote.ToInt64());
#endif
                remoteSize = 40;
                Marshal.WriteInt16(remoteAddr, 0, 32);   
            }
        }

        public void Dispose()
        {
            if (localAddr!=IntPtr.Zero)
            {
#if V1
                Marshal32.FreeHGlobal(localAddr);
#else
                Marshal.FreeHGlobal(localAddr);
#endif
                localAddr = IntPtr.Zero;
            }
            if (remoteAddr!=IntPtr.Zero)
            {
#if V1
                Marshal32.FreeHGlobal(remoteAddr);
#else
                Marshal.FreeHGlobal(remoteAddr);
#endif
                remoteAddr = IntPtr.Zero;
            }
        }
    }


#if ! V1
    static
#endif
    class CsaddrInfoOffsets
    {
        //struct CSADDR_INFO {
        //    SOCKET_ADDRESS LocalAddr;
        //    SOCKET_ADDRESS RemoteAddr;
        //    INT iSocketType;
        //    INT iProtocol;
        //}
        //struct SOCKET_ADDRESS {
        //    LPSOCKADDR lpSockaddr;
        //    INT iSockaddrLength;
        //}

        // Presumably the pointer field is 8-aligned.
        public static readonly int OffsetRemoteAddr_lpSockaddr_8 = 2 * IntPtr.Size;
        public static readonly int OffsetRemoteAddr_iSockaddrLength_12 = 3 * IntPtr.Size;

        static bool s_doneAssert;

        [System.Diagnostics.Conditional("DEBUG")]
        public static void AssertCheckLayout()
        {
            if (s_doneAssert)
                return;
            s_doneAssert = true;
#if !NETCF // No OffsetOf on NETCF
            System.Diagnostics.Debug.Assert(CsaddrInfoOffsets.OffsetRemoteAddr_lpSockaddr_8
                == Marshal.OffsetOf(typeof(CSADDR_INFO), "remoteAddr").ToInt64(), "offset remoteAddr");
            System.Diagnostics.Debug.Assert(CsaddrInfoOffsets.OffsetRemoteAddr_iSockaddrLength_12
                == Marshal.OffsetOf(typeof(CSADDR_INFO), "remoteSize").ToInt64(), "offset remoteSize");
#endif
        }

    }//class

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.