欢迎大家来到IT世界,在知识的湖畔探索吧!
CBrother是一门跨平台的脚本语言,支持真正的多线程,它抛弃了传统脚本语言的GIL全局锁技术,从而更能发挥多核CPU的强劲性能。
CBrother语法继承了C类语法体系,有Java,C++,C#,JS,PHP等语言基础便可以直接上手编写代码。
你可以用C++扩展它,也可以将CBrother嵌入C++语言里使用。
Cbrother官网:http://www.cbrother.net
让编程变得更简单
创建一个 helloworld.cb 代码:
function main(parm)
{
print "Hello World!";
}
欢迎大家来到IT世界,在知识的湖畔探索吧!
创建小型HttpServer服务器:
欢迎大家来到IT世界,在知识的湖畔探索吧!import CBHttp.code
function main(parm)
{
var httpServer = new HttpServer();
httpServer.startServer();
while(1) //主线程不能退出
{
Sleep(1000);
}
}
MySQL用法
import CBMySQL.code
function main(parm)
{
var mysql = new MySQL("127.0.0.1",3306,"root","root","test");
if(!mysql.connect())
{
print "mysql connect err!!";
return;
}
var res = mysql.upDate("INSERT INTO test (`strv`, `intv`) VALUES ('333', 222)");
if(!res)
{
print "update err " + mysql.getErr();
}
res = mysql.upDate("INSERT INTO test (`strv`, `intv`) VALUES ('555', 666)");
if(!res)
{
print "update err " + mysql.getErr();
}
var res = mysql.query("select * from test");
if(!res)
{
print "query err " + mysql.getErr();
return;
}
while(mysql.next())
{
var strv = mysql.getString(0);
var intv = mysql.getInt(1);
print "mysql data: " + strv + " " + intv;
var strv1 = mysql.getString("strv");
var intv = mysql.getInt("intv");
print "name query mysql data: " + strv + " " + intv;
}
mysql.closeConnect();
}
Socket通信
服务端:
欢迎大家来到IT世界,在知识的湖畔探索吧!import CBSocket.code
class TcpAction
{
var tcpModule;
function OnAccept(sock)
{
print "accept " + sock;
}
function OnClose(sock)
{
print "onclose " + sock;
}
function OnRecv(sock,byteArray,len)
{
print "onrecv " + sock + " " + byteArray.readString() + " len:" + len;
var byteArray = new ByteArray();
byteArray.writeString("hello tcp client");
tcpModule.sendData(sock,byteArray);
}
function OnSend(sock,len)
{
print "onsend " + sock + " " + len;
}
}
function main(parm)
{
var tcpModule = new TcpModule();
var tcpAction = new TcpAction();
tcpAction.tcpModule = tcpModule;
tcpModule.setTcpAction(tcpAction); //设置TCP处理类
tcpModule.addListenPort(6060); //监听6060端口
tcpModule.addListenPort(6061,"*"); //监听6061端口,IPV4和IPV6兼容
tcpModule.start();
while(1) //主线程不能退出
{
Sleep(1000);
}
}
客户端:
import CBSocket.code
class TcpAction
{
var tcpModule;
function OnClose(sock)
{
print "onclose " + sock;
}
function OnRecv(sock,byteArray,len)
{
print "onrecv " + sock + " " + byteArray.readString() + " len:" + len;
}
function OnSend(sock,len)
{
print "onsend " + sock + " " + len;
}
function OnConnect(sock)
{
print "onconnect " + sock;
var byteArray = new ByteArray();
byteArray.writeString("hellp tcp server!");
tcpModule.sendData(sock,byteArray);
}
}
function main(parm)
{
var tcpModule = new TcpModule();
var tcpAction = new TcpAction();
tcpAction.tcpModule = tcpModule;
tcpModule.setTcpAction(tcpAction); //设置TCP处理类
tcpModule.start();
tcpModule.connect("127.0.0.1",6060);
while(1) //主线程不能退出
{
Sleep(1000);
}
}
用CLib来加载一个第三方动态库
function loadLib()
{
var systemName = SystemName();
var libPath = GetRoot();
if(systemName == "windows")
{
libPath += "testdll.dll";
}
else if(systemName == "linux")
{
libPath += "libtestdll.so";
}
else
{
libPath += "libtestdll.dylib";
}
var clibs = new CLib(libPath);
if(!clibs.load())
{
print "load err!";
return 0;
}
print "load success!!";
}
调用C++ 代码
C++:
//c++函数定义
int tAdd(int a,int b)
{
return a + b;
}
CB代码:
mport CBCLib.code
function main(parm)
{
var clibs = loadLib();
var tAdd = clibs.findFunc("tAdd","int","int","int");
var res = tAdd.callFunc(100,300);
print "tAdd : " + res;
}
基本类型
var v1 = 100; //整型
var v2 = 100.0; //浮点型
var v3 = "100"; //字符串
var v4 = true; //or false 布尔值
var v5 = null; //空对象
var v6; //空对象
var v7 = UINT(100); //无符号整型
var v8 = 0x00ff00aa; //整型 0x开头为16进制赋值
var v9 = 0o11537; //整型 0o开头为8进制赋值
var v10 = 0b10101; //整型 0b开头为2进制赋值
var v11 = new Array(); //数组
var v12 = new Map(); //字典
var v13 = new List(); //列表
class MyClass
{
var a = 100;
var b = "100";
}
var v14 = new MyClass(); //自定义类型
var v15 = 0xffffffffffff0000; //数值超过int范围时,自动变为long
var v16 = 4200000000; //数值超过int范围时,自动变为long
var v17 = 'a'; //整型 a的ascii值97
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/31397.html