Example #1
0
        protected void SendCommand(Socket socket, string command, CommandSentEventHandler callback, bool closeConnection = false)
        {
            try
            {
                Wait();
                var byteData = DongleEncoding.Default.GetBytes(command);
                socket.BeginSend(byteData, 0, byteData.Length, 0, asyncResult =>
                {
                    int bytesSent;
                    try
                    {
                        bytesSent = socket.EndSend(asyncResult);
                    }
                    catch (Exception exception)
                    {
                        if (OnErrorOcurred != null)
                        {
                            OnErrorOcurred(exception);
                        }
                        if (OnSocketDisconnected != null)
                        {
                            OnSocketDisconnected(socket);
                        }
                        return;
                    }

                    IsSending = false;

                    if (callback != null)
                    {
                        callback(command, bytesSent);
                    }
                    if (closeConnection)
                    {
                        try
                        {
                            socket.Disconnect(false);
                            if (OnSocketDisconnected != null)
                            {
                                OnSocketDisconnected(socket);
                            }
                        }
                        catch (Exception exception)
                        {
                            if (OnErrorOcurred != null)
                            {
                                OnErrorOcurred(exception);
                            }
                        }                        
                    }
                }, null);
            }
            catch (Exception)
            {                
                if (OnSocketDisconnected != null)
                {
                    OnSocketDisconnected(socket);
                }
            }            
        }
Example #2
0
 public void Connect()
 {
     CommandSent += new CommandSentEventHandler(client_CommandSent);
     CommandSendingFailed += new CommandSendingFailedEventHandler(client_CommandSendingFailed);
     CommandReceivingFailed += new CommandReceivingFailedEventHandler(client_CommandReceivingFailed);
     ServerDisconnected += new ServerDisconnectedEventHandler(client_ServerDisconnected);
     DisconnectedFromServer += new DisconnectedEventHandler(client_DisconnectedFromServer);
     ConnectingSuccessed += new ConnectingSuccessedEventHandler(client_ConnectingSuccessed);
     ConnectingFailed += new ConnectingFailedEventHandler(client_ConnectingFailed);
     NetworkDead += new NetworkDeadEventHandler(client_NetworkDead);
     NetworkAlived += new NetworkAlivedEventHandler(client_NetworkAlived);
     ConnectToServer();
 }