Class: GG::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/GRuby/GG.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Session

Returns a new instance of Session


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/GRuby/GG.rb', line 14

def initialize( params )
  @uin = params[:uin]
  @password = params[:password]
	  @message_seq = 0
  @connected = true
  @logged = false
  @socket = nil
  @wait4login = true 
  @wait4login = params[:async] unless params[:async].nil?
  
	  # callbacks
	  @revice_msg_callback = nil
	  @login_failed_callback = lambda do exit end
	  @disconnecting_callback = lambda do exit end
	  
	  socket_thread = Thread.new do
    begin
      params = GG::connection_params
      @socket = TCPSocket.new( params['host'] , params['port'] )
      while @connected do
        begin
          r_packet = PACKET.new
          puts ":: Wait for data ..." if $DEBUG
          r_packet.header = @socket.read r_packet.header_length
          r_packet.data   = @socket.read r_packet.length
        rescue => e
          @logged = false
          @connected = false
        end
        case r_packet.type
        when WELCOME
          r_packet.type   = PACKET_WELCOME
          s_packet        = PACKET.new
          s_packet.type   = PACKET_LOGIN60
          s_packet.uin    = @uin.to_i
          s_packet.hash   = GG::( @password, r_packet.seed )

          @socket.write s_packet.packed
          puts "++ #{s_packet} send" if $DEBUG
          s_packet = s_packet.destroy
        when LOGIN_OK
          @logged = true
          @wait4login = false
        when RECV_MSG
          r_packet.type   = PACKET_RECV_MSG
          @recive_msg_callback.call r_packet if @recive_msg_callback
        when SEND_MSG_ACK
          r_packet.type =   PACKET_SEND_MSG_ACK
			when LOGIN_FAILED
 puts "!! Login failed" if $DEBUG
 @login_failed_callback.call if @login_failed_callback
			when DISCONNECTING
 puts "!! Disconecting" if $DEBUG
 @disconnecting_callback.call if @disconnecting_callback
			else
          puts "?? Unknown packet" if $DEBUG 
        end
        r_packet = r_packet.destroy
      end
      puts ":: Disconected" if $DEBUG
    rescue => e
      puts "!! Exception: #{e}" if $DEBUG
      puts "Something went wrong :)" unless $DEBUG
      exit 0
    end
  end
  while @wait4login do
    sleep 1
  end
end

Instance Attribute Details

#connectedObject (readonly)

Returns the value of attribute connected


12
13
14
# File 'lib/GRuby/GG.rb', line 12

def connected
  @connected
end

Instance Method Details

#on_diconnect(&block) ⇒ Object

Disconnecting callback


103
104
105
# File 'lib/GRuby/GG.rb', line 103

def on_diconnect( &block )
  @disconnecting_callback = block;
end

#on_login_failed(&block) ⇒ Object

Login failed callback


96
97
98
# File 'lib/GRuby/GG.rb', line 96

def ( &block )
  @login_failed = block;
end

#on_recive_message(&block) ⇒ Object

Recive message callback


89
90
91
# File 'lib/GRuby/GG.rb', line 89

def on_recive_message( &block )
  @recive_msg_callback = block;
end

#send_message(params) ⇒ Object

Send message

:uin => GG reciver number 
:message => message text

112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/GRuby/GG.rb', line 112

def send_message( params )
  if @logged
    s_packet           = PACKET.new
    s_packet.type      = PACKET_SEND_MSG
    s_packet.recipient = params[:uin]
    s_packet.message   = params[:message]

    @socket.write s_packet.packed
    puts "++ #{s_packet} send" if $DEBUG
    s_packet = s_packet.destroy
  end
end

#set_status(params) ⇒ Object

Set new status.

:status => new status, see constans.
:description => new description

130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/GRuby/GG.rb', line 130

def set_status( params )
  if @logged
    s_packet = PACKET.new
    s_packet.type = PACKET_NEW_STATUS
    s_packet.status = params[:status]
    s_packet.description = params[:description]

    @socket.write s_packet.packed
    puts "++ #{s_packet} send" if $DEBUG
    s_packet = s_packet.destroy
  end
end