Esempio n. 1
0
 /// <summary>
 /// 接收TCP的线程
 /// </summary>
 /// <param name="sender">调用者</param>
 /// <param name="e">参数</param>
 private unsafe void acceptHandleTCP(object sender, SocketAsyncEventArgs e)
 {
     try {
         Socket socket = e.AcceptSocket;
         e.AcceptSocket = null;
         SOCKDATA data = new SOCKDATA();
         data.m_socket = socket;
         int socketID = (int)socket.Handle;
         data.m_hSocket    = socketID;
         m_datas[socketID] = data;
         String remoteIP   = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
         int    remotePort = ((IPEndPoint)socket.RemoteEndPoint).Port;
         String szAccept   = String.Format("accept:{0}:{1}", remoteIP, remotePort);
         FCServerSockets.writeServerLog((int)socketID, m_hSocket, 1, szAccept);
         SocketAsyncEventArgs args = new SocketAsyncEventArgs();
         args.Completed += new EventHandler <SocketAsyncEventArgs>(onIoCompleted);
         args.SetBuffer(new byte[1024], 0, 1024);
         args.AcceptSocket = socket;
         if (!socket.ReceiveAsync(args))
         {
             processReceive(args);
         }
         beginAccept(e);
     } catch (Exception ex) {
     }
 }
Esempio n. 2
0
 private void processReceive(SocketAsyncEventArgs e)
 {
     try {
         if (e.BytesTransferred > 0)
         {
             if (e.SocketError == SocketError.Success)
             {
                 byte[]   buffer   = e.Buffer;
                 int      socketID = (int)e.AcceptSocket.Handle;
                 SOCKDATA data     = m_datas[socketID];
                 if (data != null)
                 {
                     data.m_buffer = e.Buffer;
                     data.m_len    = e.BytesTransferred;
                     int state = recv(data);
                     if (state == -1)
                     {
                         data.m_socket.Close();
                         FCServerSockets.writeServerLog((int)socketID, m_hSocket, 2, "socket exit");
                         m_datas[socketID] = null;
                         return;
                     }
                 }
             }
         }
         int      socketID2 = (int)e.AcceptSocket.Handle;
         SOCKDATA data2     = m_datas[socketID2];
         data2.m_socket.Close();
         m_datas[socketID2] = null;
         FCServerSockets.writeServerLog(socketID2, m_hSocket, 2, "socket exit");
     } catch (Exception ex) {
     }
 }
Esempio n. 3
0
        /// <summary>
        /// 保持活跃
        /// </summary>
        /// <param name="socketID">套接字ID</param>
        /// <returns>状态</returns>
        public virtual int keepAlive(int socketID)
        {
            FCBinary bw = new FCBinary();

            bw.writeInt((int)4);
            byte[] bytes  = bw.getBytes();
            int    length = bytes.Length;
            int    ret    = FCServerSockets.send(m_socketID, socketID, bytes, length);

            bw.close();
            return(ret);
        }
Esempio n. 4
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message">消息</param>
        public virtual int send(FCMessage message)
        {
            FCBinary bw = new FCBinary();

            byte[] body          = message.m_body;
            int    bodyLength    = message.m_bodyLength;
            int    uncBodyLength = bodyLength;

            lock (m_compressTypes) {
                if (m_compressTypes.ContainsKey(message.m_socketID))
                {
                    message.m_compressType = m_compressTypes.get(message.m_socketID);
                }
            }
            if (message.m_compressType == COMPRESSTYPE_GZIP)
            {
                using (MemoryStream cms = new MemoryStream()) {
                    using (GZipStream gzip = new GZipStream(cms, CompressionMode.Compress)) {
                        gzip.Write(body, 0, body.Length);
                    }
                    body       = cms.ToArray();
                    bodyLength = body.Length;
                }
            }
            int len = sizeof(int) * 4 + bodyLength + sizeof(short) * 3 + sizeof(byte) * 2;

            bw.writeInt(len);
            bw.writeShort((short)message.m_groupID);
            bw.writeShort((short)message.m_serviceID);
            bw.writeShort((short)message.m_functionID);
            bw.writeInt(message.m_sessionID);
            bw.writeInt(message.m_requestID);
            bw.writeByte((byte)message.m_state);
            bw.writeByte((byte)message.m_compressType);
            bw.writeInt(uncBodyLength);
            bw.writeBytes(body);
            byte[] bytes  = bw.getBytes();
            int    length = bytes.Length;
            int    ret    = FCServerSockets.send(m_socketID, message.m_socketID, bytes, length);

            m_upFlow += ret;
            bw.close();
            return(ret);
        }
Esempio n. 5
0
 /// <summary>
 /// 启动服务
 /// </summary>
 /// <param name="port">端口</param>
 /// <returns>状态</returns>
 public static int startServer(int port)
 {
     FCServerSockets.setListener(new FCServerSocketListener());
     return(FCServerSockets.start(0, port));
 }
Esempio n. 6
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="socketID">服务器套接字ID</param>
 /// <param name="localSID">客户端套接字ID</param>
 /// <param name="str">数据</param>
 /// <param name="len">长度</param>
 /// <returns>状态</returns>
 public static int sendByServer(int socketID, int localSID, byte[] str, int len)
 {
     return(FCServerSockets.send(socketID, localSID, str, len));
 }
Esempio n. 7
0
 /// <summary>
 /// 关闭服务
 /// </summary>
 /// <param name="socketID">套接字ID</param>
 /// <returns>状态</returns>
 public static int closeServer(int socketID)
 {
     return(FCServerSockets.close(socketID));
 }
Esempio n. 8
0
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>状态</returns>
        public int recv(SOCKDATA data)
        {
            if (!data.m_submit)
            {
                if (data.m_len == 1024 && data.m_buffer[0] == 'm' && data.m_buffer[1] == 'i' && data.m_buffer[2] == 'a' && data.m_buffer[3] == 'o')
                {
                    data.m_submit = true;
                    return(1);
                }
                else
                {
                    return(-1);
                }
            }
            int intSize = 4;

            data.m_index = 0;
            while (data.m_index < data.m_len)
            {
                int diffSize = 0;
                if (!data.m_get)
                {
                    diffSize = intSize - data.m_headSize;
                    if (diffSize == 0)
                    {
                        data.m_head = (((data.m_buffer[data.m_index] | (data.m_buffer[data.m_index + 1] << 8)) | (data.m_buffer[data.m_index + 2] << 0x10)) | (data.m_buffer[data.m_index + 3] << 0x18));
                    }
                    else
                    {
                        for (int i = 0; i < diffSize; i++)
                        {
                            data.m_headStr[data.m_headSize + i] = data.m_buffer[i];
                        }
                        data.m_head = (((data.m_headStr[0] | (data.m_headStr[1] << 8)) | (data.m_headStr[2] << 0x10)) | (data.m_headStr[3] << 0x18));
                    }
                    if (data.m_str != null)
                    {
                        data.m_str = null;
                    }
                    data.m_str = new byte[data.m_head];
                    if (diffSize > 0)
                    {
                        for (int i = 0; i < data.m_headSize; i++)
                        {
                            data.m_str[i] = data.m_headStr[i];
                        }
                        data.m_pos     += data.m_headSize;
                        data.m_headSize = intSize;
                    }
                }
                data.m_bufferRemain = data.m_len - data.m_index;
                data.m_strRemain    = data.m_head - data.m_pos;
                data.m_get          = data.m_strRemain > data.m_bufferRemain;
                int remain = Math.Min(data.m_strRemain, data.m_bufferRemain);
                Array.Copy(data.m_buffer, data.m_index, data.m_str, data.m_pos, remain);
                data.m_pos   += remain;
                data.m_index += remain;
                if (!data.m_get)
                {
                    FCServerSockets.recvClientMsg(data.m_hSocket, m_hSocket, data.m_str, data.m_head);
                    data.m_head = 0;
                    data.m_pos  = 0;
                    if (data.m_len - data.m_index == 0 || data.m_len - data.m_index >= intSize)
                    {
                        data.m_headSize = intSize;
                    }
                    else
                    {
                        data.m_headSize = data.m_bufferRemain - data.m_strRemain;
                        for (int j = 0; j < data.m_headSize; j++)
                        {
                            data.m_headStr[j] = data.m_buffer[data.m_index + j];
                        }
                        break;
                    }
                }
            }
            return(1);
        }