xml地图|网站地图|网站标签 [设为首页] [加入收藏]

正规赌博平台

当前位置:网上十大正规赌博平台 > 正规赌博平台 > C语言明解,PDO相当管理详解

C语言明解,PDO相当管理详解

来源:http://www.nb-machinery.com 作者:网上十大正规赌博平台 时间:2019-07-11 00:10

异常处理:

int pow4(int x){}

简单的测试代码:

您可能感兴趣的文章:

  • PHP的异常处理类Exception的使用及说明
  • PHP如何抛出异常处理错误
  • PHP中的错误处理、异常处理机制分析
  • php中异常处理方法小结
  • PHP中异常处理的一些方法整理
  • PHP 的异常处理、错误的抛出及回调函数等面向对象的错误处理方法
  • 一个显示效果非常不错的PHP错误、异常处理类
  • 实例讲解如何在PHP的Yii框架中进行错误和异常处理
  • PHP异常处理Exception类
  • php5编程中的异常处理详细方法介绍
  • php异常处理方法实例汇总
  • PHP异常处理定义与使用方法分析

*/

#ifndef _RPACKET_H  #define _RPACKET_H    #include "buffer.h"    typedef struct rpacket  {      unsigned long cmd;      unsigned long len;     //包长      unsigned long rpos;    //读下标      unsigned long data_remain;      unsigned long binbufpos;      unsigned long begin_pos;      buffer_t binbuf;       //用于存放跨越buffer_t边界数据的buffer_t      buffer_t buf;          //存放此数据包内容的buffer_t链表      buffer_t readbuf;      //当前rpos所在的buffer_t  }*rpacket_t;    struct wpacket;    rpacket_t rpacket_create(buffer_t,unsigned long pos);  rpacket_t rpacket_create_by_wpacket(struct wpacket*);//通过wpacket构造  void      rpacket_destroy(rpacket_t*);    //数据读取接口  unsigned long  rpacket_len(rpacket_t);  unsigned long  rpacket_read_cmd(rpacket_t);  unsigned long  rpacket_data_remain(rpacket_t);  unsigned char  rpacket_read_char(rpacket_t);  unsigned short rpacket_read_short(rpacket_t);  unsigned long  rpacket_read_long(rpacket_t);  double         rpacket_read_double(rpacket_t);  const char*    rpacket_read_string(rpacket_t);  const void*    rpacket_read_binary(rpacket_t,unsigned long *len);    #endif

#include "rpacket.h"  #include "wpacket.h"  #include <stdlib.h>  #include <string.h>    rpacket_t rpacket_create(buffer_t b,unsigned long pos/*数据在b中的起始下标*/)  {      rpacket_t r = calloc(1,sizeof(*r));      r->binbuf = 0;      r->binbufpos = 0;      r->buf = buffer_acquire(0,b);      r->readbuf = buffer_acquire(0,b);      r->len = *(unsigned long*)(&(b->buf[pos]));      r->data_remain = r->len;      r->rpos = pos   sizeof(r->len);      r->begin_pos = pos;      return r;  }    rpacket_t rpacket_create_by_wpacket(struct wpacket *w)  {      rpacket_t r = calloc(1,sizeof(*r));      r->binbuf = 0;      r->binbufpos = 0;      r->buf = buffer_acquire(0,w->buf);      r->readbuf = buffer_acquire(0,w->buf);      //这里的len只记录构造时wpacket的len,之后wpacket的写入不会影响到rpacket的len      r->len = *(unsigned long*)(&(w->buf->buf[w->begin_pos]));      r->data_remain = r->len;      r->rpos = 0   sizeof(r->len);      r->begin_pos = w->begin_pos;      return r;  }    void      rpacket_destroy(rpacket_t *r)  {      //释放所有对buffer_t的引用      buffer_release(&(*r)->buf);      buffer_release(&(*r)->readbuf);      buffer_release(&(*r)->binbuf);  }    unsigned long  rpacket_read_cmd(rpacket_t r)  {      return r->cmd;  }    unsigned long  rpacket_len(rpacket_t r)  {      return r->len;  }    unsigned long  rpacket_data_remain(rpacket_t r)  {      return r->data_remain;  }    static int rpacket_read(rpacket_t r,char *out,unsigned long size)  {      buffer_t _next = 0;      if(r->data_remain < size)          return -1;      while(size>0)      {          unsigned long copy_size = r->readbuf->size - r->rpos;          copy_size = copy_size >= size ? size:copy_size;          memcpy(out,r->readbuf->buf   r->rpos,copy_size);          size -= copy_size;          r->rpos  = copy_size;          r->data_remain -= copy_size;          out  = copy_size;          if(r->rpos >= r->readbuf->size && r->data_remain)          {              //当前buffer数据已经被读完,切换到下一个buffer              r->rpos = 0;              r->readbuf = buffer_acquire(r->readbuf,r->readbuf->next);          }      }      return 0;  }    unsigned char  rpacket_read_char(rpacket_t r)  {      unsigned char value = 0;      rpacket_read(r,(char*)&value,sizeof(value));      return value;  }    unsigned short rpacket_read_short(rpacket_t r)  {      unsigned short value = 0;      rpacket_read(r,(char*)&value,sizeof(value));      return value;  }    unsigned long  rpacket_read_long(rpacket_t r)  {      unsigned long value = 0;      rpacket_read(r,(char*)&value,sizeof(value));      return value;  }    double   rpacket_read_double(rpacket_t r)  {      double value = 0;      rpacket_read(r,(char*)&value,sizeof(value));      return value;  }    const char* rpacket_read_string(rpacket_t r)  {      unsigned long len = 0;      return (const char *)rpacket_read_binary(r,&len);  }    const void* rpacket_read_binary(rpacket_t r,unsigned long *len)  {      void *addr = 0;      unsigned long size = rpacket_read_long(r);      *len = size;      if(r->data_remain < size)          return addr;      if(r->buf->size - r->rpos >= size)      {          addr = &r->buf[r->rpos];          r->rpos  = size;          r->data_remain -= size;          if(r->rpos >= r->readbuf->size && r->data_remain)          {              //当前buffer数据已经被读完,切换到下一个buffer              r->rpos = 0;              r->readbuf = buffer_acquire(r->readbuf,r->readbuf->next);          }      }      else      {          //数据跨越了buffer边界,创建binbuf,将数据拷贝到binbuf中          if(!r->binbuf)          {              r->binbufpos = 0;              r->binbuf = buffer_create_and_acquire(0,r->len);          }          addr = r->binbuf->buf   r->binbufpos;          while(size)          {              unsigned long copy_size = r->readbuf->size - r->rpos;              copy_size = copy_size >= size ? size:copy_size;              memcpy(r->binbuf->buf   r->binbufpos,r->readbuf->buf   r->rpos,copy_size);              size -= copy_size;              r->rpos  = copy_size;              r->data_remain -= copy_size;              r->binbufpos  = copy_size;                      if(r->rpos >= r->readbuf->size && r->data_remain)              {                  //当前buffer数据已经被读完,切换到下一个buffer                  r->rpos = 0;                  r->readbuf = buffer_acquire(r->readbuf,r->readbuf->next);              }          }        }      return addr;  }

PDO::ERRMODE_WARNING: 引发 E_WARNING 错误,主动报错

*/

rpacket从网络中接收到的数据封包:

<?php
try{
  $pdo = new PDO("mysql:host=localhost;dbname=jikexueyuan","root","");
  //主动抛出异常
  $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION );
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,'jike','w',55)";
//用try{}catch(){}抓取异常
try{
  $res = $pdo->exec($sql);
}catch(PDOException $e){
  echo $e->getMessage();
}

创建一个函数,返回元素个数为no的int型数组vc中的最小值。

本文贴出用C语言重写的网络封包,主体设计思路前文已经介绍过,就是尽可能的共享缓存,减少不必要的内存拷贝.

MYSQL:默认为静默模式,错就错,不报错

 1  #include "stdio.h"   2    3  int main(int argc, char const *argv[])   4  {   5      int x,y;   6    7      puts("请输入俩个整数。");   8      printf("整数1:");scanf("%d",&x);   9      printf("整数2:");scanf("%d",&y);  10   11      printf("较小的整数是%dn",minof(x,y));  12   13      return 0;  14   15   16  }  17   18  int minof(int x,int y){  19   20      if (x<y) {  21          return x;  22      }else{  23          return y;  24      }  25  }

 

<?php
//默认是不提示的 需要用 errorCode() errorInfo();
try{
  $pdo = new PDO("mysql:host=localhost;dbname=jikexueyuan","root","");
  //下面这句是默认设置,有无均可
  $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT);
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,'jike','w',55)";
$res = $pdo->exec($sql);
if($res){
  echo "OK";
}else{
  echo $pdo->errorCode();
  print_r($pdo->errorInfo());
} 

 

void test1()  {      wpacket_t w = wpacket_create(12);      rpacket_t r,r1;      wpacket_t w1 = 0;      const char *str;      wpacket_write_string(w,"hello kenny");      r = rpacket_create_by_wpacket(w);      wpacket_destroy(&w);      str = rpacket_read_string(r);      printf("str=%sn",str);      w1 = wpacket_create_by_rpacket(r);      r1 = rpacket_create_by_wpacket(w1);      str = rpacket_read_string(r1);      printf("str=%sn",str);      rpacket_destroy(&r);      rpacket_destroy(&r1);      wpacket_destroy(&w1);  }    void test2()  {      wpacket_t w = wpacket_create(12);      rpacket_t r;      write_pos wp;      wpacket_write_long(w,1);      wp = wpacket_get_writepos(w);      wpacket_write_short(w,2);      wpacket_write_string(w,"hello kenny");      wpacket_rewrite_short(&wp,4);      r = rpacket_create_by_wpacket(w);      printf("%un",rpacket_read_long(r));      printf("%un",rpacket_read_short(r));      printf("%sn",rpacket_read_string(r));      rpacket_destroy(&r);      wpacket_destroy(&w);  }

以前,当PHP碰到错误的时候,会直接报错,错误处理会变得相当麻烦。后来,当错误发生之后,会将错误信息不再直接输出,放到一个类的对象里(PDOException)

*/

最后,代码没有做优化,内存的分配都是经由calloc,后面会用内存池代替。

PDO::ERRMODE_SILENT: 默认模式,不主动报错,需要主动以 $pdo->errorInfo()的形式获取错误信息。

 

#ifndef _WPACKET_H  #define _WPACKET_H  #include "buffer.h"  typedef struct wpacket  {      unsigned long *len;      buffer_t buf;      buffer_t writebuf;      unsigned long wpos;      unsigned char factor;      unsigned long begin_pos;  }*wpacket_t;  struct rpacket;      typedef struct  {      buffer_t buf;      unsigned long wpos;  }write_pos;    wpacket_t wpacket_create(unsigned long size);  wpacket_t wpacket_create_by_rpacket(struct rpacket*);//通过rpacket构造  void wpacket_destroy(wpacket_t*);    write_pos wpacket_get_writepos(wpacket_t);    void wpacket_write_char(wpacket_t,unsigned char);  void wpacket_write_short(wpacket_t,unsigned short);  void wpacket_write_long(wpacket_t,unsigned long);  void wpacket_write_double(wpacket_t,double);    void wpacket_rewrite_char(write_pos*,unsigned char);  void wpacket_rewrite_short(write_pos*,unsigned short);  void wpacket_rewrite_long(write_pos*,unsigned long);  void wpacket_rewrite_double(write_pos*,double);    //不提供对非定长数据的rewrite  void wpacket_write_string(wpacket_t,const char*);  void wpacket_write_binary(wpacket_t,const void*,unsigned long);  #endif

#include "wpacket.h"  #include <stdlib.h>  #include <string.h>  #include <assert.h>  #include "rpacket.h"    static int is_pow_of_2(unsigned long size)  {      return !(size&(size-1));  }    static unsigned char GetK(unsigned long size)  {      unsigned char k = 0;      if(!is_pow_of_2(size))      {          size = (size << 1);      }      //除最高为1位以外,其它位全部清0      while(size > 1)      {          k  ;          size = size >> 1;      }      return k;  }    wpacket_t wpacket_create(unsigned long size)  {      unsigned char k = GetK(size);      wpacket_t w;      size = 1 << k;      w = calloc(1,sizeof(*w));      w->factor = k;      w->wpos = sizeof(w->len);      w->buf = buffer_create_and_acquire(0,size);      w->writebuf = buffer_acquire(0,w->buf);      w->len = (unsigned long*)w->buf->buf;      *(w->len) = 0;      w->buf->size = sizeof(w->len);      w->begin_pos = 0;      return w;  }    wpacket_t wpacket_create_by_rpacket(struct rpacket *r)  {      wpacket_t w = calloc(1,sizeof(*w));      w->factor = 0;      w->writebuf = 0;      w->begin_pos = r->begin_pos;      w->buf = buffer_acquire(0,r->buf);      w->len = (unsigned long*)(r->buf->buf   r->begin_pos);      w->wpos = 0;      return w;  }    write_pos wpacket_get_writepos(wpacket_t w)  {      write_pos wp = {w->writebuf,w->wpos};      return wp;  }    void wpacket_destroy(wpacket_t *w)  {      buffer_release(&(*w)->buf);      buffer_release(&(*w)->writebuf);      free(*w);      *w = 0;  }    static void wpacket_expand(wpacket_t w)  {      unsigned long size;      w->factor <<= 1;      size = 1 << w->factor;      w->writebuf->next = buffer_create_and_acquire(0,size);      w->writebuf = buffer_acquire(w->writebuf,w->writebuf->next);       w->wpos = 0;  }      static void wpacket_copy(wpacket_t w,buffer_t buf)  {      char *ptr = buf->buf;      buffer_t tmp_buf = w->buf;      unsigned long copy_size;      while(tmp_buf)      {          copy_size = tmp_buf->size - w->wpos;          memcpy(ptr,tmp_buf->buf,copy_size);          ptr  = copy_size;          w->wpos = 0;          tmp_buf = tmp_buf->next;      }  }    static void wpacket_write(wpacket_t w,char *addr,unsigned long size)  {      char *ptr = addr;      unsigned long copy_size;      buffer_t tmp;      unsigned char k;      if(!w->writebuf)      {          /*wpacket是由rpacket构造的,这里执行写时拷贝,          * 执行完后wpacket和构造时传入的rpacket不再共享buffer          */          k = GetK(*w->len);          w->factor = k;          tmp = buffer_create_and_acquire(0,1 << k);          wpacket_copy(w,tmp);          w->begin_pos = 0;          w->len = (unsigned long*)tmp->buf;          w->wpos = sizeof(*w->len);          w->buf = buffer_acquire(w->buf,tmp);          w->writebuf = buffer_acquire(w->writebuf,w->buf);      }      while(size)      {          copy_size = w->buf->capacity - w->wpos;          if(copy_size == 0)          {              wpacket_expand(w);//空间不足,扩展              copy_size = w->buf->capacity - w->wpos;          }          copy_size = copy_size > size ? size:copy_size;          memcpy(w->writebuf->buf   w->wpos,ptr,copy_size);          w->writebuf->size  = copy_size;          (*w->len)  = copy_size;          w->wpos  = copy_size;          ptr  = copy_size;          size -= copy_size;      }  }      void wpacket_write_char(wpacket_t w,unsigned char value)  {      wpacket_write(w,(char*)&value,sizeof(value));  }    void wpacket_write_short(wpacket_t w,unsigned short value)  {      wpacket_write(w,(char*)&value,sizeof(value));  }    void wpacket_write_long(wpacket_t w,unsigned long value)  {      wpacket_write(w,(char*)&value,sizeof(value));  }    void wpacket_write_double(wpacket_t w ,double value)  {      wpacket_write(w,(char*)&value,sizeof(value));  }    static void wpacket_rewrite(write_pos *wp,char *addr,unsigned long size)  {      char *ptr = addr;      unsigned long copy_size;      unsigned long pos = wp->wpos;      while(size)      {          copy_size = wp->buf->capacity - pos;          copy_size = copy_size > size ? size:copy_size;          memcpy(wp->buf->buf   pos,ptr,copy_size);          ptr  = copy_size;          size -= copy_size;          pos  = copy_size;          if(size && pos >= wp->buf->capacity)          {              assert(wp->buf->next);              wp->buf = wp->buf->next;              pos = 0;          }        }  }    void wpacket_rewrite_char(write_pos *wp,unsigned char value)  {      wpacket_rewrite(wp,&value,sizeof(value));  }    void wpacket_rewrite_short(write_pos *wp,unsigned short value)  {      wpacket_rewrite(wp,(char*)&value,sizeof(value));  }    void wpacket_rewrite_long(write_pos *wp,unsigned long value)  {      wpacket_rewrite(wp,(char*)&value,sizeof(value));  }    void wpacket_rewrite_double(write_pos *wp,double value)  {      wpacket_rewrite(wp,(char*)&value,sizeof(value));  }    void wpacket_write_string(wpacket_t w ,const char *value)  {      wpacket_write_binary(w,value,strlen(value) 1);  }    void wpacket_write_binary(wpacket_t w,const void *value,unsigned long size)  {      assert(value);      wpacket_write_long(w,size);      wpacket_write(w,(char*)value,size);  }

方式一:

 

其次,封包主要是为了适合网络游戏等有固定模式的,面向字节流的协议,所以并不适合用于http类协议的处理.

方式二:

/*练习6-3

项目地址:

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用PHP能有所帮助,如果有疑问大家可以留言交流。

 

wpacket发送数据封包:

PDO::ERRMODE_EXCEPTION: 主动抛出 exceptions 异常,需要以try{}cath(){}输出错误信息。

 1 #include "stdio.h"   2    3 int main(int argc, char const *argv[])   4 {   5     int x;   6        7     puts("请输1个整数。");   8     printf("整数1:");scanf("%d",&x);   9       10     printf("该数的4次幂是%d.n",pow4(x));  11       12     return 0;  13       14       15 }  16   17 int pow4(int x){  18       19     return x*x*x*x;  20 }

pdo异常处理设置:

 

设置PDO::ATTR_ERRMODE ,有以下三个值:

创建一个函数,返回int型整数的立方。

PHP:默认为直接报错

 这里涉及到矩阵乘法:

<?php
try{
  $pdo = new PDO("mysql:host=localhost;dbname=jikexueyuan","root","");
  //主动以警告的形式报错
  $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,'jike','w',55)";
//若有错误直接在浏览器页面中显示错误信息
$res = $pdo->exec($sql);

 

实例:

 

方式三:

 

PDO:默认为静默模式,错就错,不报错

 /*

总结

 

int cube(int x){..}

void alert(int no){}

/*练习6-6

 1 #include<stdio.h>   2    3 int ax = 0;   4    5 int main()   6 {   7     static int bx =0;   8        9     puts("ax bx");  10     for (int i = 0; i<5; i  ) {  11         printf("%d=n",ax  ,bx  );  12   13     }  14       15       16     return 0;  17 }

 /*

void intary_rcpy( int v1[],const int v2[],int no){}


 1 #include "stdio.h"   2    3    4 void rev_intary(int v1[],const int v2[],int no){   5        6     for (int i=0; i<no; i  ) {   7         v1[i]=v2[no-1-i];   8     }   9   10 }  11   12   13 int main(int argc, char const *argv[])  14 {  15     const int n = 6;  16     const int v2[n]={11,23,45,34,19,76};  17     int v1[n];  18       19     rev_intary(v1,v2,n);  20     for (int i = 0; i<n; i  ) {  21         printf("v1[%d]=%dn",i,v1[i]);  22   23     }  24       25       26     return 0;  27 }

练习6-11

 

 

创建一个函数,返回俩个int型整数中较小一数的值。

 /*

 

int minof(int x,int y)

本文由网上十大正规赌博平台发布于正规赌博平台,转载请注明出处:C语言明解,PDO相当管理详解

关键词: bbin娱乐官网

上一篇:thinkPHP自定义类实现方法详解,C语言宏中

下一篇:没有了