Beispiel #1
0
 // Disconnect
 public void Disconnect()
 {
     CSLogger.LogNotice("Disconnect " + m_server_ip + ":" + m_server_port);
     if (null != m_server_socket)
     {
         m_server_socket.DisConnect(true);
     }
 }
Beispiel #2
0
        // Parse a protocol.
        private void ParseProcotol()
        {
            // Check recv len
            int socket_recv_len = m_socket.Receive(m_recv_buff, SocketFlags.None);

            if (socket_recv_len <= 0)
            {
                return;
            }
            // Check pos
            m_recv_stream.Position = m_recv_stream.Length;
            m_recv_stream.Write(m_recv_buff, 0, socket_recv_len);
            m_recv_stream.Position = m_recv_stream_pos;
            while (m_recv_stream.Length - m_recv_stream.Position >= Message.MSG_BASIC_LEN)
            {
                m_recv_stream_pos = m_recv_stream.Position;
                m_recv_stream.Read(m_recv_buff, 0, Message.MSG_BASIC_LEN);
                MBinaryReader mbr = new MBinaryReader(m_recv_buff);

                int msg_len = mbr.ReadUInt16();
                if (m_recv_stream.Length - m_recv_stream.Position >= msg_len - Message.MSG_BASIC_LEN)
                {
                    m_recv_stream_pos = 0;
                    m_recv_stream.Read(m_recv_buff, 0, msg_len - Message.MSG_BASIC_LEN);
                    mbr = new MBinaryReader(m_recv_buff);
                    ushort  msg_id = mbr.ReadUInt16();
                    Message msg    = MessageFactory.CreateMessageById(msg_id);
                    if (null == msg)
                    {
                        CSLogger.LogNotice("Invalid msg id: " + msg_id);
                    }
                    if (!msg.FromBytes(mbr))
                    {
                        CSLogger.LogNotice("ParseProcotol: Fail to parse msg: " + msg.ToString());
                    }
                    if (m_recv_queue != null)
                    {
                        lock (m_recv_queue)
                        {
                            m_recv_queue.Enqueue(msg);
                        }
                    }
                }
                else
                {
                    m_recv_stream.Position = m_recv_stream_pos;
                    break;
                }
                mbr.Close();
                mbr = null;
            }
            if (m_recv_stream.Position == m_recv_stream.Length)
            {
                m_recv_stream.SetLength(0);
            }
        }
Beispiel #3
0
 // Send msg
 public void SendMsg(Message msg)
 {
     if (!IsConnected())
     {
         CSLogger.LogError("GateServer is disconnected when send " + msg.ToString());
         return;
     }
     CSLogger.LogNotice("Send Msg: " + msg.ToString());
     lock (m_send_queue)
     {
         m_send_queue.Enqueue(msg);
     }
     m_event_wait_send.Set();
 }
Beispiel #4
0
        // run this player
        public void RunGatePlayer(string strSrvIP, int nSrvPort)
        {
            if (!Init(strSrvIP, nSrvPort))
            {
                return;
            }
            AsyncConnect();
            // Max try count: 1000
            DateTime loop_begin_time = DateTime.Now;

            for (int i = 0; i < 1000; ++i)
            {
                if (IsServerConnected())
                {
                    OnAsyncConnectSuccess();
                    break;
                }
                System.Threading.Thread.Sleep(2);
                TimeSpan time_span = DateTime.Now - loop_begin_time;
                if (time_span.TotalSeconds >= 3.0f)
                {
                    break;
                }
            }
            if (!IsServerConnected())
            {
                CSLogger.LogNotice("Connected failed");
                OnAsyncConnectFailed();
                return;
            }
            // Main loop
            while (true)
            {
                Update();
            }
        }
Beispiel #5
0
 // Send msg to server
 public void SendMsgToServer(Message msg)
 {
     if (m_server_socket == null)
     {
         return;
     }
     if (!m_server_socket.IsConnected())
     {
         CSLogger.LogNotice("Server Is DisConnected");
         return;
     }
     if (null == msg)
     {
         CSLogger.LogError("SendMsgToServer: null == msg");
         return;
     }
     if (null == m_server_socket)
     {
         CSLogger.LogError("SendMsgToServer: null == m_gs_socket");
         return;
     }
     CSLogger.LogDebug("SendMsgToServer: " + msg.ToString());
     m_server_socket.SendMsg(msg);
 }
Beispiel #6
0
 // Default handle of msg: GC_HeartBeat_Msg
 protected override bool OnGC_HeartBeat_Msg(GC_HeartBeat_Msg msg)
 {
     CSLogger.LogNotice("Receive HB: " + msg.heartbeat_index);
     return(false);
 }
Beispiel #7
0
 // Called after connect success
 public void OnAsyncConnectSuccess()
 {
     CSLogger.LogNotice("Success to connect " + m_server_ip + ":" + m_server_port);
     m_heartbeat_index     = 0;
     m_send_heartbeat_time = DateTime.UtcNow;
 }