예제 #1
0
        public static void Send(Int16 key, IMessage msg)
        {
            var typeBytes = LittleEndianByte.GetBytes(key);
            var dataBytes = msg.ToByteArray();
            var sendBytes = typeBytes.Concat(dataBytes).ToArray();

            m_ClientSocket.Send(sendBytes);
        }
예제 #2
0
        public void Send(Socket cfd, byte[] data)
        {
            if (!m_OnlineClients.ContainsKey(cfd))
            {
                return;
            }
            Int16 len = (Int16)data.Length;

            byte[] lenBytes  = LittleEndianByte.GetBytes(len);/* BitConverter.GetBytes(len);*/
            byte[] sendBytes = lenBytes.Concat(data).ToArray();
            cfd.BeginSend(sendBytes, 0, sendBytes.Length, 0, SendCallback, cfd);
        }
예제 #3
0
 public static void Connect(String ip, Int32 port)
 {
     m_ClientSocket = new ClientSocket();
     m_ClientSocket.onReceive.AddListener(delegate(byte[] data)
     {
         Int16 type = LittleEndianByte.GetInt16(data, 0);
         if (m_ProtocMap.ContainsKey(type))
         {
             m_ProtocMap[type].Invoke(data, 2, data.Length - 2);
         }
     });
     m_ClientSocket.Connect("127.0.0.1", 8888);
 }
예제 #4
0
        public static void Send(Socket cfd, Int16 key, IMessage msg)
        {
            if (m_ServerSocket == null)
            {
                return;
            }

            var typeBytes = LittleEndianByte.GetBytes(key);
            var dataBytes = msg.ToByteArray();
            var sendBytes = typeBytes.Concat(dataBytes).ToArray();

            m_ServerSocket.Send(cfd, sendBytes);
        }
예제 #5
0
        public void Send(Socket cfd, byte[] data)
        {
            if (!cfd.Connected)
            {
                return;
            }


            Int16 len = (Int16)data.Length;

            byte[] lenBytes  = LittleEndianByte.GetBytes(len);
            byte[] sendBytes = lenBytes.Concat(data).ToArray();
            cfd.BeginSend(sendBytes, 0, sendBytes.Length, 0, SendCallback, cfd);
        }
예제 #6
0
 public static void Bind(String ip, Int32 port)
 {
     if (m_ServerSocket != null)
     {
         return;
     }
     m_ServerSocket = new ServerSocket();
     m_ServerSocket.Bind(ip, port);
     m_ServerSocket.onReceive.AddListener(delegate(Socket cfd, byte[] data)
     {
         Int16 id = LittleEndianByte.GetInt16(data, 0);
         if (m_ProtocMap.ContainsKey(id))
         {
             m_ProtocMap[id].Invoke(cfd, data, 2, data.Length - 2);
         }
     });
 }
예제 #7
0
        public void Update(Int32 count)
        {
            if (count <= 0)
            {
                return;
            }

            lock (recvBuffer)
            {
                recvBuffer.Add(count);
                while (recvBuffer.size > 0)
                {
                    Int32 totalLen = recvBuffer.size;
                    if (m_BuildBuffer != null)
                    {
                        Int32 remainLen  = m_BuildBuffer.GetFreeLength();
                        Int32 receiveLen = (remainLen <= totalLen ? remainLen : totalLen);
                        Int32 size       = m_BuildBuffer.Add(recvBuffer, 0, receiveLen);
                        recvBuffer.Remove(size);

                        if (m_BuildBuffer.GetFreeLength() <= 0)
                        {
                            // 已完全接收一条数据
                            lock (m_DataQueue)
                            {
                                m_DataQueue.Enqueue(m_BuildBuffer);
                            }
                            m_BuildBuffer = null;
                        }
                    }
                    else if (recvBuffer.size >= 2)
                    {
                        Int16 len = LittleEndianByte.GetInt16(recvBuffer);
                        recvBuffer.Remove(2);
                        m_BuildBuffer = new ByteArray(len);
                    }
                    else
                    {
                        // 数据接收完毕或不足凑一个 Int16
                        break;
                    }
                }
            }
        }
예제 #8
0
        // 若放入数据前队列是空时,会返回打包后的数据
        public ByteArray Add(byte[] data)
        {
            Int16 len = (Int16)data.Length;

            byte[] lenBytes  = LittleEndianByte.GetBytes(len);
            byte[] sendBytes = lenBytes.Concat(data).ToArray();

            ByteArray res = null;

            lock (m_DataQueue)
            {
                m_DataQueue.Enqueue(new ByteArray(sendBytes));
                if (m_DataQueue.Count == 1)
                {
                    res = m_DataQueue.First();
                }
            }
            return(res);
        }