Esempio n. 1
0
        private void CreateContext(Export export)
        {
            ExportContext context = null;

            lock (m_contexts)
            {
                if (!m_contexts.TryGetValue(export.Name, out context))
                {
                    // Context for the export does not exist, so we create one.
                    ExportSetting serverPortSetting = export.FindSetting("ServerPort");
                    ExportSetting legacyModeSetting = export.FindSetting("LegacyMode");
                    if ((serverPortSetting != null) && !string.IsNullOrEmpty(serverPortSetting.Value))
                    {
                        // Create the server socket for export context.
                        context.Socket = ServerBase.Create(string.Format("Protocol=TCP;Port={0}", serverPortSetting.Value));
                        context.Socket.Start();             // Start the server; need to do only one time.
                        context.Socket.ClientConnected    += CommunicationServer_ClientConnected;
                        context.Socket.ClientDisconnected += CommunicationServer_ClientDisconnected;

                        // Determine the format of transmission.
                        if (legacyModeSetting != null && Convert.ToBoolean(legacyModeSetting.Value))
                        {
                            // Data is to be transmitted using PacketType1.
                            context.DataPerPacket   = 50;
                            context.TransmitHandler = TransmitPacketType1;
                        }
                        else
                        {
                            // Data is to be transmitted using PacketType101.
                            context.DataPerPacket   = 1000000;
                            context.TransmitHandler = TransmitPacketType101;
                        }

                        // Save export context
                        m_contexts.Add(export.Name, context);

                        // Provide a status update.
                        OnStatusUpdate(string.Format("TCP server created for export {0}", export.Name));
                    }
                }
            }
        }