C++数据引擎开发第二周记录

//第一步:在core.hpp里面定义需要的头文件、全局变量等:目录文件:/emeralddb/src/include/core.hpp
/******************************************************************************
   Copyright (C) 2013 SequoiaDB Software Inc.
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License, version 3,
   as published by the Free Software Foundation.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.
   You should have received a copy of the GNU Affero General Public License
   along with this program. If not, see <http://www.gnu.org/license/>.
*******************************************************************************/
//宏定义,避免重复定义
#ifndef CORE_HPP__
#define CORE_HPP__
//下面是常用的标准头文件
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <sys/time.h>
#include <time.h>
#include <stdarg.h>
#include <unistd.h>
#include <syscall.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <sys/mman.h>
//下面是常用的c++头文件
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
//下面是全局变量,如换行符、操作中的各种权限、错误码定义等
#define OSS_MAX_PATHSIZE  PATH_MAX
#define OSS_FILE_SEP_STR  "/"
#define OSS_FILE_SEP_CHAR *((const char*)OSS_FILE_SEP_STR)[0]
#define OSS_NEWLINE       "\n"
// error code list
#define EDB_OK                                           0
#define EDB_IO                                          -1
#define EDB_INVALIDARG                                  -2
#define EDB_PERM                                        -3
#define EDB_OOM                                         -4
#define EDB_SYS                                         -5
#define EDB_PMD_HELP_ONLY                               -6
#define EDB_PMD_FORCE_SYSTEM_EDU                        -7
#define EDB_TIMEOUT                                     -8
#define EDB_QUIESCED                                    -9
#define EDB_EDU_INVAL_STATUS                            -10
#define EDB_NETWORK                                     -11
#define EDB_NETWORK_CLOSE                               -12
#define EDB_APP_FORCED                                  -13
#define EDB_IXM_ID_EXIST                                -14
#define EDB_HEADER_INVALID                              -15
#define EDB_IXM_ID_NOT_EXIST                            -16
#define EDB_NO_ID                                       -17
#endif

//第二步:定义ossSocket.hpp头文件,声明socket操作需要的成员变量和成员函数,目录文件:/emeralddb/src/include/ossSocket.hpp
/*******************************************************************************
   Copyright (C) 2013 SequoiaDB Software Inc.
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License, version 3,
   as published by the Free Software Foundation.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.
   You should have received a copy of the GNU Affero General Public License
   along with this program. If not, see <http://www.gnu.org/license/>.
*******************************************************************************/
#ifndef OSSNETWORK_HPP_
#define OSSNETWORK_HPP_
#include "core.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <string.h>
#define SOCKET_GETLASTERROR errno
// by default 10ms timeout
#define OSS_SOCKET_DFT_TIMEOUT 10000
// max hostname
#define OSS_MAX_HOSTNAME NI_MAXHOST
#define OSS_MAX_SERVICENAME NI_MAXSERV
class _ossSocket
{
private ://私有成员变量定义
   int _fd ;
   socklen_t _addressLen ;  //socket地址长度
   socklen_t _peerAddressLen ;  //对方地址长度
   struct sockaddr_in  _sockAddress ; //本地socket套接字
   struct sockaddr_in  _peerAddress ;   //远程socket套接字
   bool _init ;    //是否初始化操作
   int _timeout ;   //超时时间
protected://受保护成员函数方法定义
   unsigned int _getPort ( sockaddr_in *addr ) ;    //根据socket地址或端口函数
   int _getAddress ( sockaddr_in *addr, char *pAddress, unsigned int length ) ; //获取socket地址函数
public ://公共成员函数方法定义
   int setSocketLi ( int lOnOff, int linger ) ;   
   void setAddress(const char * pHostName, unsigned int port );
   // Create a listening socket
   _ossSocket();      //创建监听
   _ossSocket ( unsigned int port, int timeout = 0 ) ;  //创建监听(带参)
   // Create a connecting socket
   _ossSocket ( const char *pHostname, unsigned int port, int timeout = 0 ) ;//创建socket连接
   // Create from a existing socket
   _ossSocket ( int *sock, int timeout = 0 ) ;    //根据一个已存在的socket信息创建一个socket连接
   ~_ossSocket ()       //释放资源的析构函数
   {
      close () ;
   }
   int initSocket () ;      //初始化socket套接字函数
   int bind_listen () ;      //绑定socket监听函数
   bool isConnected () ;     //判断socket连接状态
   int send ( const char *pMsg, int len,
                int timeout = OSS_SOCKET_DFT_TIMEOUT,  //客户端开始发送信息
                int flags = 0 ) ;
   int recv ( char *pMsg, int len,
                int timeout = OSS_SOCKET_DFT_TIMEOUT,  //客户端开始接收信息,如果超时时间为0,则一直等到接收len长度信息为止
                int flags = 0 ) ;
   int recvNF ( char *pMsg, int &len,
                  int timeout = OSS_SOCKET_DFT_TIMEOUT ) ; //客户端开始接收信息函数,收到信息后立即返回
   int connect () ;      //开始socket连接
   void close () ;      //关闭socket连接
   int accept ( int *sock, struct sockaddr *addr, socklen_t *addrlen,
                  int timeout = OSS_SOCKET_DFT_TIMEOUT ) ;      //socket服务端接收客户端消息函数
   int disableNagle () ;     //关闭使用tcp打包消息处理,数据库交互一般不使用,要求实时单个数据包交互处理
   unsigned int getPeerPort () ;
   int getPeerAddress ( char *pAddress, unsigned int length ) ;//获取对方端口
   unsigned int getLocalPort () ;
   int getLocalAddress ( char *pAddress, unsigned int length ) ;//获取本地端口
   int setTimeout ( int seconds ) ;    //设置超时时间
   static int getHostName ( char *pName, int nameLen ) ; //获取主机名
   static int getPort ( const char *pServiceName, unsigned short &port ) ;//根据服务名转化为端口号,/etc/services下面有服务名和端口号匹配的所有定义
};
typedef class _ossSocket ossSocket ;
#endif

//第三步:定义ossSocket.cpp头文件,具体实现socket操作需要的成员变量和成员函数,目录文件:/emeralddb/src/oss/ossSocket.cpp
/*******************************************************************************
   Copyright (C) 2013 SequoiaDB Software Inc.
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License, version 3,
   as published by the Free Software Foundation.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.
   You should have received a copy of the GNU Affero General Public License
   along with this program. If not, see <http://www.gnu.org/license/>.
*******************************************************************************/
#include <stdio.h>
#include "ossSocket.hpp"
// Create a listening socket 创建一个socket监听
_ossSocket::_ossSocket ( unsigned int port, int timeout )
{
   _init = false ;
   _fd = 0 ;
   _timeout = timeout ;
   memset ( &_sockAddress, 0, sizeof(sockaddr_in) ) ; //初始化_sockAddress分配空间并清0
   memset ( &_peerAddress, 0, sizeof(sockaddr_in) ) ;
   _peerAddressLen = sizeof (_peerAddress) ;  //对方地址长度 
   _sockAddress.sin_family = AF_INET ;   //初始化本地的AF_INET属性
   _sockAddress.sin_addr.s_addr = htonl ( INADDR_ANY ) ;//设置监听的来源是全部来源
   _sockAddress.sin_port = htons ( port ) ;  //设置监听端口号
   _addressLen = sizeof ( _sockAddress ) ;  //设置地址长度
}
// Create a socket 创建一个socket连接
_ossSocket::_ossSocket()
{
   _init = false;
   _timeout = 0;
   _fd = 0;
   memset( &_sockAddress, 0, sizeof(sockaddr_in) );
   memset( &_peerAddress, 0, sizeof(sockaddr_in) );
   _peerAddressLen = sizeof(_peerAddress);
}
// Create a connecting socket 创建一个客户端的socket套接字
_ossSocket::_ossSocket ( const char *pHostname, unsigned int port, int timeout )
{
   struct hostent *hp ;
   _init = false ;
   _timeout = timeout ;
   _fd = 0 ;
   memset ( &_sockAddress, 0, sizeof(sockaddr_in) ) ;
   memset ( &_peerAddress, 0, sizeof(sockaddr_in) ) ;
   _peerAddressLen = sizeof (_peerAddress) ;
   _sockAddress.sin_family = AF_INET ;
   if ( (hp = gethostbyname ( pHostname )))//解析对方的机器名,如果解析成功用收到的地址,否则尝试用pHostname作为绝对地址
      _sockAddress.sin_addr.s_addr = *((int *)hp->h_addr_list[0] ) ; 
   else
      _sockAddress.sin_addr.s_addr = inet_addr ( pHostname ) ;
   _sockAddress.sin_port = htons ( port ) ;
   _addressLen = sizeof ( _sockAddress ) ;
}
// Create from a existing socket //从一个已存在的套接字构建socket
_ossSocket::_ossSocket ( int *sock, int timeout )
{
   int rc = EDB_OK ;
   _fd = *sock ;
   _init = true ;
   _timeout = timeout ;
   _addressLen = sizeof ( _sockAddress ) ;
   memset ( &_peerAddress, 0, sizeof(sockaddr_in) ) ;
   _peerAddressLen = sizeof ( _peerAddress ) ;
   rc = getsockname ( _fd, (sockaddr*)&_sockAddress, &_addressLen ) ;//从一个已存在的socket里面得到对方的地址
   if ( rc )
   {
      printf ( "Failed to get sock name, error = %d",
              SOCKET_GETLASTERROR ) ;
      _init = false ;
   }
   else
   {
      //get peer address      
      rc = getpeername ( _fd, (sockaddr*)&_peerAddress, &_peerAddressLen ) ;
      if ( rc )
      {
         printf ( "Failed to get peer name, error = %d",
                 SOCKET_GETLASTERROR ) ;
      }
   }
}
int ossSocket::initSocket ()
{
   int rc = EDB_OK ;
   if ( _init )
   {
      goto done ;
   }
   memset ( &_peerAddress, 0, sizeof(sockaddr_in) ) ;
   _peerAddressLen = sizeof ( _peerAddress ) ;
   _fd = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ;//创建一个新的socket套接字
   if ( -1 == _fd )
   {
      printf ( "Failed to initialize socket, error = %d",
              SOCKET_G
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值