/// <summary>
        /// Broadcast text from server to all connected clients
        /// </summary>
        /// <param name="text"></param>
        public void BroadcastText(string text)
        {
            CCriticalSection CCBroadcast = new CCriticalSection();

            CCBroadcast.Enter();
            try
            {
                if (ConnectedClientsIndexes.Count > 0)
                {
                    byte[] b = Encoding.GetEncoding(28591).GetBytes(text);
                    foreach (uint i in ConnectedClientsIndexes)
                    {
                        if (!SharedKeyRequired || (SharedKeyRequired && ClientReadyAfterKeyExchange.Contains(i)))
                        {
                            SocketErrorCodes error = SecureServer.SendDataAsync(i, b, b.Length, (x, y, z) => { });
                            if (error != SocketErrorCodes.SOCKET_OK && error != SocketErrorCodes.SOCKET_OPERATION_PENDING)
                            {
                                Debug.Console(2, error.ToString());
                            }
                        }
                    }
                }
                CCBroadcast.Leave();
            }
            catch (Exception ex)
            {
                CCBroadcast.Leave();
                Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Broadcasting messages from server. Error: {0}", ex.Message);
            }
        }
        /// <summary>
        /// send data to server
        /// </summary>
        /// <param name="tx">data</param>
        public void DataTransmit(SimplSharpString tx)
        {
            var err = new SocketErrorCodes();

            byte[] bytes = Encoding.UTF8.GetBytes(tx.ToString());
            err = _tcpClient.SendData(bytes, bytes.Length);
            Debug("Data transmitted: " + tx.ToString(), ErrorLevel.None, err.ToString());
        }
        /// <summary>
        /// connect to server and listen for data
        /// </summary>
        public void Connect()
        {
            SocketErrorCodes err = new SocketErrorCodes();

            if (_initialized)
            {
                try
                {
                    _manualDisconnect = false;
                    err = _tcpClient.ConnectToServerAsync(ConnectToServerCallback);
                    Debug("Connection attempt: " + _tcpClient.AddressClientConnectedTo, ErrorLevel.Notice, err.ToString());
                }
                catch (Exception e)
                {
                    Debug(e.Message, ErrorLevel.Error, err.ToString());
                }
            }
            else
            {
                Debug("TCPClient not initialized, missing data", ErrorLevel.Notice, err.ToString());
            }
        }
        /// <summary>
        /// Disconnect from server
        /// </summary>
        public void Disconnect()
        {
            SocketErrorCodes err = new SocketErrorCodes();

            try
            {
                _manualDisconnect = true;
                _tcpClient.Dispose();
                err = _tcpClient.DisconnectFromServer();
                Debug("Disconnect attempt: " + _tcpClient.AddressClientConnectedTo, ErrorLevel.Notice, err.ToString());
            }
            catch (Exception e)
            {
                Debug(e.Message, ErrorLevel.Error, err.ToString());
            }
        }
Esempio n. 5
0
        private SocketErrorCodes SendDatagram(byte[] data, int len)
        {
            if (client == null)
            {
                return(SocketErrorCodes.SOCKET_NOT_CONNECTED);
            }

            CMonitor.Enter(_sendDatagramLock);
            SocketErrorCodes sret = client.SendData(data, len, remoteEndpoint);

            CMonitor.Exit(_sendDatagramLock);
            if (sret != SocketErrorCodes.SOCKET_OK)
            {
                ErrorLog.Error("SendDatagram: {0}", sret.ToString());
            }
            return(sret);
        }
Esempio n. 6
0
        public virtual SocketErrorCodes Send(byte[] bytes)
        {
            if (this.Socket.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED)
            {
                SocketErrorCodes err = Socket.SendData(bytes, bytes.Length);

                if (err != SocketErrorCodes.SOCKET_OK)
                {
                    ErrorLog.Error("{0} Send to {1} Error: {2}", this.GetType().ToString(), Socket.AddressClientConnectedTo, err.ToString());
                }
                return(err);
            }
            return(SocketErrorCodes.SOCKET_NOT_CONNECTED);
        }
Esempio n. 7
0
        public void SendData(SimplSharpString dataToSend)
        {
            byte[]           pBufferToSend = System.Text.Encoding.ASCII.GetBytes(dataToSend.ToString());
            SocketErrorCodes returnCode    = tcpClient.SendData(pBufferToSend, pBufferToSend.Length);

            if (debug > 0)
            {
                CrestronConsole.PrintLine("\n TCPClientClass SendData returnCode: " + returnCode.ToString());
            }
        }
Esempio n. 8
0
        public void ConnectToServer()
        {
            SocketErrorCodes returnCode = tcpClient.ConnectToServer();

            if (debug > 0)
            {
                CrestronConsole.PrintLine("\n TCPClientClass ConnectToServer returnCode: " + returnCode.ToString());
            }

            if (returnCode == 0)
            {
                tcpClient.ReceiveDataAsync(OnDataReceiveEventCallback);
            }
        }
 private static void SendDataAsync(string dataToSend)
 {
     try
     {
         byte[] SendData = System.Text.Encoding.ASCII.GetBytes(dataToSend);
         for (uint index = 1; index <= tcpServer.NumberOfClientsConnected; index++)
         {
             if (debug > 0)
             {
                 CrestronConsole.Print("\n Catch Connect SendDataAsync attempt for index: " + index);
             }
             if (tcpServer.ClientConnected(index))
             {
                 SocketErrorCodes resultCodes = tcpServer.SendDataAsync(index, SendData, SendData.Length, OnTCPServerSendCallback);
                 if (debug > 0)
                 {
                     CrestronConsole.Print("\n Catch Connect SendDataAsync resultCodes: " + resultCodes.ToString());
                 }
             }
             else
             {
                 CrestronConsole.Print("\n Catch Connect SendDataAsync client not connected at index: " + index);
             }
         }
     }
     catch (Exception e)
     {
         ErrorLog.Error("error = " + e.Message);
     }
 }
        private static void OnTCPReceiveCallback(TCPServer myTCPServer, uint clientIndex, int numberOfBytesReceived)
        {
            if (debug > 0)
            {
                CrestronConsole.Print("\n Catch Connect OnTCPReceiveCallback clientIndex: " + clientIndex);
            }

            if (myTCPServer != null && clientIndex > 0 && numberOfBytesReceived > 0)
            {
                try
                {
                    byte[] data          = myTCPServer.GetIncomingDataBufferForSpecificClient(clientIndex);
                    string receiveBuffer = Encoding.UTF8.GetString(data, 0, numberOfBytesReceived);
                    parseResponse(receiveBuffer);
                }
                catch (Exception e)
                {
                    ErrorLog.Error("\n Catch Connect OnTCPReceiveCallback Exception: " + e.Message);
                }
            }
            //Start listening for new messages and process message queue
            if (myTCPServer != null && clientIndex > 0)
            {
                try
                {
                    SocketErrorCodes resultCodes = myTCPServer.ReceiveDataAsync(clientIndex, OnTCPReceiveCallback);
                    ProcessQueue();
                    if (debug > 0)
                    {
                        CrestronConsole.Print("\n Catch Connect OnTCPReceiveCallback ReceiveDataAsync resultCodes: " + resultCodes.ToString());
                    }
                }
                catch (Exception e)
                {
                    ErrorLog.Error("\n Catch Connect OnTCPReceiveCallback ReceiveDataAsync Exception: " + e.Message);
                }
            }
        }
        private static void OnTCPServerClientConnectCallback(TCPServer myTCPServer, uint clientIndex)
        {
            if (debug > 0)
            {
                CrestronConsole.Print("\n Catch Connect OnTCPServerClientConnectCallback index: " + clientIndex.ToString());
            }
            //Listen for data from the connected client
            if (myTCPServer.ClientConnected(clientIndex))
            {
                SocketErrorCodes resultCodes = myTCPServer.ReceiveDataAsync(clientIndex, OnTCPReceiveCallback);
                if (debug > 0)
                {
                    CrestronConsole.Print("\n Catch Connect OnTCPServerClientConnectCallback ReceiveDataAsync resultCodes: " + resultCodes.ToString());
                }
            }
            else
            {
                if (debug > 0)
                {
                    CrestronConsole.Print("\n Catch Connect OnTCPServerClientConnectCallback client not connected at: " + clientIndex);
                }
            }

            //Listen for other connections
            if (myTCPServer.MaxNumberOfClientSupported > myTCPServer.NumberOfClientsConnected)
            {
                try
                {
                    SocketErrorCodes connectionResultCodes = myTCPServer.WaitForConnectionAsync("0.0.0.0", OnTCPServerClientConnectCallback);
                    if (debug > 0)
                    {
                        CrestronConsole.Print("\n Catch Connect TCPServer  OnTCPServerClientConnectCallback WaitForConnectionAsync resultCodes: " + connectionResultCodes.ToString());
                    }
                }
                catch (Exception e)
                {
                    ErrorLog.Error("\n Catch Connect OnTCPServerClientConnectCallback WaitForConnectionAsync Exception: " + e.Message);
                }
            }
        }
 private static void StartListening()
 {
     try
     {
         SocketErrorCodes resultCodes = tcpServer.WaitForConnectionAsync("0.0.0.0", OnTCPServerClientConnectCallback);
         ErrorLog.Error("\n  Catch Connect InitializeService WaitForConnectionAsync resultCodes: " + resultCodes);
         if (debug > 0)
         {
             CrestronConsole.Print("\n Catch Connect InitializeService WaitForConnectionAsync resultCodes: " + resultCodes.ToString());
         }
     }
     catch (Exception e)
     {
         ErrorLog.Error("\n Catch Connect InitializeService Exception: " + e.Message);
     }
 }