Exemplo n.º 1
0
        public void SendMessage(NetMsg msg)
        {
            bool bRes = _socket.Send(msg);

#if TRACE_MSG_SEND_REV_RESULT
            if (!bRes)
            {
#if IGNORE_TRACE_SOME_MSG
                if (_listIgnoreTraceMsg.IndexOf(msg.Type()) == -1)
                {
                    KXLogger.Log(msg.Type() + " sending failed!");
                }
#else
                KXLogger.Log(msg.Type() + " sending failed!");
#endif
            }
            else
            {
#if IGNORE_TRACE_SOME_MSG
                if (_listIgnoreTraceMsg.IndexOf(msg.Type()) == -1)
                {
                    KXLogger.Log(msg.Type() + " sent successfully!");
                }
#else
                KXLogger.Log(msg.Type() + " sent successfully!");
#endif
                DispatchEvent((int)GameEventType.SOCKET_MESSAGE_SEND, new GameEvent(this, (int)GameEventType.SOCKET_MESSAGE_SEND));
            }
#endif
        }
Exemplo n.º 2
0
 public bool SyncConnect(string ip, int port)
 {
     if (_socket != null)
     {
         Disconnect();
     }
     _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     if (_socket == null)
     {
         connectedCallback(false);
         return(false);
     }
     try
     {
         _socket.Connect(ip, port);
     }
     catch (Exception exception)
     {
         KXLogger.LogError(exception.Message + " Exception Error: " + exception.StackTrace);
         connectedCallback(false);
         return(false);
     }
     _recvPosition = 0;
     if (!AsyncRecvMessageFromSocket())
     {
         Disconnect();
         connectedCallback(false);
         return(false);
     }
     connectedCallback(true);
     return(true);
 }
Exemplo n.º 3
0
        private bool ParseMessage(ref byte[] buf, ref int len, ref ByteArray byteArray)
        {
            if (len < 4)
            {
                return(false);
            }

            byte[] byteTmp = new byte[2];
            Array.Copy(buf, 0, byteTmp, 0, 2);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(byteTmp);
            }
            int size = BitConverter.ToUInt16(byteTmp, 0) + 2;

            if (size < 3 || size > 65535)
            {
                this.Disconnect();
                KXLogger.LogError("Data package too long or too short,:size = " + size.ToString());
                return(false);
            }
            if (len < size)
            {
                return(false);
            }
            if (!byteArray.CreateFromSocketBuff(buf, size))
            {
                RemoveRecvBuff(ref buf, ref len, size);
                return(false);
            }
            RemoveRecvBuff(ref buf, ref len, size);
            return(true);
        }
Exemplo n.º 4
0
        public bool Send(NetMsg msg)
        {
            if (!this.IsConnected() || msg == null)
            {
                return(false);
            }
            ByteArray body = new ByteArray(2097120);

            msg.seq = GetMessageId();
            msg.Write(body);
            ushort len = (ushort)body.length;

            Byte[] lenBytes = BitConverter.GetBytes((ushort)(len - 2));
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(lenBytes);
            }
            Array.Copy(lenBytes, body.buff, 2);

            try
            {
                SocketError err;
                _socket.BeginSend(body.buff, 0, body.length, SocketFlags.None, out err, new AsyncCallback(AsyncSendCallback), this);
            }
            catch (Exception ex)
            {
                KXLogger.LogError("Send socket data error : " + ex.StackTrace);
                return(false);
            }
            return(true);
        }
Exemplo n.º 5
0
 private void AsyncSendCallback(IAsyncResult ar)
 {
     try
     {
         SocketError err;
         _socket.EndSend(ar, out err);
     }
     catch (Exception ex)
     {
         KXLogger.LogError("Sending socket data callback error : " + ex.StackTrace);
     }
 }
Exemplo n.º 6
0
 public void Disconnect()
 {
     if (IsConnected())
     {
         try
         {
             id = 0;
             _socket.Disconnect(true);
         }
         catch (Exception ex)
         {
             KXLogger.LogError("Disconnect error: " + ex.StackTrace);
         }
         _socket       = null;
         _recvPosition = 0;
     }
 }
Exemplo n.º 7
0
        private void AsyncRecvCallback(IAsyncResult ar)
        {
            int num = 0;

            try
            {
                SocketError se;
                num = _socket.EndReceive(ar, out se);
                if (num <= 0)
                {
                    KXLogger.LogError("Receive data length : " + num.ToString());
                }
            }
            catch (Exception ex)
            {
                if (IsConnected())
                {
                    KXLogger.LogError(ex.Message + "SocketWrapper.AsyncRecvCallback Error : " + ex.StackTrace);
                }
                return;
            }

            if (num == 0)
            {
                KXLogger.LogError("Receive socket data length = 0 : Disconnect()");
                Disconnect();
            }
            else
            {
                _recvPosition += num;
                while (true)
                {
                    if (!this.IsConnected())
                    {
                        break;
                    }
                    ByteArray byteArray = new ByteArray(1048560);
                    if (!this.ParseMessage(ref _recvBuff, ref _recvPosition, ref byteArray))
                    {
                        break;
                    }
                    _recvQueue.Enqueue(byteArray);
                }
                AsyncRecvMessageFromSocket();
            }
        }
Exemplo n.º 8
0
 private void AsyncConnectCallback(IAsyncResult ar)
 {
     try
     {
         _socket.EndConnect(ar);
         _recvPosition = 0;
         if (!AsyncRecvMessageFromSocket())
         {
             Disconnect();
             connectedCallback(false);
         }
         connectedCallback(true);
     }
     catch (Exception ex)
     {
         KXLogger.LogError(ex.Message + " connect server callback error: " + ex.StackTrace);
         connectedCallback(false);
     }
 }
Exemplo n.º 9
0
 public void AsyncConnect(string ip, int port)
 {
     if (_socket != null)
     {
         Disconnect();
     }
     _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     if (_socket == null)
     {
         connectedCallback(false);
         return;
     }
     try
     {
         _socket.BeginConnect(ip, port, new AsyncCallback(AsyncConnectCallback), this);
     }
     catch (Exception exception)
     {
         KXLogger.LogError(exception.Message + " Exception error : " + exception.StackTrace);
         connectedCallback(false);
         return;
     }
 }
Exemplo n.º 10
0
 private bool AsyncRecvMessageFromSocket()
 {
     if (_socket != null)
     {
         try
         {
             SocketError socketError;
             _socket.BeginReceive(
                 _recvBuff,
                 _recvPosition,
                 _recvBuff.Length - _recvPosition,
                 SocketFlags.None,
                 out socketError,
                 new AsyncCallback(AsyncRecvCallback), this);
         }
         catch (Exception exception)
         {
             KXLogger.LogError("Receive socket error: " + exception.StackTrace);
             return(false);
         }
         return(true);
     }
     return(false);
 }
Exemplo n.º 11
0
 private static extern bool InitKXInput(KXLogger a_logger);